[Baidu map SDK] provides the simplest positioning function (no map interface) and sdk map.
In recent projects, you need to implement the location function without a map interface to locate the user's city. Therefore, in this article, this section describes how to use the Baidu map SDK to locate a non-navigation interface.
1. Apply for a Baidu developer account
2. Create an application and obtain the key
As shown in
3. Download the SDK file and import it to our project
The following is the project structure of my test project.
'
So here I only need to locate, and do not need other functions of Baidu map, such as navigation, so I only need to import the positioning SDK.
4. Configure permissions and write code
For more information about the configuration process, see Baidu's API description.
Http://developer.baidu.com/map/index.php? Title = android-locsdk/guide/v4-2
First, the main file
MainActivity. java
Package com. example. justlocation; import android. app. activity; import android. OS. bundle; import android. widget. textView; 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 {private TextView TV; // positioning client class public LocationClient mLocationClient = null; // locating listener class public BDLocationListener myListener = new MyLocationListener (); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. actitity_main); TV = (TextView) findViewById (R. id. TV); // initialize mLocationClient = new LocationClient (getApplicationContext (); // set the positioning parameter LocationClientOption option = new LocationClientOption (); option. setOpenGps (true); // enable gpsoption. setCoorType ("bd09ll"); // The return result is Baidu longitude and latitude. The default value is gcj02option. setScanSpan (5000); // set the interval for initiating a location request to 5000 ms // set the option for obtaining address information. setIsNeedAddress (true); mLocationClient. setLocOption (option); // register the listening function mLocationClient. registerLocationListener (myListener); // call this method to locate mLocationClient. start ();}/*** callback function after successful locating ** @ author zhaokaiqiang **/public class MyLocationListener implements BDLocationListener {@ Overridepublic void onReceiveLocation (BDLocation location) {if (location = null) return; StringBuffer sb = new StringBuffer (256); sb. append ("time:"); sb. append (location. getTime (); sb. append ("\ n return code:"); sb. append (location. getLocType (); sb. append ("\ n latitude:"); sb. append (location. getLatitude (); sb. append ("\ n longitude:"); sb. append (location. getlongpolling (); sb. append ("\ n radius:"); sb. append (location. getRadius (); sb. append ("\ n Province:"); sb. append (location. getProvince (); sb. append ("\ n City:"); sb. append (location. getCity (); if (location. getLocType () = BDLocation. typeGpsLocation) {sb. append ("\ n speed:"); sb. append (location. getSpeed (); sb. append ("\ n number of satellites:"); sb. append (location. getSatelliteNumber ();} TV. setText (TV. getText () + "\ n" + sb. toString ());}}}
Configuration File
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. example. justlocation "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 14 "android: targetSdkVersion =" 19 "/> <! -- This permission is used for network positioning --> <uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"> </uses-permission> <! -- This permission is used to access GPS positioning --> <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"> </uses-permission> <! -- Used to access Wi-Fi network information. wifi information is used to locate the network --> <uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"> </uses-permission> <! -- Obtains carrier information, which is used to support interfaces related to carrier information --> <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"> </uses-permission> <! -- This permission is used to obtain the permission for wifi, And wifi information is used to locate the network --> <uses-permission android: name = "android. permission. CHANGE_WIFI_STATE "> </uses-permission> <! -- Used to read the current status of the mobile phone --> <uses-permission android: name = "android. permission. READ_PHONE_STATE"> </uses-permission> <! -- Write extended storage and write data to the expansion card for writing offline positioning data --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "> </uses-permission> <! -- Access the network and access the Internet to locate the network --> <uses-permission android: name = "android. permission. INTERNET "/> <uses-permission android: name =" android. permission. MOUNT_UNMOUNT_FILESYSTEMS "> </uses-permission> <! -- Allow applications to read low-level system log files --> <uses-permission android: name = "android. permission. READ_LOGS "> </uses-permission> <application android: allowBackup =" true "android: icon =" @ drawable/ic_launcher "android: label =" @ string/app_name "android: theme = "@ style/AppTheme"> <meta-data android: name = "com. baidu. lbsapi. API_KEY "android: value =" ngTqhh1a0SF0PnzFfEc853W7 "/> <activity android: name =" com. example. justlocation. 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> <service android: name =" com. baidu. location. f "android: enabled =" true "android: process =": remote "> </service> </application> </manifest>
The running effect is not good, because it is not good on the real machine.
You need to leave a mailbox for the source program.
The Development Guide for Baidu map positioning SDK is completely incomprehensible.
To defeat the enemy, you cannot send troops to confront the enemy. Instead, you can gradually weaken the remaining strength of the enemy and gradually weaken the enemy.
Does the android sdk demo of Baidu map have another problem? It cannot be located on simulators.
Simulator positioning requires you to enter the latitude and longitude to determine ~ Does the simulator have a gps module ~