Baidu Map development positioning and display demo (Android)

Source: Internet
Author: User

Baidu Map provides us with a very rich API for us to develop two times. The SDK and positioning SDK for Baidu maps were updated this June. The map is updated to 3.0 and the location is updated to 4.2. Baidu said: This update on the interface has a large part of the adjustment, incompatible with the previous version. This blog post provides a small example based on the latest version of the API above: gets its own current latitude and longitude coordinates and is displayed on the map. Here only the core code, the introduction of other keys, the addition of the map control, the declaration of Rights, the location of the service statement can be in the Baidu Map API website (http://developer.baidu.com/map/ sdkandev-download.htm) for a full example download link will be attached later.


The comments in the code have been more detailed, here do not explain, the reference website is easy to understand.

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;//location Related declaration public locationclient locationclient = null;//Custom diagram Mark Bitmapdescriptor Mcurrentmarker = Null;boolean Isfirstloc = true;//is first positioned public bdlocationlistener MyListener = new BDLo Cationlistener () {@Overridepublic void onreceivelocation (bdlocation location) {//Map view is not processed after the new receive is destroyed if (location = = NULL | | mapview = = 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 (); Baidumap.setmylocationdata (locdata);//Set positioning data if (isfirstloc) {isfirstloc = false; LATLNG ll = new Latlng (Location.getlatitude (), Location.getlongitude ()); Mapstatusupdate U = mapstatusupdatefactory.newlatlngzoom (ll, 16);//Set Map center point and zoom level//mapstatusupdate U = MAPSTATUSUPDATEFACTORY.NEWLATLNG (ll); Baidumap.animatemapstatus (U);}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);// Before using the SDK components to initialize the context information, pass in applicationcontext//Note that the method is implemented before Setcontentview the method Sdkinitializer.initialize ( Getapplicationcontext ()); Setcontentview (r.layout.main_activity); Mapview = (Mapview) This.findviewbyid ( R.id.mapview); Get map Control Reference Baidumap = Mapview.getmap ();//Turn on location layer baidumap.setmylocationenabled (true); locationclient = new LocationcLient (Getapplicationcontext ()); Instantiation of the Locationclient class Locationclient.registerlocationlistener (MyListener); Register the Listener function this.setlocationoption ();//Set positioning Parameters Locationclient.start (); Start positioning//Baidumap.setmaptype (Baidumap.map_type_normal); Set to General map//Baidumap.setmaptype (Baidumap.map_type_satellite); Set to satellite map//baidumap.settrafficenabled (TRUE); Turn on traffic map}//three states to implement map lifecycle management @overrideprotected void OnDestroy () {//Destroy location Locationclient.stop on exit (); 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 positional parameters */private void Setlocationoption () {locationclientoption option = new Locationclientoption (); O Ption.setopengps (TRUE); Open Gpsoption.setlocationmode (LocationClientOption.LocationMode.Hight_Accuracy);//Set positioning mode Option.setcoOrtype ("Bd09ll"); The returned positioning result is Baidu latitude and longitude, the default value of Gcj02option.setscanspan (5000); Set the time interval for initiating a location request to 5000msoption.setisneedaddress (true); The returned positioning result contains address information option.setneeddevicedirect (TRUE); The returned positioning result contains the direction of the phone head locationclient.setlocoption (option);}}

Full Source: http://download.csdn.net/detail/u012909091/7601647


(This address: http://blog.csdn.net/zhshulin/article/details/37495575)









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.