Use Baidu map positioning SDK to obtain positioning information.

Source: Internet
Author: User

I recently learned About Baidu map SDK and recorded it.

Baidu provides detailed developer documentation. For details, refer.

I will not go into details about how to use the positioning SDK. See http://developer.baidu.com/map/geosdk-android.htm

Let's roughly describe my encapsulated Application Scenario: assuming that the APP occasionally locates, different activities of the APP or scenarios may need to request location. In some cases, an Activity can take the initiative to close the location. After obtaining the positioning information, you must disable the positioning and pass the positioning information to an Activity or a requester. Encapsulate these requirements.

The solution is as follows:

Customize an Application and encapsulate the positioning in the Application. The Code is as follows:

Public class LocationApplication extends Application {private LocationClient locationClient = null; private LocationListenner myListener = new LocationListenner (); // Baidu positioning SDK can return three coordinate systems: bd09, bd09ll, and gcj02, bd09ll can be displayed on Baidu map without any deviation. // Gcj02 is developed by the test board. Private static final String COOR_TYPE = "gcj02"; private static final String BAIDU_LOCAL_SDK_SERVICE_NAME = "com. baidu. location. service_v2.9 "; // The positioning sdk provides two locating modes: timed locating and app active request locating. // SetScanSpan <1000 indicates that the app actively requests the location; // setScanSpan> = 1000 indicates the scheduled location mode (the setScanSpan value is the time interval of the scheduled location )) // In the timed positioning mode, the positioning sdk updates the location based on the time specified by the app, and calls back the Positioning Result regularly. // This positioning mode is applicable when you want to obtain continuous positioning results. // For a single location application or an app that occasionally requires location information, you can use the app to actively request location. Private static final int SCAN_SPAN_TIME = 500; private static final String PRODUCT_NAME = "com. youku. extends ke "; private DataFeedbackListener dataFeedbackListener; @ Overridepublic void onCreate () {locationClient = new LocationClient (this); locationClient. registerLocationListener (myListener); setLocationOption (); super. onCreate () ;}// set the relevant parameter private void setLocationOption () {LocationClientOption option = new LocationClientOption (); option. setOpenGps (true); // enable gps // set the coordinate type, option. setCoorType (COOR_TYPE); option. setServiceName (BAIDU_LOCAL_SDK_SERVICE_NAME); option. setPoiExtraInfo (true); // you can return the location information, for example, "No. 8, Haidian Street, Haidian District, Beijing ", the detailed geographic location information option can be obtained only when all is set and wifi is located. setAddrType ("all"); option. setScanSpan (SCAN_SPAN_TIME); option. setPriority (LocationClientOption. netWorkFirst); // sets the network priority. If this parameter is not set, the default value is gps priority option. setPoiNumber (10); option. disableCache (true); // true indicates that cache locating is disabled, and false indicates that cache locating option is enabled. setProdName (PRODUCT_NAME); locationClient. setLocOption (option);} public void setDataFeedbackListener (DataFeedbackListener dataFeedbackListener) {this. dataFeedbackListener = dataFeedbackListener;}/*** start locating * @ Title: startLocate * @ return void * @ date 4:32:26 */public void startLocate () {if (locationClient. isStarted () {locationClient. requestLocation ();} else {locationClient. start () ;}}/*** you want to proactively stop locating and call this method * @ Title: stopLocate * @ return void * @ date 4:31:12 */public void stopLocate () {if (locationClient. isStarted () {locationClient. stop () ;}/ *** locate the returned value listener * @ Package com. baidu. locTest * @ ClassName: LocationListenner * @ author * @ mail * @ date 2013-7-2 4:31:49 */class LocationListenner implements BDLocationListener {@ Overridepublic void onReceiveLocation (BDLocation location) {if (location = null) return; if (location. getLatitude ()> 0 | location. getlonglistener ()> 0) {dataFeedbackListener. onReceiver (location); locationClient. stop ();} else {locationClient. requestLocation () ;}} public void onReceivePoi (BDLocation poiLocation) {if (poiLocation = null) {return ;}}}}

Then locate the request in a specific Activity. The processing method is as follows:

@ Overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); MTV = (textview) findviewbyid (R. id. textview); mstartbtn = (button) findviewbyid (R. id. startbtn); location = (locationapplication) getapplication (); location. setdatafeedbacklistener (New datafeedbacklistener () {@ overridepublic void onreceiver (bdlocation location) {// todo auto-generated method stubdisplaylocation (location) ;}}); // start the mstartbtn button. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {location. startlocate () ;}});} private void displaylocation (bdlocation location) {stringbuffer sb = new stringbuffer (256); 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. getlongpolling (); latitude = location. getlatitude (); longdistance = location. getlongpolling (); log. D ("LZC", "latitude ==============>" + latitude + ", longpolling ============> "+ longpolling); sb. append ("\ nradius:"); sb. append (location. getradius (); If (location. getloctype () = bdlocation. typegpslocation) {sb. append ("\ nspeed:"); sb. append (location. getspeed (); sb. append ("\ nsatellite:"); sb. append (location. getsatellitenumber (); location. getaddrstr ();} else if (location. getloctype () = bdlocation. typenetworklocation) {sb. append ("\ naddr:"); log. D ("LZC", "location ADDR =======================>" + location. getaddrstr (); sb. append (location. getaddrstr ();} sb. append ("\ nsdk version:"); // sb. append (mlocclient. getversion (); sb. append ("\ niscellchangeflag:"); sb. append (location. iscellchangeflag (); MTV. settext (sb. tostring ());}

Have questions.

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.