Android Baidu map SDK v3.3.0-map positioning and layer display

Source: Internet
Author: User

Android Baidu map SDK v3.3.0-map positioning and layer display
In the previous blog, we successfully imported the map into our project. In this article, we are going to add a map: first, the positioning function; second, the layer display, and third, combine the direction sensor to confirm the direction of the road by rotating the mobile phone. With these three functions, the map can already serve me!

For convenience, I put all the buttons in the menu.
1. configure a service in AndroidManifest. xml

   
         
         
 

2. Start positioning for the first time 
/*** Initialize location */private void initMyLocation () {// map initialize mBaiduMap = mMapView. getMap (); MapStatusUpdate msu = MapStatusUpdateFactory. zoomTo (15.0f); mBaiduMap. setMapStatus (msu); // enable the positioning 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 (1000); // set the interval for initiating a request to 1000msoption. setIsNeedAddress (true); // The returned positioning result contains the address information option. setNeedDeviceDirect (true); // The returned positioning result contains the direction of the mobile phone head to mLocClient. setLocOption (option); // sets the location parameter // mLocClient. start ();}

3. Then the listener MyLocationListener is located:
/*** Locate the listener ** @ author TanZuAi */public class MyOrientationListener implements SensorEventListener {private Context context; private SensorManager sensorManager; private Sensor sensor; private float lastX; private OnOrientationListener listener; public MyOrientationListener (Context context) {this. context = context;} // start public void start () {// obtain sensor manager sensorManager = (SensorManager) cont Ext. getSystemService (Context. SENSOR_SERVICE); if (sensorManager! = Null) {// obtain the direction sensor Sensor = sensorManager. getdefasensensor (sensor. TYPE_ORIENTATION);} // register if (sensor! = Null) {// SensorManager. SENSOR_DELAY_UIsensorManager.registerListener (this, sensor, SensorManager. SENSOR_DELAY_UI) ;}}// stop public void stop () {sensorManager. unregisterListener (this) ;}@ Overridepublic void onAccuracyChanged (Sensor sensor, int accuracy) {}@ Overridepublic void onSensorChanged (SensorEvent event) {// accept the type of the direction Sensor if (event. sensor. getType () = Sensor. TYPE_ORIENTATION) {// here we can get the data and process float x = event as needed. values [SensorManager. DATA_X]; if (Math. abs (x-lastX) & gt; 1.0) {onOrientationListener. onOrientationChanged (x);} lastX = x;} public void setOnOrientationListener (OnOrientationListener onOrientationListener) {this. onOrientationListener = onOrientationListener;} public interface OnOrientationListener {void onOrientationChanged (float x );}}
4. positioning also consumes a lot of power, so we can enable positioning in onStart and disable positioning in onStop ~~ In this way, when the application is minimized, no GPS request is always located. If the user sees that your app has been located, it is estimated that it will be immediately uninstalled ~
@ Override protected void onStart () {// enable Layer positioning mBaiduMap. setMyLocationEnabled (true); if (! MLocClient. isStarted () {mLocClient. start ();} // enable the direction sensor myOrientationListener. start (); super. onStart () ;}@ Overrideprotected void onStop () {// close the layer to locate mBaiduMap. setMyLocationEnabled (false); mLocClient. stop (); // turn off the direction sensor myOrientationListener. stop (); super. onStop ();}

Initialize the direction sensor in onCreate
/*** Initialize the direction sensor */private void initOritationListener () {myOrientationListener = new MyOrientationListener (getApplicationContext (); myOrientationListener. setOnOrientationListener (new OnOrientationListener () {@ Overridepublic void onOrientationChanged (float x) {mXDirection = (int) x; // construct the positioning data MyLocationData locData = new MyLocationData. builder (). accuracy (mCurrentAccracy) // set the direction information obtained by the developer, clockwise 0-360.direction (mXDirection ). latitude (mCurrentLantitude ). longpolling (mcurrentlongpolling ). build (); // you can specify mBaiduMap. setMyLocationData (locData); // set the custom icon mCurrentMarker = BitmapDescriptorFactory. fromResource (R. drawable. navi_map_gps_locked); mBaiduMap. setMyLocationConfigeration (new MyLocationConfiguration (mCurrentMode, true, mCurrentMarker ));}});}

 

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.