Baidu Android map Learning (1)

Source: Internet
Author: User

After reading this article, users who like android can go to http://blog.csdn.net/woshishuoshuoa/article/details/9858.pdf.

Google map is very open, but unfortunately, there are too few materials worth referencing. After Google's revision, the requirements for mobile phone versions have also changed, and some interfaces have high requirements for mobile phone versions, it is even more worth mentioning that some interfaces will be exhausted... However, Google Maps are indeed very good, so it is easy to learn Baidu maps after simply learning Google, baidu map has learned many things from Google. However, due to its low development nature, many things on Baidu map are dead and cannot be modified by others, next, let's talk about the hundred-degree chart ~~~

When learning Baidu map, let's take a look at the official Baidu map API website http://developer.baidu.com/?, however, some of the things provided by the official website still need to be integrated with your own brains. In this case, cainiao will post some code for you to see, which is a very simple column ~~~

Http://developer.baidu.com/map/sdkandev-2.htmdetailed explanation of how to obtain a world map ~~~

After obtaining the world map, how can I locate my current location? You only need to modify the code in mainactivity on the basis of obtaining the world map:

Import COM. baidu. location. bdlocation; import COM. baidu. location. bdlocationlistener; import COM. baidu. location. locationclient; import COM. baidu. location. locationclientoption; import COM. baidu. mapapi. bmapmanager; import COM. baidu. mapapi. map. locationdata; import COM. baidu. mapapi. map. mapcontroller; import COM. baidu. mapapi. map. mapview; import COM. baidu. mapapi. map. mylocationoverlay; import COM. baidu. platform. coma Pi. basestruct. geopoint; import android. OS. bundle; import android. util. log; import android. app. activity; public class mainactivity extends activity {bmapmanager mbmapman = NULL; mapview mmapview = NULL; Public locationclient mlocationclient = NULL; Public bdlocationlistener mylistener = new mylocationlistener (); @ overrideprotected void oncreate (bundle savedinstancestate) {// ============================================== ========================== Initialize the map activity, use key ========================== ======================================= super. oncreate (savedinstancestate); mbmapman = new bmapmanager (getapplication (); mbmapman. init ("4b605b48b3062e4687d0ada2ecd65a8e", null); // Note: Initialize the bmapmanager object before using setcontentview. Otherwise, the setcontentview (R. layout. activity_main); mmapview = (mapview) findviewbyid (R. id. bmapsview); mmapview. setbuiltinzoomcontrols (true); // set to enable built-in Scaling Control mapcontroller mmapcontroller = mmapview. getcontroller (); // get control of mmapview, which can be used to control and drive translation and scaling geopoint point = new geopoint (INT) (39.915*1e6), (INT) (116.404*1e6); // construct a geopoint with the given longitude and latitude, in the unit of (degree * 1e6) mmapcontroller. setcenter (point); // set the map center point mmapcontroller. setzoom (12 ); // set the map zoom level. ================================================================ ================================================ MLO Cationclient = new locationclient (getapplicationcontext (); // declare the locationclient class mlocationclient. registerlocationlistener (mylistener); // register the listening function locationclientoption option = new locationclientoption (); option. setopengps (true); option. setaddrtype ("all"); // The returned positioning result contains the address information option. setcoortype ("bd0911"); // The return result is Baidu longitude and latitude. The default value is gcj02option. setscanspan (2000); option. disablecache (true); // disable cache locating option. setpoidistance (1000 ); // Poi query distance option. setpoiextrainfo (true); // whether the poi phone number, address, and other details are required. mlocationclient. setlocoption (option); mlocationclient. start (); // start locationclient !!! Mmapview. getcontroller (). setzoom (15); If (mlocationclient! = NULL & mlocationclient. isstarted () {mlocationclient. requestlocation ();} else {log. D ("locsdk3", "location is null or not started"); If (mlocationclient! = NULL & mlocationclient. isstarted () {mlocationclient. requestpoi () ;}} class mylocationlistener implements bdlocationlistener {@ overridepublic void onreceivelocation (bdlocation location) {// todo auto-generated method stubgeopoint geopoint = new geopoint (INT) (location. getlatitude () * 1e6), (INT) (location. getlongationoverlay () * 1e6);/* mylocationoverlay is only responsible for displaying my location, * storing the acquired location data in a locationdata structure and setting mylcationoverl with this structure For the ay data source, * mylocationoverlay */mylocationoverlay = new mylocationoverlay (mmapview); locationdata = new locationdata (); locationdata. latitude = location. getlatitude (); locationdata. longpolling = location. getlongpolling (); locationdata. direction = 2.0f; mylocationoverlay. setdata (locationdata); mmapview. getoverlays (). clear (); mmapview. getoverlays (). add (mylocationoverlay); mmapview. refresh (); // Call mapview after updating the map cover. refresh () makes the update take effect in mmapview. getcontroller (). animateto (geopoint) ;}@ overridepublic void onreceivepoi (bdlocation arg0) {// todo auto-generated method stub }/// ============================ ==================================== rewrite the following method, manage APIs =======@ override protected void ondestroy () {mmapview. destroy (); If (mbmapman! = NULL) {mbmapman. Destroy (); mbmapman = NULL;} super. ondestroy () ;}@ override protected void onpause () {mmapview. onpause (); If (mbmapman! = NULL) {mbmapman. Stop () ;}super. onpause () ;}@ override protected void onresume () {mmapview. onresume (); If (mbmapman! = NULL) {mbmapman. Start () ;}super. onresume ();}}

Note: Do not forget to add service in mainfest

<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"></service>

:

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.