Baidu Map API get latitude and longitude and city name

Source: Internet
Author: User

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

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.