Android Baidu positioning to get the current specific location

Source: Internet
Author: User

Baidu Maps Android SDK is an application interface based on Android version 2.1 and above devices. You can use the SDK to develop a map application for Android mobile devices, and by invoking the Map SDK interface, you can easily access the Baidu Map service and data to build a rich and interactive map class application.

Android Project note the file name of Libs is fixed, otherwise it will be an error.

Layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <textview android:id= "@+id/textview1" android:layout_width= "Match_parent" Android:layout_h eight= "Wrap_content" android:layout_weight= "2.89" android:text= "" android:textappearance= "? android:a Ttr/textappearancelarge "/> <linearlayout android:layout_width=" Match_parent "android:layout_height = "Wrap_content" android:layout_weight= "0.50" android:gravity= "Center|top" android:orientation= "vertic            Al "> <button android:id=" @+id/addfence "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "open positioning"/> </LinearLayout></LinearLayout>

Mainactivity:

Package Com.example.getcoord_text;import Android.app.activity;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.textview;import Com.baidu.location.locationclient;import Com.baidu.location.locationclientoption;import Com.baidu.location.locationclientoption.locationmode;public class Mainactivity extends Activity{private Locationclient mlocationclient;//Positioning SDK Core class private TextView locationresult;private Button startlocation;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); mlocationclient = ((locationapplication) getapplication ()). Mlocationclient; Locationresult = (TextView) Findviewbyid (R.ID.TEXTVIEW1); ((locationapplication) getapplication ()). Mlocationresult = locationresult;//call Locationapplication, Get the information you need startlocation = (Button) Findviewbyid (r.id.addfence); Startlocation.setonclicklistener (new Onclicklistener () {Publicvoid OnClick (View v) {initlocation ();//Initialize if (Startlocation.gettext (). Equals ("open position")) {Mlocationclient.start (); Startlocation.settext ("Stop Positioning");} Else{mlocationclient.stop (); Startlocation.settext ("open position");}}); @Overrideprotected void OnStop () {mlocationclient.stop (); Super.onstop ();} private void Initlocation () {locationclientoption option = new Locationclientoption (); Option.setlocationmode ( locationmode.hight_accuracy)///Set high precision positioning mode Option.setcoortype ("Bd09ll");//set Baidu latitude and longitude coordinate system format option.setscanspan (1000) ;//Set the time interval for initiating a location request to 1000msoption.setisneedaddress (true);//decompile to obtain a specific location, only network positioning can mlocationclient.setlocoption (option) ;}}

locationapplication class:

Package Com.example.getcoord_text;import Android.app.application;import Android.util.log;import Android.widget.textview;import Com.baidu.location.bdlocation;import Com.baidu.location.bdlocationlistener;import Com.baidu.location.locationclient;public class Locationapplication extends application {public locationclient mlocationclient;//Positioning SDK Core class public Mylocationlistener mmylocationlistener;//defines the Listener class public TextView Mlocationresult, logmsg; @Overridepublic void OnCreate () {super.oncreate (); mlocationclient = new Locationclient ( This.getapplicationcontext ()); Mmylocationlistener = new Mylocationlistener (); Mlocationclient.registerlocationlistener (Mmylocationlistener);} /** * Implement real bit callback listener */public class Mylocationlistener implements Bdlocationlistener {public void onreceivelocation (bdlocation Location) {//receive location stringbuffer sb = new StringBuffer, Sb.append ("Time:"); Sb.append (Location.gettime () );//Get Current time Sb.append ("\nerror Code:"); Sb.append (Location.getloctype ());//Get Erro code to know positioning status SB.APpend ("\nlatitude:"); Sb.append (Location.getlatitude ());//Gain Latitude Sb.append ("\nlontitude:"); Sb.append ( Location.getlongitude ());//Obtain Longitude Sb.append ("\nradius:"); Sb.append (Location.getradius ()); if (Location.getloctype () = = bdlocation.typegpslocation) {//via GPS positioning Sb.append ("\nspeed:"); Sb.append (Location.getspeed ());//Get Speed sb.append ("\ Nsatellite: "); Sb.append (Location.getsatellitenumber ()); Sb.append (" \ndirection: "); Sb.append (" \naddr: "); Sb.append (Location.getaddrstr ());//Get the current address Sb.append (Location.getdirection ());//Get directions} else if (Location.getloctype ( = = bdlocation.typenetworklocation) {//locate Sb.append via network connection ("\NADDR:"); Sb.append (Location.getaddrstr ());//get current address// Carrier information Sb.append ("\noperationers:"); Sb.append (Location.getoperators ());//access to operators? }logmsg (Sb.tostring ()); LOG.I ("Baidulocationapidem", sb.tostring ());}} /** * Display Request String * @param str */public void logmsg (String str) {try {if (Mlocationresult! = null) mlocationresult.settext (str) ;} catch (Exception e) {e.printstacktrace ();}}}

Androidmanifest.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.getcoord_text "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-s DK android:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" Androi D.permission.access_coarse_location "> </uses-permission> <uses-permission android:name=" Android.permission.ACCESS_FINE_LOCATION "> </uses-permission> <uses-permission android:name=" Android.permission.ACCESS_WIFI_STATE "> </uses-permission> <uses-permission android:name=" Android.permission.ACCESS_NETWORK_STATE "> </uses-permission> <uses-permission android:name=" Android.permission.CHANGE_WIFI_STATE "> </uses-permission> <uses-permission android:name=" Android.permission.READ_PHONE_STATE "> </uses-permission> <uses-permission Android:namE= "Android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission> <uses-permission android:name= " Android.permission.INTERNET "/> <uses-permission android:name=" Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "> </uses-permission> <uses-permission android:name=" Android.permission.READ_LOGS "> </uses-pe rmission> <uses-permission android:name= "Android.permission.VIBRATE"/> <uses-permission android:name= " Android.permission.WAKE_LOCK "/> <uses-permission android:name=" Android.permission.WRITE_SETTINGS "/> < Application Android:name= "Com.example.getcoord_text. Locationapplication "android:allowbackup=" true "android:icon=" @drawable/ic_launcher "android:label=" @ String/app_name "Android:theme=" @style/apptheme "> <service android:name=" com.baidu.locatio N.f "android:enabled=" true "android:process=": Remote > <intent-filter> <action android:name= "com.baidu.location.service_v2.2" > </action> </intent-filter> </service> <!--Meta-data needs to be written in application--<meta- Data android:name= "Com.baidu.lbsapi.API_KEY" android:value= "Sgfsyzlrvnhgr9giph70g6nd"/> & Lt;activity android:name= "Com.example.getcoord_text. Mainactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application></manifest>

Android Baidu positioning to get the current specific location

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.