Android Baidu location to get the current location
The Baidu map Android SDK is an application interface based on Android 2.1 and later. You can use this SDK to develop a map application for Android mobile devices. by calling the map SDK interface, you can easily access Baidu map service and data, build map applications with rich functions and strong interactivity.
For Android projects, note that the libs file names are fixed; otherwise, an error is reported.
Layout file:
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; // locate the SDK's 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 to obtain the required information: startLocation = (Button) findViewById (R. id. addfence); startLocation. setOnClickListener (new OnClickListener () {public void onClick (View v) {InitLocation (); // initialize if (startLocation. getText (). equals ("enable positioning") {mLocationClient. start (); startLocation. setText ("Stop locating");} else {mLocationClient. stop (); startLocation. setText ("enable positioning") ;}}}@overrideprotected void onStop () {mLocationClient. stop (); super. onStop ();} private void InitLocation () {LocationClientOption option = new LocationClientOption (); option. setLocationMode (LocationMode. hight_Accuracy); // sets the high-precision positioning mode option. setCoorType ("bd09ll"); // sets the Baidu longitude/latitude Coordinate System Format option. setScanSpan (1000); // set the interval for initiating a request to 1000msoption. setIsNeedAddress (true); // obtain the location by decompiling. mLocationClient is only available for Network Locating. 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; // locates the SDK's core class public MyLocationListener mMyLocationListener; // defines the listener class pu Blic TextView mLocationResult, logMsg; @ Overridepublic void onCreate () {super. onCreate (); mLocationClient = new LocationClient (this. getApplicationContext (); mMyLocationListener = new MyLocationListener (); mLocationClient. registerLocationListener (mMyLocationListener);}/*** implement real-time callback listener */public class MyLocationListener implements BDLocationListener {public void onReceiveLocation (BDLocation location) {// Re Ceive Location StringBuffer sb = new StringBuffer (256); sb. append ("time:"); sb. append (location. getTime (); // obtain the current time sb. append ("\ nerror code:"); sb. append (location. getLocType (); // obtain the erro code to identify the current situation sb. append ("\ nlatitude:"); sb. append (location. getLatitude (); // obtain the latitude sb. append ("\ nlontitude:"); sb. append (location. getlongpolling (); // obtain the longitude sb. append ("\ nradius:"); sb. append (location. getRadius (); if (location. GetLocType () = BDLocation. typeGpsLocation) {// locate sb by GPS. append ("\ nspeed:"); sb. append (location. getSpeed (); // get the speed sb. append ("\ nsatellite:"); sb. append (location. getSatelliteNumber (); sb. append ("\ ndirection:"); sb. append ("\ naddr:"); sb. append (location. getAddrStr (); // obtain the current address sb. append (location. getDirection (); // obtain the location} else if (location. getLocType () = BDLocation. typeNetWorkLocation) {// locate sb through network connection. ap Pend ("\ naddr:"); sb. append (location. getAddrStr (); // obtain the current address // carrier information sb. append ("\ noperationers:"); sb. append (location. getOperators (); // get the operator ?} 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: