Android uses Baidu Map SDK to get location information

Source: Internet
Author: User

This article was developed using Android Studio.

Getting location information is relatively straightforward and we only need the following steps:

The first step, register Baidu account, in the Baidu map open platform new application, generate Api_key. This is not a detail, please go here: Http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/key

The second step, download the SDK, address: http://lbsyun.baidu.com/index.php?title=android-locsdk/geosdk-android-download

Step three, build Android Studio project (skip the talk), configure the environment:
Put the extracted files into the Libs folder, and under the Src/main to create a folder called Jnilibs, and the extracted folders inside the folder in, such as:

Fourth step, add the Baidulbs_android.jar to the environment variable (right-click, add as Library), and in the app's Build.gradle Android added:

sourceSets {        main {            jniLibs.srcDirs = [‘libs‘]        }    }


Fifth step, declare the permissions in the Androidmanifest.xml file:

<uses-permission android:name="Android.permission.INTERNET"/>    <uses-permission android:name="Android.permission.ACCESS_WIFI_STATE" />     <uses-permission android:name="Android.permission.ACCESS_NETWORK_STATE" / >    <!--network location --    <uses-permission android:name="Android.permission.ACCESS_COARSE_LOCATION" / >    <!--GPS positioning --    <uses-permission android:name="Android.permission.ACCESS_FINE_LOCATION" /> 

and add the following in the application tag:

<meta-data            android:name="com.baidu.lbsapi.API_KEY"            android:value="你申请的API key" />        <service            android:name="com.baidu.location.f"            android:enabled="true"            android:process=":remote" />

The sixth step, test the code, get the positioning information:

 Public  class mainactivity extends appcompatactivity {     PublicLocationclient mlocationclient =NULL; PublicBdlocationlistener MyListener =NewMylocationlistener ();@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);    Startlocate (); }/** * Positioning * * *    Private void startlocate() {mlocationclient =NewLocationclient (Getapplicationcontext ());//Declaration Locationclient ClassMlocationclient.registerlocationlistener (MyListener);//Register listener functionlocationclientoption option =NewLocationclientoption (); Option.setlocationmode (LocationClientOption.LocationMode.Battery_Saving);//optional, default high precision, set positioning mode, high accuracy, low power, device onlyOption.setcoortype ("Bd09ll");//optional, default GCJ02, set the returned positioning result coordinate system        intspan = +; Option.setscanspan (span);//optional, default 0, which is to locate only once, set the interval for initiating a location request to be greater than or equal to 1000ms is validOption.setisneedaddress (true);//optional, set whether address information is required, default does not needOption.setopengps (true);//optional, default false to set whether to use GPSOption.setlocationnotify (true);//optional, default false to set whether GPS results are output according to 1S/1 frequency when GPS is activeOption.setisneedlocationdescribe (true);//optional, default false, setting whether to require positional semantic results, can be obtained in bdlocation.getlocationdescribe, the result is similar to "near the gate of Tian ' an, Beijing"Option.setisneedlocationpoilist (true);//optional, default false, set whether POI results are required, can be obtained in bdlocation.getpoilistOption.setignorekillprocess (false);//optional, default True, the location SDK inside is a service, and placed in a standalone process, set whether to kill the process at stop, the default does not killOption. Setignorecacheexception (false);//optional, default false, sets whether to collect crash information, default collectionOption.setenablesimulategps (false);//optional, default false to set whether the GPS simulation results need to be filtered by defaultmlocationclient.setlocoption (option);//Open positionMlocationclient.start (); }Private  class Mylocationlistener implements Bdlocationlistener {        @Override         Public void onreceivelocation(bdlocation location) {StringBuffer SB =NewStringBuffer ( the); 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) {//GPS Location ResultsSb.append ("\nspeed:"); Sb.append (Location.getspeed ());/per unit: km/hSb.append ("\nsatellite:");                Sb.append (Location.getsatellitenumber ()); Sb.append ("\nheight:"); Sb.append (Location.getaltitude ());//unit: MSb.append ("\ndirection:"); Sb.append (Location.getdirection ());//Unit degreeSb.append ("\NADDR:");                Sb.append (Location.getaddrstr ()); Sb.append ("\ndescribe:"); Sb.append ("GPS positioning Success"); }Else if(Location.getloctype () = = bdlocation.typenetworklocation) {//Network location ResultsSb.append ("\NADDR:"); Sb.append (Location.getaddrstr ());//Carrier informationSb.append ("\noperationers:");                Sb.append (Location.getoperators ()); Sb.append ("\ndescribe:"); Sb.append ("Network positioning Success"); }Else if(Location.getloctype () = = bdlocation.typeofflinelocation) {//Offline location resultsSb.append ("\ndescribe:"); Sb.append ("Offline positioning is successful, offline location results are also effective"); }Else if(Location.getloctype () = = Bdlocation.typeservererror) {Sb.append ("\ndescribe:"); Sb.append ("service-side network location failed, can be feedback IMEI number and the approximate location of the time to [email protected], someone will be traced to the cause"); }Else if(Location.getloctype () = = bdlocation.typenetworkexception) {Sb.append ("\ndescribe:"); Sb.append ("Network differences cause location failure, please check the network is unobstructed"); }Else if(Location.getloctype () = = bdlocation.typecriteriaexception) {Sb.append ("\ndescribe:"); Sb.append ("Unable to obtain an effective positioning based on the failure of the location, generally because of the phone's reasons, in flight mode is generally caused by this result, you can try to focus on the mobile phone"); } sb.append ("\nlocationdescribe:"); Sb.append (Location.getlocationdescribe ());//Location semantics informationlist<poi> list = Location.getpoilist ();//POI data            if(List! =NULL) {Sb.append ("\npoilist size =:"); Sb.append (List.size ()); for(Poi p:list) {Sb.append ("\npoi=:"); Sb.append (P.getid () +" "+ p.getname () +" "+ P.getrank ()); }} LOG.E ("Description:", sb.tostring ()); }    }

Android uses Baidu Map SDK to get location information

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.