When it comes to Baidu maps, this time should be controversial well known, many words do not pull, directly to the theme.
Access to the Baidu Map API must apply for key, there is no need to apply key here, the official documents on the above describes how to apply for key.
The Baidu map location used here is version 4.2, and the API is version 3.1.1. If you apply for key, then you can enter the coding phase, the specific implementation is as follows:
1. Introduction of Baidu Map related jar package and Armeabi directory into the Libs directory
2. Configure the relevant information in the project's Androidmanifest.xml file
Configure permissions
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name= "android.permission.ACCESS_COARSE_ Location "/> <uses-permission android:name=" Android.permission.ACCESS_FINE_LOCATION "/>< Uses-permission android:name= "Android.permission.GET_ACCOUNTS"/> <uses-permission android:name= " Android.permission.USE_CREDENTIALS "/> <uses-permission android:name=" Android.permission.MANAGE_ACCOUNTS "/ > <uses-permission android:name= "Android.permission.AUTHENTICATE_ACCOUNTS"/> <uses-permission android: Name= "Android.permission.INTERNET"/> <uses-permission android:name= "com.android.launcher.permission.READ_ SETTINGS "/> <uses-permission android:name=" Android.permission.CHANGE_WIFI_STATE "/> <uses-permission an Droid:name= "Android.permission.READ_PHONE_STATE"/> <uses-permission andRoid:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name= " Android.permission.BROADCAST_STICKY "/> <uses-permission android:name=" Android.permission.WRITE_SETTINGS "/ > <uses-permission android:name= "Android.permission.READ_PHONE_STATE"/> <uses-permission android:name= "Android.permission.READ_LOGS"/> <uses-permission android:name= "android.permission.MOUNT_UNMOUNT_ Filesystems "/>
The service tag is declared in the application tag, and each app has a separate service
<service android:name= "COM.BAIDU.LOCATION.F" android:enabled= "true" android:process= ": Remote"/>
Set Baidu key
<meta-data android:name= "Com.baidu.lbsapi.API_KEY" android:value= "ymsctrtxnokoznyxtvxama7o"/>
3. Configure the Layout file
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <com.baidu.mapapi.map.mapview android:id= "@+id/mv_map" android:layout _width= "Match_parent" android:layout_height= "match_parent" android:clickable= "true"/></ Linearlayout>
4. Code specific implementation
Package Com.example.map.activity;import Com.baidu.location.bdlocation;import Com.baidu.location.bdlocationlistener;import Com.baidu.location.locationclient;import Com.baidu.location.locationclientoption;import Com.baidu.location.locationclientoption.locationmode;import Com.baidu.mapapi.sdkinitializer;import Com.baidu.mapapi.map.baidumap;import Com.baidu.mapapi.map.bitmapdescriptor;import Com.baidu.mapapi.map.bitmapdescriptorfactory;import Com.baidu.mapapi.map.mapstatusupdatefactory;import Com.baidu.mapapi.map.mapview;import Com.baidu.mapapi.map.markeroptions;import Com.baidu.mapapi.map.mylocationconfiguration;import Com.baidu.mapapi.map.mylocationdata;import Com.baidu.mapapi.map.overlayoptions;import Com.baidu.mapapi.model.latlng;import Android.app.activity;import Android.os.bundle;public class MainActivity Extends activity{private locationclient locationclient;//locating client private Mylocationlistener locationlistener=new Mylocationlistener ();p rivate mapview mvmap;//Baidu Map control private Baidumap Baidumap;//Map Object Controller private static Double X_PI = 3.14159265358979324 * 3000.0/180.0; @Overrideprotected void OnCreate (Bundle s Avedinstancestate) {super.oncreate (savedinstancestate);//Must be sdkinitializer.initialize before Setcontentview ( Getapplicationcontext ()); Setcontentview (r.layout.main_activity); Initmap ();//Initialize}/** * Initialize map */private void InitMap ( {//Get map Control mvmap= (Mapview) Findviewbyid (R.ID.MV_MAP);//Get Map Object Controller baidumap=mvmap.getmap (); Baidumap.setbuildingsenabled (TRUE);//settings Display floor contour baidumap.setmapstatus (Mapstatusupdatefactory.zoomto (19f));//Set map status/ /Create location Client Locationclient=new locationclient (Getapplicationcontext ());// Register Listener Locationclient.registerlocationlistener (Locationlistener);//Instantiate client location parameters locationclientoption Options=new Locationclientoption (); Options.setlocationmode (locationmode.hight_accuracy);//Set positioning mode Options.setcoortype ("gcj02 ");//Returns the positioning result Options.setscanspan (6000);//Set the location scan interval time options.setisneedaddress (true);//Set whether the location result contains address information, The default is no Options.setneeddevicedirect (true);//When setting up a network location, the device direction locationclient.setlocoption is required (options);//Set positioning Parameters Locationclient.start ();//start Positioning}/** * Implement Baidu Positioning Listener * @author Arvin_li * */public class Mylocationlistener Implements bdlocationlistener{@Overridepublic void Onreceivelocation (bdlocation location) {if (location = = NULL) return ;//create geographic coordinates point object latlng point=new latlng (Location.getlatitude (), Location.getlongitude ());//convert national geodetic coordinates to Baidu coordinates point= Gcjtobaidu (point);//Open Location layer baidumap.setmylocationenabled (TRUE);//construct positioning data Mylocationdata locdata=new Mylocationdata.builder (). Accuracy (Location.getradius ())//Set the location accuracy information. Direction (location.getdirection ())// Set the direction information to get to, clockwise 0-360.latitude (point.latitude)//Set the latitude of the location data. Longitude (point.longitude)//Set the longitude of the location data. build ();/ Build the Build Location data object//Set location data Baidumap.setmylocationdata (locdata);//Build Location icon Constructionlocationicon (point);}} /** * Build Positioning icon * @param point icon location */private void Constructionlocationicon (latlng points) {//Create marker icon Bitmapdescriptor DESCR Iptor=bitmapdescriptorfactory.fromresource (R.DRAWABLE.XK);//build markeroption for adding markeroverlayoptions options on the map =new markeroptions (). Icon(descriptor)//Set icon. Position (point)//Set icon location. Draggable (TRUE)//Allow drag-and-drop. ZIndex (5);//Set icon Level//empty map all Overlay cover and Infowindowbaidumap.clear ();//Add icon Baidumap.addoverlay (options) on the map;//Set positioning layer configuration information, Setting the anchor layer configuration information will take effect only if the layer is first allowed to be positioned baidumap.setmylocationconfigeration (new Mylocationconfiguration ( Com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.FOLLOWING, True, descriptor));// Close the positioning layer baidumap.setmylocationenabled (false);} @Overrideprotected void OnPause () {super.onpause (); Mvmap.onpause ();} @Overrideprotected void Onresume () {super.onresume (); Mvmap.onresume ();} @Overrideprotected void OnDestroy () {Super.ondestroy (); if (null!=locationclient&&locationclient.isstarted ( ) {locationclient.stop ();//Stop positioning}locationclient=null;mvmap.ondestroy ();//Destroy Map}/** * National Survey Bureau conversion Baidu latitude and longitude * @param point * @ return */public static latlng Gcjtobaidu (latlng point) {//Double x = Gg_lon, y = gg_lat;double x = point.longitude, y = P Oint.latitude;double z = math.sqrt (x * x + y * y) + 0.00002 * Math.sin (y * x_pi);d ouble theta = math.atan2(y, x) + 0.000003 * MATH.COS (x * x_pi);d ouble Bd_lon = z * Math.Cos (theta) + 0.0065;double Bd_lat = z * Math.sin (theta) + 0.006; LATLNG point1 = new latlng (Bd_lat,bd_lon); return point1;}}
The return positional parameter has three values:
1.GCJ02 return to national geodetic coordinates by latitude coordinate system
2.bd09 back to Baidu Mercator coordinate system
3.BD09LL return Baidu latitude and longitude coordinates
Default is GCJ02
There are three ways to set the positioning mode:
1.battery_saving Low Power mode
2.device_sensors only device GPS mode
3.hight_accuracy High Accuracy Mode
Option.setlocationmode (locationmode.hight_accuracy)
Set the positioning layer configuration information to be effective only if you allow the location layer to be set after the position layer configuration information is first allowed
The positioning layer is displayed in 3 ways:
1.COMPASS Compass State, display orientation circle, keep positioning icon in center of map
2.NORMAL Normal: Do not take any action on the map when updating the location data
3.FOLLOWING follower, keep the positioning icon in the center of the map
Baidumap.setmylocationconfigeration (New Mylocationconfiguration ( Com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.FOLLOWING, True, descriptor));
The function to locate here is the end.
Android Baidu Map Location