How Android uses Baidu Map

Source: Internet
Author: User
Tags imap

After learning Android, I decided to use a small application. What should we do? I used to see a good map made by people using iOS in my company. I just want to make an Android version.

Baidu's API is very simple. It took half a day to solve the problem. First create a project, and then set the baidumapapi. copy the JAR file to the Lib folder under the project, create a folder armeabi in it, copy the libbmapapiengine_v1_3_3.so dynamic library file, open eclipse, right click Properties of the project, go to Java build path, open libraries, click Add external jars, and set baidumapapi. jar is added, OK, and configured.

Custom an aplication class, wheremapapplication, inherited from application (copy from official Baidu ):

Public class wheremapapplication extends application {static wheremapapplication mdemoapp; // Baidu mapapi management class bmapmanager mbmapman = NULL; // Authorization key // todo: enter your key, // application address: http://dev.baidu.com/wiki/static/imap/key/String mstrkey = "applied key value"; Boolean m_bkeyright = true; // The Authorization key is correct, verification passed // common event listening, used to handle common network errors, authorization verification errors and other static class mygenerallistener implements mkgenerallistener {@ overridepublic void ongetnetworks Tate (INT ierror) {log. D ("mygenerallistener", "ongetnetworkstate error is" + ierror); toast. maketext (wheremapapplication. mdemoapp. getapplicationcontext (), "your network has an error! ", Toast. length_long ). show () ;}@ overridepublic void ongetpermissionstate (INT ierror) {log. D ("mygenerallistener", "ongetpermissionstate error is" + ierror); If (ierror = mkevent. error_permission_denied) {// Authorization key error: Toast. maketext (wheremapapplication. mdemoapp. getapplicationcontext (), "See bmapapidemoapp. enter the correct authorization key for the Java file! ", Toast. length_long ). show (); wheremapapplication. mdemoapp. m_bkeyright = false ;}}@ override public void oncreate () {log. V ("bmapapidemoapp", "oncreate"); mdemoapp = This; mbmapman = new bmapmanager (this); mbmapman. init (this. mstrkey, new mygenerallistener (); mbmapman. getlocationmanager (). setpolicyinternal (10, 5); // If (mbmapman! = NULL) {// mbmapman. destroy (); // mbmapman = NULL; //} super. oncreate () ;}@ override // we recommend that you call the destroy () function of mapadpi before exiting your app to avoid time consumption caused by repeated initialization. Public void onterminate () {// todo auto-generated method stubif (mbmapman! = NULL) {mbmapman. Destroy (); mbmapman = NULL;} super. onterminate ();}}

Baidu API address: http://dev.baidu.com/wiki/static/index.htm

Baidu application key address: http://dev.baidu.com/wiki/static/imap/key/
Register a Baidu user, apply for a key value, and replace the key value with the "applied key value" in the above Code ".

Modify the attributes of the application in the androidmanifest. xml file and add Android: Name = ". wheremapapplication" as follows:

<application        android:name=".WhereMapApplication"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >

And add it before. If it is added at the end, a warning may be generated:

<uses-sdk        android:minSdkVersion="3" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >    </uses-permission>    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >    </uses-permission>    <uses-permission android:name="android.permission.INTERNET" >    </uses-permission>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >    </uses-permission>    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >    </uses-permission>    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >    </uses-permission>    <uses-permission android:name="android.permission.READ_PHONE_STATE" >    </uses-permission>    <supports-screens        android:anyDensity="true"        android:largeScreens="true"        android:normalScreens="true"        android:resizeable="true"        android:smallScreens="true" />

If you want to use it in the activity, you need to create a new custom activity that inherits from mapactivity. I created a mainactivity, as shown below:

Public class mainactivity extends mapactivity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); wheremapapplication APP = (wheremapapplication) This. getapplication (); If (App. mbmapman = NULL) {app. mbmapman = new bmapmanager (getapplication (); app. mbmapman. init (App. mstrkey, new wheremapapplication. mygenerallistener ();} App. mbmapman. start (); // if you use the map SDK, initialize the map activity // long itime = system. nanotime (); super. initmapactivity (App. mbmapman); // itime = system. nanotime ()-itime; // log. D ("mapviewdemo", "The init time is" + itime); mapview = (mapview) findviewbyid (R. id. bmapview); mapview. setbuiltinzoomcontrols (true) ;}@ override public Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. Activity_main, menu); Return true;} @ overrideprotected Boolean isroutedisplayed () {// todo auto-generated method stubreturn false;} @ synchronized void onpause () {wheremapapplication APP = (wheremapapplication) this. getapplication (); If (App. mbmapman! = NULL) app. mbmapman. stop (); super. onpause () ;}@ overrideprotected void onresume () {wheremapapplication APP = (wheremapapplication) This. getapplication (); app. mbmapman. start (); super. onresume ();}}

After bmapmanager is new in application, after init initialization, start and stop are used in the desired activity to control the display and closure of MAP:

Public class mainactivity extends mapactivity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); wheremapapplication APP = (wheremapapplication) This. getapplication (); If (App. mbmapman = NULL) {app. mbmapman = new bmapmanager (getapplication (); app. mbmapman. init (App. mstrkey, new wheremapapplication. mygenerallistener ();} App. mbmapman. start (); // if you use the map SDK, initialize the map activity // long itime = system. nanotime (); super. initmapactivity (App. mbmapman); // itime = system. nanotime ()-itime; // log. D ("mapviewdemo", "The init time is" + itime); mapview = (mapview) findviewbyid (R. id. bmapview); mapview. setbuiltinzoomcontrols (true) ;}@ override public Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. Activity_main, menu); Return true;} @ overrideprotected Boolean isroutedisplayed () {// todo auto-generated method stubreturn false;} @ synchronized void onpause () {wheremapapplication APP = (wheremapapplication) this. getapplication (); If (App. mbmapman! = NULL) app. mbmapman. stop (); super. onpause () ;}@ overrideprotected void onresume () {wheremapapplication APP = (wheremapapplication) This. getapplication (); app. mbmapman. start (); super. onresume ();}}

 

At this time, the map can be displayed, but it was tested on the simulator. If there is no real machine, it can only be written here first. The rest of the functions, such as logging and searching, are described in detail in the official code and will not be described in detail.

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.