to integrate Baidu API positioning needs to complete the following steps:1: Registered developer
2: Apply for key
3: Download the jar package
4: Code Integrationfirst Look at the effect:
1: Registered developerIt is best to apply in advance, 1, 2 days for audit
2: Apply for keyI use Android Studio, so use the command line to view the SHA1 code
3: Download the jar packagesimply download the "Location feature" development package to
4: Code Integration (detailed official explanation, the following code is concise and practical mainly)
Package Com.union.roid.testroid;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;public class MainActivity extends Activity {public locationclient mlocationclient = null; Public Bdlocationlistener MyListener = new Mylocationlistener (); @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mlocationclient = new Locationclient (Getapplicationcontext ());//Declaration Locationclient Class Mlocationclient.registerlocation Listener (MyListener);//Register the Listener function locationclientoption option = new Locationclientoption ();//Set Request parameters Option.setlo Cationmode (LocationClientOption.LocationMode.Hight_Accuracy);//Set positioning mode Option.setcoortype ("Bd09ll");// The returned positioning result is Baidu latitude and longitude, the default value gcj02 OPTION.SETSCAnspan (5000);//sets the time interval for initiating a location request to 5000ms option.setisneedaddress (true);//The Returned location result contains address information option.setneeddevicedirect (true);//The positioning result returned contains the direction of the phone head mlocationclient.setlocoption (option); Mlocationclient.start ();//Start request if (mlocationclient! = null && mlocationclient.isstarted ()) Mloca Tionclient.requestlocation (); else LOG.D ("location", "locclient was null or not started"); } public class Mylocationlistener implements Bdlocationlistener {@Override public void onreceivelocation (bdlocation location) {if (location = = null) return; StringBuffer sb = new StringBuffer (256); Sb.append ("Time:"); Sb.append (Location.gettime ()); Sb.append ("\nerror Code:"); Sb.append (Location.getloctype ()); Sb.append ("\nlatitude:"); Sb.append (Location.getlatitude ()); Sb.append ("\nlontitude:"); Sb.append (LocatiOn.getlongitude ()); Sb.append ("\nradius:"); Sb.append (Location.getradius ()); if (location.getloctype () = = bdlocation.typegpslocation) {sb.append ("\nspeed:"); Sb.append (Location.getspeed ()); Sb.append ("\nsatellite:"); Sb.append (Location.getsatellitenumber ()); } else if (location.getloctype () = = bdlocation.typenetworklocation) {sb.append ("\naddr:"); Sb.append (Location.getaddrstr ()); } LOG.V ("--->", "========="); LOG.V ("--->", sb.tostring ()); } }}
Baidu Map API get latitude and longitude and city name