Android Positioning Based on Baidu positioning SDK (LBS)

Source: Internet
Author: User

Nowadays, many apps automatically locate the current location, such as group buying websites, recruitment websites, and weather apps. GPS, base station, and Wi-Fi can be used for positioning. Baidu map Android positioning SDK uses the current GPS information of the device (GPS positioning), and the base station information (base station positioning) and Wi-Fi information (Wi-Fi positioning. After a developer successfully integrates the Baidu positioning SDK in an application, the developer can easily Request Location Information from Baidu through the positioning SDK interface.

The positioning SDK will generate the positioning basis based on the current situation of the device (such as whether to enable GPS, whether to connect to the network, and whether to scan Wi-Fi information, based on the actual positioning policies set by developers (including three types: high-precision mode, low-power mode, and device-only mode.

About Baidu SDK more detailed positioning principle can refer to Baidu documentation: http://developer.baidu.com/map/geosdk.htm

What is LBS can refer to: http://baike.baidu.com/link? Url = QyoQddAxcacf6YHoPhxNK8omWn3HxZyG3Hm5Wf1 -- Signature

To use Baidu positioning SDK first to register as a developer: http://developer.baidu.com/map/

Download and locate the SDK


To apply for a key, follow the instructions on http://developer.baidu.com/map/geosdk-android-key.htm.


The key I get is


If all the preceding conditions are met, create a project and import the SDK <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + xL/CvL3hubk8L3A + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20140412/2014041209051995.png" alt = "\">

Effect after Positioning


The application configuration file AndroidManifest. xml Defines permissions and declares services, which can be found in Baidu's documentation.

 
     
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
              
          
          
                      
                                  
               
              
 
Where

Android: name = "com. baidu. lbsapi. API_KEY"
Android: value = "8mrnaFzKu3DoduLnWuB5Lt2w"/>

The value is the AK you applied for. You can set it in the Code or in the configuration file. The Code settings are as follows:

MLocationClient. setAccessKey ("8mrnaFzKu3DoduLnWuB5Lt2w"); // V4.1

Code in Activity

Package com. dzt. weather; import com. baidu. location. BDLocation; import com. baidu. location. BDLocationListener; import com. baidu. location. locationClient; import com. baidu. location. locationClientOption; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView ;/** * Baidu base station positioning error return code * // 61: GPS Positioning Result // 62: Scan integration positioning basis failure. The positioning result is invalid. // 63: failed to initiate a request to the server due to a network exception. The positioning result is invalid. // 65: locate the cache result. // 66: offline location result. Returned results when calling requestOfflineLocaiton // 67: offline location failed. Returned results when you call requestOfflineLocaiton // 68: when the network connection fails, find the corresponding results when the local offline location is located // 161: indicates the network location result // 162 ~ 167: Server locating failed // 502: KEY parameter error // 505: KEY does not exist or is invalid // 601: The KEY service is disabled by the developer/602: KEY Mcode does not match, this means that there is a problem with the security code during your ak configuration. Please make sure that: sha1 is correct, ";" Semicolon is in English; and the package name is the package name of your current running application // 501-700: KEY Verification Failed public class WeatherActivity extends Activity implements OnClickListener {private static final String TAG = "dzt "; private TextView mText; private TextView mTextPoi; private LocationClient mLocationClient = null; private BDLocationListener myLi Stener = new MyLocationListener (); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_weather); mLocationClient = new LocationClient (getApplicationContext (); // declare the LocationClient class // mLocationClient. setAccessKey ("8mrnaFzKu3DoduLnWuB5Lt2w"); // V4.1 // mLocationClient. setAK ("8mrnaFzKu3DoduLnWuB5Lt2w"); // V4.0mLocationClient. re GisterLocationListener (myListener); // register the listening function setLocationOption (); mLocationClient. start (); // start locating initWidgets ();} private void initWidgets () {mText = (TextView) findViewById (R. id. TV _text); mTextPoi = (TextView) findViewById (R. id. TV _text_poi); Button btn = (Button) findViewById (R. id. btn_request); btn. setOnClickListener (this); btn = (Button) findViewById (R. id. btn_request_poi); btn. setOnClickListener (this );} @ Overrideprotected void onDestroy () {// TODO Auto-generated method stubsuper. onDestroy (); mLocationClient. stop (); // stop positioning}/*** set related parameters */private void setLocationOption () {LocationClientOption option = new LocationClientOption (); option. setOpenGps (true); option. setIsNeedAddress (true); // The returned positioning result contains the address information option. setAddrType ("all"); // The returned positioning result contains the address information option. setCoorType ("bd09ll"); // The return result is Baidu longitude and latitude. The default value is gcj02option. s EtScanSpan (5000); // you can specify a time interval of msoption to initiate a request. disableCache (true); // disable cache locating option. setPoiNumber (5); // maximum number of returned POI options. setPoiDistance (1000); // poi query distance option. setPoiExtraInfo (true); // whether the POI phone number, address, and other details are required. mLocationClient. setLocOption (option);} public class MyLocationListener implements BDLocationListener {@ Overridepublic void onReceiveLocation (BDLocation location) {if (location = null) return; StringB Uffer sb = new StringBuffer (256); sb. append ("current time:"); sb. append (location. getTime (); sb. append ("\ n Error 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 (); if (location. getLocType () = BDLocation. typeGpsLocation) {sb. append ("\ n speed:"); sb. append (location. GetSpeed (); sb. append ("\ n number of satellites:"); sb. append (location. getSatelliteNumber ();} else if (location. getLocType () = BDLocation. typeNetWorkLocation) {sb. append ("\ n address:"); sb. append (location. getAddrStr ();} mText. setText (sb. toString (); Log. d (TAG, "oncancelocation" + sb. toString ();} public void onReceivePoi (BDLocation poiLocation) {// remove the poi function in the next version if (poiLocation = null) {return;} StringBuffer sb = ne W StringBuffer (256); sb. append ("Poi time:"); sb. append (poiLocation. getTime (); sb. append ("\ nerror code:"); sb. append (poiLocation. getLocType (); sb. append ("\ nlatitude:"); sb. append (poiLocation. getLatitude (); sb. append ("\ nlontitude:"); sb. append (poiLocation. getlongpolling (); sb. append ("\ nradius:"); sb. append (poiLocation. getRadius (); if (poiLocation. getLocType () = BDLocation. typeNetWorkLocation) {sb. Append ("\ naddr:"); sb. append (poiLocation. getAddrStr ();} if (poiLocation. hasPoi () {sb. append ("\ nPoi:"); sb. append (poiLocation. getPoi ();} else {sb. append ("noPoi information");} mTextPoi. setText (sb. toString (); Log. d (TAG, "onReceivePoi" + sb. toString () ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. btn_request: if (mLocationClient! = Null & mLocationClient. isStarted () mLocationClient. requestLocation (); elseLog. d (TAG, "locClient is null or not started"); break; case R. id. btn_request_poi: // request POI data if (mLocationClient! = Null & mLocationClient. isStarted () mLocationClient. requestPoi (); break; default: break ;}}}
Complete Demo http://download.csdn.net/detail/deng0zhaotai/7177379

It must be noted that the accesskey of V4.1 must be set in V4.1. If no NULL pointer exception is prompted, you can locate the accesskey in V4.0, however, it is unclear whether there are other risks. V4.1 sets the AK function as setAccessKey in the code, and V4.0 sets the AK function as setAK.

There are still web files that haven't called mLocationClient. start (); // start positioning. If no call is made, it cannot be set. There are still many inaccuracies in Baidu webpage documentation, so be sure to pay attention to it during use.

You can also obtain the province or city


Sb. append ("\ n Province:"); sb. append (location. getProvince (); sb. append ("\ n City:"); sb. append (location. getCity (); sb. append ("\ n district/county:"); sb. append (location. getDistrict (); sb. append ("\ n sub-district:"); sb. append (location. getStreet (); sb. append ("\ n street number:"); sb. append (location. getStreetNumber (); sb. append ("\ n address:"); sb. append (location. getAddrStr ());



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.