Baidu map api obtains latitude and longitude and city names, and map api obtains latitude and longitude

Source: Internet
Author: User

Baidu map api obtains latitude and longitude and city names, and map api obtains latitude and longitude
To integrate Baidu API positioning, follow these steps: 1. Register a developer
2: Apply for key
3: Download the jar package
4: Code integration first look at the effect:

1: it is best to apply for a registered developer in advance. The review takes 1 or 2 days.
2: I used Android Studio to apply for the key, so I used the command line to view the SHA1 code.
3: To download the jar package, you only need to download the "positioning function" Development Kit.
4: Code integration (for details, see the official explanation. The following code is simple and practical)

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 myLis Tener = new MyLocationListener (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mLocationClient = new LocationClient (getApplicationContext (); // declare the LocationClient class mLocationClient. registerLocationListener (myListener); // register the listening function LocationClientOption option = new LocationClientOption (); // set the request parameter option. setLocatio NMode (LocationClientOption. locationMode. hight_Accuracy); // sets the positioning mode option. setCoorType ("bd09ll"); // The return result is Baidu longitude and latitude. The default value is gcj02 option. setScanSpan (5000); // set the interval between initiating a request for locating to 5000 ms option. setIsNeedAddress (true); // The returned positioning result contains the address information option. setNeedDeviceDirect (true); // The returned positioning result contains the direction of the mobile phone head to mLocationClient. setLocOption (option); mLocationClient. start (); // initiate the request if (mLocationClient! = Null & mLocationClient. isStarted () mLocationClient. requestLocation (); else Log. d ("Location", "locClient is 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. getlongpolling (); 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 ());}}}





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.