"Positioning" of Baidu Map development

Source: Internet
Author: User

In the previous several times we have completed the data retrieval and line planning functions, but if we do not know their location, retrieval is not a very troublesome?

Knowing your location allows you to search and route according to your location.


Location Services

The location service in the read map is separate from the basic map service, so you need to introduce another so file box jar package, and if you choose a composite package, just use the so file as OK.


First, add the following service to the manifest file

<Service Android:name="COM.BAIDU.LOCATION.F"Android:Enabled="true"Android:Process=": Remote"></Service>

Then it's the permissions needed to join the locator.

Notice that you remove the same permissions as the map permissions

<!--This permission is used for network positioning -<Uses-Permission Android:Name="Android.permission.ACCESS_COARSE_LOCATION"></Uses-Permission><!--This permission is used to access GPS location -<Uses-Permission Android:Name="Android.permission.ACCESS_FINE_LOCATION"></Uses-Permission><!--For accessing WiFi network information, WiFi information will be used for network location -<Uses-Permission Android:Name="Android.permission.ACCESS_WIFI_STATE"></Uses-Permission><!--Get carrier information to support the interface that provides carrier-related information -<Uses-Permission Android:Name="Android.permission.ACCESS_NETWORK_STATE"></Uses-Permission><!--This permission is used to get the WiFi access, WiFi information will be used for network location -<Uses-Permission Android:Name="Android.permission.CHANGE_WIFI_STATE"></Uses-Permission><!--Used to read the current state of the phone -<Uses-Permission Android:Name="Android.permission.READ_PHONE_STATE"></Uses-Permission><!--Write extended storage, write data to the expansion card for writing offline location data -<Uses-Permission Android:Name="Android.permission.WRITE_EXTERNAL_STORAGE"></Uses-Permission><!--Access network, network location requires Internet access -<Uses-Permission Android:Name="Android.permission.INTERNET" /><!--SD card Read access, user write offline location data -<Uses-Permission Android:Name="Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></Uses-Permission><!--Allow the app to read low-level system log files -<Uses-Permission Android:Name="Android.permission.READ_LOGS"></Uses-Permission>


Still a key file, map and location using the same key file

Configure under the application node of the manifest file

<Meta-data            android:name="Com.baidu.lbsapi.API_KEY"             android:value="key"/>  


Knowledge related to maps, not to repeat


  Gets the map control reference    Mmapview = (mapview) Findviewbyid (R.id.bmapview);    Map = Mmapview.getmap ();    Set the position to open position    map.setmylocationenabled (true);    Set the anchor icon, you can use the system can also be customized    Mcurrentmarker = Bitmapdescriptorfactory.fromresource (R.drawable.icon_geo);    If you use the system, let Mcurrentmarker be empty    map.setmylocationconfigeration (new Mylocationconfiguration (locationmode.normal , true, Mcurrentmarker));

Basic positioning operations

Client = new Locationclient (Getapplicationcontext ()); DECLARE locationclient class    listener = Mylisetener;    Registered monitor    Client.registerlocationlistener (listener);    locationclientoption option = new Locationclientoption ();    Option.setcoortype ("Bd09ll");//Returns the positioning result is Baidu latitude and longitude, the default value gcj02    Option.setscanspan (5000);//Set the time interval for initiating a location request to 5000ms    Option.setopengps (TRUE);//Turn    on GPS//Option.setisneedaddress (TRUE);//Return positioning result contains address information    // Option.setneeddevicedirect (TRUE);//The positioning result returned contains the direction of the phone head    client.setlocoption (option);


Specific implementation code


Package Com.flyou.hellomap;import Android.app.activity;import Android.os.bundle;import Com.baidu.location.bdlocation;import Com.baidu.location.bdlocationlistener;import Com.baidu.location.locationclient;import Com.baidu.location.locationclientoption;import Com.baidu.mapapi.sdkinitializer;import Com.baidu.mapapi.map.baidumap;import com.baidu.mapapi.map.BaiduMapOptions ; Import Com.baidu.mapapi.map.bitmapdescriptor;import Com.baidu.mapapi.map.bitmapdescriptorfactory;import Com.baidu.mapapi.map.mapstatus;import Com.baidu.mapapi.map.mapstatus.builder;import Com.baidu.mapapi.map.mapstatusupdate;import Com.baidu.mapapi.map.mapstatusupdatefactory;import Com.baidu.mapapi.map.mapview;import Com.baidu.mapapi.map.mylocationconfiguration;import Com.baidu.mapapi.map.mylocationconfiguration.locationmode;import Com.baidu.mapapi.map.mylocationdata;import Com.baidu.mapapi.model.latlng;import Com.baidu.platform.comapi.map.s;public class Locationmapactivity extends Activity {Private Locationclient client;  Private Bdlocationlistener listener;  Mapview Mmapview = null;  Bitmapdescriptor Mcurrentmarker;  Private Boolean isfirstloc = true;    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Initialize the context information before using each component of the SDK, passing in ApplicationContext//Note that the method is implemented Sdkinitializer.initialize before the method is Setcontentview (    Getapplicationcontext ());    Setcontentview (R.layout.activity_main);    Gets the map control reference Mmapview = (Mapview) Findviewbyid (R.id.bmapview);    Map = Mmapview.getmap ();    Set the position to open position map.setmylocationenabled (true);    Set the anchor icon, you can use the system can also be customized Mcurrentmarker = Bitmapdescriptorfactory.fromresource (R.drawable.icon_geo); Map.setmylocationconfigeration (New Mylocationconfiguration (Locationmode.normal, True) if you use the system to leave mcurrentmarker empty    , Mcurrentmarker));  Location (); } private void location () {client = new Locationclient (Getapplicationcontext ());//declaration locationclient Class listener = M    Ylisetener; Register Monitor Client.registerloCationlistener (listener);    locationclientoption option = new Locationclientoption ();    Option.setcoortype ("Bd09ll");//Returns the positioning result is Baidu latitude and longitude, the default value gcj02 Option.setscanspan (5000);//Set the time interval for initiating a location request to 5000ms Option.setopengps (TRUE);//Turn on GPS//Option.setisneedaddress (TRUE);//Returns the location result contains address information//Option.setneeddevicedirect (tru  e);//return positioning results include the direction of the phone head client.setlocoption (option);    } @Override protected void OnDestroy () {Super.ondestroy ();    Execute Mmapview.ondestroy () when activity executes OnDestroy, implement map life cycle management Mmapview.ondestroy ();  Map.setmylocationenabled (FALSE);    } @Override protected void Onresume () {super.onresume ();    Location (); Executes Mmapview when the activity executes Onresume.    Onresume () To achieve Map Lifecycle management mmapview.onresume ();  Client.start ();    } @Override protected void OnPause () {super.onpause (); Executes Mmapview when the activity executes OnPause.    OnPause () To achieve Map Lifecycle management mmapview.onpause ();  Client.stop (); } bdlocationlistener Mylisetener = new Bdlocationlistener () {@Override publicvoid Onreceivelocation (bdlocation location) {//Map view is not processed after the newly received position is not handled if (site = = NULL | | mmapview = = NULL      ) return;          Mylocationdata locdata = new Mylocationdata.builder (). Accuracy (Location.getradius ())//Here Set the direction information the developer gets, clockwise 0-360      . direction. Latitude (Location.getlatitude ()). Longitude (Location.getlongitude ()). build ();      Map.setmylocationdata (Locdata);        if (isfirstloc) {isfirstloc = false;        LATLNG ll = new Latlng (Location.getlatitude (), Location.getlongitude ());        The range of scaling is 3-20 mapstatusupdate u = mapstatusupdatefactory.newlatlngzoom (ll, 17f);      Map.animatemapstatus (U);  }    }  }; Private Baidumap Map;}






Achieve the positioning, and then achieve the previous data retrieval, travel planning will be very simple?

Ha ha......

Start playing the map ...

"Positioning" of Baidu Map development

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.