Android uses Baidu lbs sdk (2) for positioning, androidlbs

Source: Internet
Author: User

Android uses Baidu lbs sdk (2) for positioning, androidlbs

Yesterday, I initially contacted Baidu's lbs sdk to allow applications to display maps through MapView. Today, based on yesterday's code, I implemented the following positioning functions:

First, let's take a look:



The buttons in the lower left corner can be switched into three modes:

NORMAL (LocationMode. NORMAL ),

Follow (LocationMode. FOLLOWING ),

I believe that those who have used Baidu map are familiar with these models.

The following code is used:

Declare a MapView and a BaiduMap respectively:

private MapView mMapView;private BaiduMap mBaiduMap;

Initialize the SDK before SetContentView:

SDKInitializer.initialize(getApplicationContext());

How to change the positioning icon:

MCurrentMarker = BitmapDescriptorFactory. fromResource (R. drawable. icon_geo); // custom icon // mCurrentMarker = null; // default icon mBaiduMap. setMyLocationConfigeration (new MyLocationConfiguration (mCurrentMode, true, mCurrentMarker ));

Positioning mode:

private LocationMode mCurrentMode;

MCurrentMode = LocationMode. NORMAL; // NORMAL mode // mCurrentMode = LocationMode. FOLLOWING; // Follow Mode // mCurrentMode = LocationMode. COMPASS; // COMPASS mode mBaiduMap. setMyLocationConfigeration (new MyLocationConfiguration (mCurrentMode, true, mCurrentMarker ));


Initialization:

// Map initialization mMapView = (MapView) findViewById (R. id. bmapView); mBaiduMap = mMapView. getMap (); // enable the position layer mBaiduMap. setMyLocationEnabled (true); // locate initialization mLocClient = new LocationClient (this); mLocClient. registerLocationListener (myListener); LocationClientOption option = new LocationClientOption (); option. setOpenGps (true); // enable gpsoption. setCoorType ("bd09ll"); // sets the coordinate type option. setScanSpan (5000); mLocClient. setLocOption (option); mLocClient. start ();

SetScanSpan is the positioning interval (MS), setCoorType coordinate types are divided into three types:

Bd09ll Baidu encrypted longitude and latitude coordinates

Bd09 Baidu encrypted mocato Coordinate

Gcj02 coordinate of the national test authority to encrypt the longitude and latitude


Find the SDK listening function:

Public class MyLocationListenner implements BDLocationListener {@ Overridepublic void onReceiveLocation (BDLocation location) {// After map view is destroyed, if (location = null | mMapView = null) return; MyLocationData locData = new MyLocationData. builder (). accuracy (location. getRadius () // set the direction information obtained by the developer, clockwise 0-100 ction ). latitude (location. getLatitude ()). longpolling (location. getlongpolling ()). build (); mBaiduMap. setMyLocationData (locData); if (isFirstLoc) {isFirstLoc = false; LatLng ll = new LatLng (location. getLatitude (), location. getlongpolling (); MapStatusUpdate u = MapStatusUpdateFactory. newLatLng (ll); mBaiduMap. animateMapStatus (u) ;}} public void onReceivePoi (BDLocation poiLocation ){}}


Rewrite onPause, onResume, onDestory:

@ Overrideprotected void onPause () {mMapView. onPause (); super. onPause () ;}@ Overrideprotected void onResume () {mMapView. onResume (); super. onResume () ;}@ Overrideprotected void onDestroy () {// destroy the mLocClient when exiting. stop (); // close the position layer mBaiduMap. setMyLocationEnabled (false); mMapView. onDestroy (); mMapView = null; super. onDestroy ();}


Declare the Service in AndroidManifest. xml. This step is critical and cannot be located without writing:

 <service            android:name="com.baidu.location.f"            android:enabled="true"            android:process=":remote" >        </service>

Of course, the Key is also indispensable and must be correct:

        <meta-data            android:name="com.baidu.lbsapi.API_KEY"            android:value="************************" />

Finally, add the required permissions to the AndroidManifest. xml file:

<! -- For BaiduMap sdk start --> <permission android: name = "android. permission. BAIDU_LOCATION_SERVICE "> </permission> <uses-permission android: name =" android. permission. BAIDU_LOCATION_SERVICE "> </uses-permission> <uses-permission android: name =" android. permission. ACCESS_NETWORK_STATE "> </uses-permission> <uses-permission android: name =" android. permission. ACCESS_COARSE_LOCATION "> </uses-permission> <us Es-permission android: name = "android. permission. INTERNET "> </uses-permission> <uses-permission android: name =" android. permission. ACCESS_MOCK_LOCATION "> </uses-permission> <! -- <Uses-permission android: name = "android. permission. WRITE_APN_SETTINGS "> </uses-permission> --> <uses-permission android: name =" android. permission. ACCESS_FINE_LOCATION "> </uses-permission> <uses-permission android: name =" com. android. launcher. permission. READ_SETTINGS "/> <uses-permission android: name =" android. permission. WAKE_LOCK "> </uses-permission> <uses-permission android: name =" android. permission. CHANGE_WIFI_STATE "/> <uses-permission android: name =" android. permission. ACCESS_WIFI_STATE "/> <uses-permission android: name =" android. permission. ACCESS_GPS "/> <uses-permission android: name =" android. permission. READ_PHONE_STATE "/> <uses-permission android: name =" android. permission. READ_CONTACTS "/> <uses-permission android: name =" android. permission. CALL_PHONE "/> <uses-permission android: name =" andro Id. permission. READ_SMS "/> <uses-permission android: name =" android. permission. SEND_SMS "/> <! -- SDK1.5 requires the permission of android. permission. GET_TASKS to determine whether the program is the currently running application? --> <Uses-permission android: name = "android. permission. GET_TASKS "/> <uses-permission android: name =" android. permission. CAMERA "> </uses-permission> <uses-permission android: name =" android. permission. RECORD_AUDIO "> </uses-permission> <uses-permission android: name =" android. permission. WRITE_EXTERNAL_STORAGE "> </uses-permission> <uses-permission android: name =" android. permission. RECEIVE_BOOT_COMPLETED" /> <Uses-permission android: name = "android. permission. BROADCAST_STICKY"/> <uses-permission android: name = "android. permission. WRITE_SETTINGS"/> <! -- Call cancel --> <uses-permission android: name = "android. permission. process_outgoing_cils "> </uses-permission> <uses-permission android: name =" android. permission. READ_PHONE_STATE "/> <uses-permission android: name =" android. permission. MODIFY_AUDIO_SETTINGS "/> <! -- For BaiduMap sdk end -->


This is all done.


Reprinted please indicate the source: Zhou mu Shui CSDN blog http://blog.csdn.net/zhoumushui

My GitHub: Zhou mu Shui's GitHub https://github.com/zhoumushui


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.