Android's Baidu targeting API uses

Source: Internet
Author: User

API version:

Baidu positioning api:v5.0 (http://developer.baidu.com/map/wiki/index.php?title=android-locsdk/guide/v5-0)

Baidu geocoding api:v2.0 (http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding#7. E9.80.86.E5.9C.B0.E7.90.86.E7.BC.96.E7.A0.81.E6.9C.8D.E5.8A.A1)

How to use:

1) Apply for key (Http://lbsyun.baidu.com/apiconsole/key?application=key)

Create a new app (note that the app category is the Android SDK), and after the creation is complete, you can get a value for access to the app (AK).

2) Set Androidmanifest.xml

Declare service components in the Application tab, each with its own individual location service

<android:name= "COM.BAIDU.LOCATION.F"  android:enabled= "true"  android:process= ": Remote"></service>

Declaring usage rights

<!--This permission is used for network positioning -<uses-permissionAndroid:name= "Android.permission.ACCESS_COARSE_LOCATION"></uses-permission><!--This permission is used to access GPS location -<uses-permissionAndroid:name= "Android.permission.ACCESS_FINE_LOCATION"></uses-permission><!--for accessing WiFi network information, WiFi information will be used for network location -<uses-permissionAndroid:name= "Android.permission.ACCESS_WIFI_STATE"></uses-permission><!--get carrier information to support the interface that provides carrier-related information -<uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE"></uses-permission><!--This permission is used to get the WiFi access, WiFi information will be used for network location -<uses-permissionAndroid:name= "Android.permission.CHANGE_WIFI_STATE"></uses-permission><!--used to read the current state of the phone -<uses-permissionAndroid:name= "Android.permission.READ_PHONE_STATE"></uses-permission><!--Write extended storage, write data to the expansion card for writing offline location data -<uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><!--access network, network location requires Internet access -<uses-permissionAndroid:name= "Android.permission.INTERNET" /><!--SD card Read access, user write offline location data -<uses-permissionAndroid:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission><!--allow the app to read low-level system log files -<uses-permissionAndroid:name= "Android.permission.READ_LOGS"></uses-permission>

In the application label settings Acesskey

< Meta-data             Android:name = "Com.baidu.lbsapi.API_KEY"             android:value= " previously requested Access application (AK)"/>

3) Use the API in activity or fragment (no complete code listed, only the key implementation section, here for example in fragment)

Declaring member variables

 Public locationclient mlocclient;  Public Mylocationlistener Mmylocationlistener;

Initializing member variables

  @Override  public  void   OnCreate (Bundle arg0) { // Span style= "color: #008000;" > TODO auto-generated Method stub  super  .            OnCreate (arg0); Mlocclient  = new   Locationclient (    Getactivity ()); Mmylocationlistener  = new              Mylocationlistener ();      //  Baidu positioning initialize   //  get location information   

Implement callback snooping

    /*** Implement real-bit callback monitoring*/     Public classMylocationlistenerImplementsBdlocationlistener {@Override Public voidonreceivelocation (bdlocation location) {//coordinate information conversion into specific location information (JSON format, need to parse)list<namevaluepair> list =NewArraylist<namevaluepair>(); Basicnamevaluepair Akvaluepair=NewBasicnamevaluepair ("AK", "here, fill in your AK."); Basicnamevaluepair Mcodevaluepair=NewBasicnamevaluepair ("Mcode", "here, fill in your mcode."); Basicnamevaluepair Locationvaluepair=NewBasicnamevaluepair ("Location", "32.214283,118.893443"); Basicnamevaluepair Outputvaluepair=NewBasicnamevaluepair ("Output", "JSON"); Basicnamevaluepair Poisvaluepair=NewBasicnamevaluepair ("POIs", "0");            List.add (Akvaluepair);            List.add (Mcodevaluepair);            List.add (Locationvaluepair);            List.add (Outputvaluepair);            List.add (Poisvaluepair);                        Geodecoder (list); //Output to log            if(D) log.i (TAG, sb.tostring ()); //End location information Get            if(Mlocclient! =NULL) {Mlocclient.unregisterlocationlistener (Mmylocationlistener);            Mlocclient.stop (); }        }    }  

Call Baidu Geocoding API V2.0 in the thread to resolve the location information corresponding to the address information, and put into the handler for processing

//coordinate information into specific location information (put in the thread to complete, does not affect the main thread)    Private voidGeodecoder (FinalList<namevaluepair>list) {        NewThread () { Public voidrun () {String URL= "http://api.map.baidu.com/geocoder/v2/"; String result=httputil.httppostclient (URL, list); Message msg=NewMessage (); Msg.obj=result;            Mhandler.sendmessage (msg);        };    }.start (); }        //mainline thread processing other thread result data    PrivateHandler Mhandler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {Try{geocodersearchresponse Geocodersearchresponse=NewGson (). Fromjson (Msg.obj.toString (), Geocodersearchresponse.class);//output to control                if(GeocoderSearchResponse.status.equals ("0") ) {text_index_location=(TextView) getactivity (). Findviewbyid (r.id.text_index_location); Text_index_location.settext (geocoderSearchResponse.result.addressComponent.city+geocoderSearchResponse.result.addressComponent.district); }Else{text_index_location=(TextView) getactivity (). Findviewbyid (r.id.text_index_location); Text_index_location.settext ("GPS Location acquisition failed!" Errcode: "+geocodersearchresponse.status); }            } Catch(Exception e) {//Todo:handle Exception            }        }; };

Note

1) The use of Baidu geocoding API V2.0 for httpclient POST request data, need to add a Mcode parameter, shaped like

"Xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx;com.xxx. YYY "(IS

Security code to be filled in when applying for AK)

2) When the JSON parsing of the address information, the internal class parameter composition and the document content is not very consistent (perhaps Baidu often changes the content of the class), so need

Organize classes according to the JSON format content that is actually returned

Android's Baidu targeting API uses

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.