Baidu map Development Positioning and display Demo (Android)
Baidu map provides us with a wealth of APIS for our secondary development. The Baidu map SDK and positioning SDK were updated in May June this year. The map is updated to 3.0, and the positioning is updated to 4.2. Baidu said: this update has made a majority of changes to the interface, which is incompatible with previous versions. This blog post provides a small example based on the latest API: Get your current longitude and latitude coordinates and display them on the map. Here only give the core code, the introduction of other keys, map controls to add, permission declaration, positioning service declaration can be in Baidu map API official website (http://developer.baidu.com/map/sdkandev-download.htm) for viewing, the download link for the complete example will be attached later.
The comments in the Code have been described in detail and are not described here. It is easy to understand by referring to the official website.
Package org. zsl. android. map; import android. app. activity; import android. OS. bundle; import android. util. log; 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. bitmapDescriptor; import com. baidu. mapapi. map. mapStatusUpdate; import com. baidu. mapapi. map. mapStatusUpdateFactory; import com. baidu. mapapi. map. mapView; import com. baidu. mapapi. map. myLocationData; import com. baidu. mapapi. model. latLng; public class MainActivity extends Activity {public MapView mapView = null; public BaiduMap baiduMap = null; // positioning Declaration public LocationClient locationClient = null; // custom icon BitmapDescriptor mCurrentMarker = null; boolean isFirstLoc = true; // whether to locate public BDLocationListener myListener = new BDLocationListener () {@ Overridepublic void onReceiveLocation (BDLocation location) {// After map view is destroyed, if (location = null | mapView = null) return; MyLocationData locData = new MyLocationData. builder (). accuracy (location. getRadius () // set the direction information obtained by the developer, clockwise 0-100 ction ). latitude (location. getLatitude ()). longpolling (location. getlongpolling ()). build (); baiduMap. setMyLocationData (locData); // you can specify if (isFirstLoc) {isFirstLoc = false; LatLng ll = new LatLng (location. getLatitude (), location. getlongpolling (); MapStatusUpdate u = MapStatusUpdateFactory. newLatLngZoom (ll, 16); // sets the map center and zoom level. // MapStatusUpdate u = MapStatusUpdateFactory. newLatLng (ll); baiduMap. animateMapStatus (u) ;}};@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // initialize the context information before using the SDK components, and pass in the ApplicationContext // note that this method must be implemented before the setContentView method. initialize (getApplicationContext (); setContentView (R. layout. main_activity); mapView = (MapView) this. findViewById (R. id. mapView); // obtain the map control reference baiduMap = mapView. getMap (); // enable the position layer baiduMap. setMyLocationEnabled (true); locationClient = new LocationClient (getApplicationContext (); // instantiate the LocationClient class locationClient. registerLocationListener (myListener); // registers the listener function this. setLocationOption (); // you can specify the locationClient parameter. start (); // start locating // baiduMap. setMapType (BaiduMap. MAP_TYPE_NORMAL); // set it to a general map // baiduMap. setMapType (BaiduMap. MAP_TYPE_SATELLITE); // set it to a satellite map // baiduMap. setTrafficEnabled (true); // enable traffic map} // three States implement map lifecycle management @ Overrideprotected void onDestroy () {// destroy locationClient when exiting. stop (); baiduMap. setMyLocationEnabled (false); // TODO Auto-generated method stubsuper. onDestroy (); mapView. onDestroy (); mapView = null ;}@ Overrideprotected void onResume () {// TODO Auto-generated method stubsuper. onResume (); mapView. onResume () ;}@ Overrideprotected void onPause () {// TODO Auto-generated method stubsuper. onPause (); mapView. onPause ();}/*** set positioning parameter */private void setLocationOption () {LocationClientOption option = new LocationClientOption (); option. setOpenGps (true); // enable GPSoption. setLocationMode (LocationClientOption. locationMode. hight_Accuracy); // sets the positioning mode option. setCoorType ("bd09ll"); // The return result is Baidu longitude and latitude. The default value is gcj02option. setScanSpan (5000); // you can specify a time interval of msoption to initiate a request. setIsNeedAddress (true); // The returned positioning result contains the address information option. setNeedDeviceDirect (true); // The returned positioning result contains the direction of the mobile phone head locationClient. setLocOption (option );}}
Complete source code: http://download.csdn.net/detail/u012909091/7601647
(Address: http://blog.csdn.net/zhshulin/article/details/37495575)