Realization of Baidu Map localization process based on Android _android

Source: Internet
Author: User

I. Description of the problem

LBS Location Service is an important function of Android applications, the application is more and more extensive, the following we gradually learn and realize lbs related applications such as positioning, map, navigation and so on, first of all, we see how to achieve positioning based on Baidu map function

Second, configure the environment

1. Registration Key: Address http://developer.baidu.com/map/

2, download the location SDK, and import the SDK as shown in the figure:

Iii. Preparation of MyApplication class

Write the MyApplication class, in order to use the application component in which we can encapsulate the method of implementation positioning

Encapsulates the following methods

1. Get positioning information--requestlocationinfo ()

2. Send location information via broadcast--sendbroadcast ()

3, stop positioning--stoplocationclient ()

public class MyApplication extends application{public locationclient mlocationclient = null;
  Public Geofenceclient mgeofenceclient;
  Public Mylocationlistenner MyListener = new Mylocationlistenner ();
  public static String TAG = "MyApplication";
  private static MyApplication minstance = null;
    @Override public void OnCreate () {minstance = this;
    Mlocationclient = new Locationclient (this);
    /** * Project key, oneself to the official website application Http://lbsyun.baidu.com/apiconsole/key * * * Mlocationclient.setak ("Your Application Key");
    Mlocationclient.registerlocationlistener (MyListener);
    Mgeofenceclient = new Geofenceclient (this);
    Super.oncreate (); LOG.D (TAG, "...)
  Application onCreate ... pid= "+ process.mypid ());
  public static MyApplication getinstance () {return minstance; /** * Stop positioning/public void stoplocationclient () {if (mlocationclient!= null && Mlocationclient.iss
    Tarted ()) {mlocationclient.stop (); }/** * Initiate positioning/public void Requestlocationinfo () {setlocationoption ();
    if (mlocationclient!= null &&!mlocationclient.isstarted ()) {Mlocationclient.start ();
    } if (mlocationclient!= null && mlocationclient.isstarted ()) {mlocationclient.requestlocation (); }/** * Set the relevant parameters of Baidu map * * private void Setlocationoption () {locationclientoption option = new Locationclie
    Ntoption (); Option.setopengps (TRUE); Open the GPS Option.setcoortype ("Bd09ll");
    Set the coordinate type Option.setservicename ("com.baidu.location.service_v2.9");
    Option.setpoiextrainfo (TRUE);
    Option.setaddrtype ("All");
    Option.setpoinumber (10);
    Option.disablecache (TRUE);
  mlocationclient.setlocoption (option); /** * Listener function, when the update location, format into a string, output to the screen/public class Mylocationlistenner implements Bdlocationlistener {@ Override public void Onreceivelocation (bdlocation location) {if (location = null) {sendbroadcast ("Location failed!
        ");
      Return} sendbroadcast (Location.getaddrstr ());
        public void Onreceivepoi (Bdlocation poilocation) {if (poilocation = = null) {Sendbroadcast ("Locate failed!");
      Return
    } sendbroadcast (Poilocation.getaddrstr ()); }/** * received send broadcast * @param address/public void Sendbroadcast (String address) {stoplocationclient (
    );
    Intent Intent = new Intent (MAINACTIVITY.LOCATION_BCR);
    Intent.putextra ("Address", address);
  Sendbroadcast (Intent); }
}

three, main program mainactivity

public class Mainactivity extends activity{public static String TAG = "Loctestdemo";
  Private Broadcastreceiver Broadcastreceiver;
  public static String location_bcr = "LOCATION_BCR";
  Private Button locbtn;
  Private TextView Locinfo;
  Private MyApplication Application;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    application= (MyApplication) super.getapplication ();
    Initialize ();
    Initializeviews ();
  Initializelisteners (); private void Initialize () {registerbroadcastreceiver ();//registered broadcast} private void Initializeviews () {locbtn
    = (Button) Findviewbyid (r.id.location);
  Locinfo = (TextView) Findviewbyid (r.id.location_info); private void Initializelisteners () {Locbtn.setonclicklistener (new Onclicklistener () {@Override Publ
      IC void OnClick (View v) {locinfo.settext ("positioning ..."); Call Request location Information Application.requeStlocationinfo ();
  }
    }); /** * Registers a broadcast, listens for the location result, receives the broadcast to obtain the address information * * The private void Registerbroadcastreceiver () {broadcastreceiver = new Bro Adcastreceiver () {public void OnReceive (context context, Intent Intent) {String address = Intent.getstringextra (
        "Address");
      Locinfo.settext (address);
    }
    };
    Intentfilter intenttoreceivefilter = new Intentfilter ();
    Intenttoreceivefilter.addaction (LOCATION_BCR);
  Registerreceiver (Broadcastreceiver, Intenttoreceivefilter);
    } @Override protected void OnDestroy () {Super.ondestroy ();
  Unregisterreceiver (Broadcastreceiver); }
}

Four, androidmanifest.xml configuration information

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/"
    Android "package=" Com.jereh.baidulocation "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application android:name=" Com.jereh.baiduloca tion. MyApplication "android:allowbackup=" true "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name
      "Android:theme=" @style/apptheme "> <activity android:name=" com.jereh.baidulocation.MainActivity " Android:label= "@string/app_name" > <intent-filter> <action android:name= "android.intent.action . MAIN "/> <category android:name=" Android.intent.category.LAUNCHER/> </intent-filter> &l t;/activity> <service android:name= "COM.BAIDU.LOCATION.F" android:enabled= "true" Android:proce ss= ": Remote" > <intent-filter> <action android:name= "com.baidu.location.service_v2.2" > </action> </i ntent-filter> </service> </application> <uses-permission android:name= "Android.permission.ACCESS _coarse_location "> </uses-permission> <uses-permission android:name=" android.permission.ACCESS_FINE_ LOCATION "> </uses-permission> <uses-permission android:name=" Android.permission.ACCESS_WIFI_STATE " > </uses-permission> <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" > </ uses-permission> <uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE" > </ uses-permission> <uses-permission android:name= "Android.permission.READ_PHONE_STATE" > </ uses-permission> <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" > </ uses-permission> <uses-permission android:name= "Android.permission.INTERNET "/> <uses-permission android:name=" Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "> </ uses-permission> <uses-permission android:name= "Android.permission.READ_LOGS" > </uses-permission> & Lt;uses-permission android:name= "Android.permission.VIBRATE"/> <uses-permission android:name= " Android.permission.WAKE_LOCK "/> <uses-permission android:name= Android.permission.WRITE_SETTINGS"/> < /manifest>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.