Using Baidu API to achieve hot (WIFI), GPS, base station positioning

Source: Internet
Author: User

Directly in the code:

。 Rattle

/** * Baidu Base Station location error return code *///61:GPS location Results//62: Scan integration based on targeted failure. The positioning result at this point is invalid. 63: Network exception, the request was not successfully initiated to the server. The positioning result is not valid at this time.

65: The result of locating the cache. 66: Offline location results. The corresponding return result when called by Requestofflinelocaiton//67: Offline location failed. The corresponding return result when called by Requestofflinelocaiton//68: When the network connection fails. Find local offline location when corresponding return results///161: Indicates network location result//162~167: Server location failed//502:key parameter error//505:key not present or illegal//601:key service disabled by developer//602:key Mcode does not match, meaning that there is a problem with the security Code Setup during your AK configuration. Please make sure: SHA1 is correct, ";" The semicolon is the English state. And the package name is the package name//501-700:key validation failure of your currently executing app public class Baiduactivity extends Activity implements Onclicklistener {private static Final String TAG = "baiduactivity";p rivate TextView mtext;private TextView mtextpoi;private locationclient Mlocationclient = null;private Bdlocationlistener MyListener = new Mylocationlistener (); @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_weather ); mlocationclient = new Locationclient (Getapplicationcontext ()); Declaration of Locationclient class//Mlocationclient.setaccesskey ("8mrnafzku3dodulnwub5lt2w"); v4.1//Mlocationclient.setak ("8mrnafzku3dodulnwub5lt2w"); V4.0mlocationclient.registerlocationlisteneR (MyListener); Register the Listener function setlocationoption (); Mlocationclient.start ();//start positioning initwidgets ();} private void Initwidgets () {mText = (TextView) Findviewbyid (r.id.tv_text); mtextpoi = (TextView) Findviewbyid (r.id.tv_ TEXT_POI); Button btn = (button) Findviewbyid (r.id.btn_request); Btn.setonclicklistener (this); btn = (Button) Findviewbyid ( R.ID.BTN_REQUEST_POI); Btn.setonclicklistener (this);} @Overrideprotected void OnDestroy () {Super.ondestroy (); Mlocationclient.stop ();//Stop positioning}/** * Set relevant parameters */private void Setlocationoption () {locationclientoption option = new Locationclientoption (); Option.setopengps (true); O Ption.setisneedaddress (TRUE);//returns the location result including address information option.setaddrtype ("All"),//returns the location result including address information option.setcoortype (" Bd09ll ");//Return positioning results are Baidu latitude and longitude, the default value of Gcj02option.setscanspan (5000);//Set the time interval for initiating a location request is 5000msoption.disablecache (true);// Disable Enable cache location Option.setpoinumber (5); Returns the maximum number of POI option.setpoidistance (1000); Poi Query Distance Option.setpoiextrainfo (true); Is there a need for specific information such as POI's phone and address option.setpriority (Locationclientoption.networkfirst); Priority network positioning//Option.setlocationmode (locationmode.battery_saving);//Set positioning mode mlocationclient.setlocoption (option);} public class Mylocationlistener implements Bdlocationlistener {@Overridepublic void onreceivelocation (bdlocation Location) {if (location = = null) return; StringBuffer sb = new StringBuffer, Sb.append ("Current Time:"), Sb.append (Location.gettime ()), sb.append ("\ n error code:"); Sb.append (Location.getloctype ()); Sb.append ("\ n Latitude:"); Sb.append (Location.getlatitude ()); Sb.append ("\ n Longitude:"); Sb.append (Location.getlongitude ()); Sb.append ("\ n radius:"); Sb.append (Location.getradius ()); if (Location.getloctype () = = bdlocation.typegpslocation) {sb.append ("\ n Speed:"), Sb.append (Location.getspeed ()); Sb.append ("\ n satellites:"); Sb.append ( Location.getsatellitenumber ());} else if (location.getloctype () = = bdlocation.typenetworklocation) {sb.append ("\ n address:"); Sb.append (location.getaddrstr ());} Mtext.settext (Sb.tostring ()); LOG.D (TAG, "onreceivelocation" + sb.tostring ());} public void Onreceivepoi (Bdlocation poilocation){//The POI function will be removed in the next version number if (poilocation = = null) {return;} StringBuffer sb = new StringBuffer, Sb.append ("Poi Time:"), Sb.append (Poilocation.gettime ()); Sb.append ("\nerror Code: "); Sb.append (Poilocation.getloctype ()); Sb.append (" \nlatitude: "); Sb.append (Poilocation.getlatitude ()); Sb.append ("\nlontitude:"); Sb.append (Poilocation.getlongitude ()); Sb.append ("\nradius:"); Sb.append ( Poilocation.getradius ()); if (poilocation.getloctype () = = bdlocation.typenetworklocation) {sb.append ("\naddr:"); Sb.append (Poilocation.getaddrstr ());} if (Poilocation.haspoi ()) {Sb.append ("\npoi:"); Sb.append (Poilocation.getpoi ());} else {sb.append ("Nopoi Information");} Mtextpoi.settext (Sb.tostring ()); LOG.D (TAG, "Onreceivepoi" + sb.tostring ());}} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.btn_request:if (mlocationclient! = null && mLo Cationclient.isstarted ()) mlocationclient.requestlocation (); ELSELOG.D (TAG, "locclient is null or not started"); Case r.id.btn_request_poi://Request POIData if (mlocationclient! = null && mlocationclient.isstarted ())) Mlocationclient.requestpoi (); Break;default: Break;}}}

code Download: baidulocation

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Using Baidu API to achieve hot (WIFI), GPS, base station positioning

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.