Android Baidu Map SDK v3.3.0 (ii)---Map positioning and layer display

Source: Internet
Author: User

Reprint Please specify Source: http://blog.csdn.net/tanzuai/article/details/43763159in the previous blog, we successfully imported the map into our project. This article we are going to add for the map: first, positioning function; second, layer display, third, combined with the direction of the sensor, the direction of the road by rotating the phone to confirm. With these three features, the map can already serve me!

For convenience, I put all the buttons in the menu.
1. in androidmanifest.xml with a service
   <!--location Service--        <service            android:name= "com.baidu.location.f"            android:enabled= "true"            android:process= ": Remote" >        </service>

2. Initial start-up positioning
    /**     * Initialize location *    /private void Initmylocation () {    //Map Initialization Mbaidumap = Mmapview.getmap (); Mapstatusupdate MSU = Mapstatusupdatefactory.zoomto (15.0f); Mbaidumap.setmapstatus (MSU);//Open location Layer// Mbaidumap.setmylocationenabled (TRUE);//positioning initialization mlocclient = new Locationclient (this); Mlocclient.registerlocationlistener (MyListener); locationclientoption option = new Locationclientoption (); Option.setopengps (true);//Open Gpsoption.setcoortype ("Bd09ll "); Set the coordinate type Option.setscanspan (1000);//Set the time interval for initiating a location request to 1000msoption.setisneedaddress (true);// The returned positioning result contains address information option.setneeddevicedirect (TRUE);//The positioning result returned contains the direction of the phone head mlocclient.setlocoption (option);//Set positioning parameters// Mlocclient.start ();}

3. Then the location of the listener Mylocationlistener:     
    /** * Positioned listener * * @author Tanzuai */public class Myorientationlistener implements SENSOREVENTL Istener {private Context context;private sensormanager sensormanager;private Sensor sensor;private float lastx;private Onorientationlistener Onorientationlistener;public Myorientationlistener (context context) {This.context = context;} Start public void Start () {//Get sensor manager Sensormanager = (sensormanager) context.getsystemservice (Context.sensor_service); if (Sensormanager! = null) {//get direction sensors sensor = sensormanager.getdefaultsensor (sensor.type_orientation);} Register if (sensor! = NULL) {///SensorManager.SENSOR_DELAY_UIsensorManager.registerListener (this, sensor, SENSORMANAGER.SENSOR_DELAY_UI);}} Stop detection public void Stop () {Sensormanager.unregisterlistener (this);} @Overridepublic void onaccuracychanged (sensor sensor, int accuracy) {} @Overridepublic void onsensorchanged (sensorevent Event) {//Accept the type of direction sensor if (event.sensor.getType () = = Sensor.type_orientation) {//Here we can get the data and then handle F as neededLoat x = event.values[sensormanager.data_x];if (Math.Abs (X-LASTX) > 1.0) { Onorientationlistener.onorientationchanged (x);} LASTX = x;}} public void Setonorientationlistener (Onorientationlistener onorientationlistener) {This.onorientationlistener = Onorientationlistener;} public interface Onorientationlistener {void onorientationchanged (float x);}}
4. Positioning is also relatively power consumption, so we in the onstart to open the positioning in the onstop to close the positioning ~ ~ so that the application of the minimum will not always be where the GPS request location, the user if you look at your app has been in that location, estimated to be uninstalled immediately ~
@Override    protected void OnStart () {    //Turn on Layer positioning mbaidumap.setmylocationenabled (true); Mlocclient.isstarted ()) {Mlocclient.start ();} Open Direction sensor Myorientationlistener.start (); Super.onstart ();    }        @Overrideprotected void OnStop () {//Turn off layer positioning mbaidumap.setmylocationenabled (false); Mlocclient.stop ();// Turn off Direction sensor myorientationlistener.stop (); Super.onstop ();}

  initializing direction sensor in OnCreate
      /** * Initialize direction sensor */private void Initoritationlistener () {myorientationlistener = new Myorientationlistener ( Getapplicationcontext ()); Myorientationlistener.setonorientationlistener (new Onorientationlistener () {@ overridepublic void onorientationchanged (float x) {mxdirection = (int) x;//constructs positioning data mylocationdata locdata = new Mylocationd Ata. Builder (). Accuracy (mcurrentaccracy)//Here Set the direction information the developer obtains, clockwise 0-360.direction (mxdirection). Latitude ( Mcurrentlantitude). Longitude (mcurrentlongitude). build ();//Set location data Mbaidumap.setmylocationdata (locdata);// Set Custom Icon Mcurrentmarker = Bitmapdescriptorfactory.fromresource (r.drawable.navi_map_gps_locked); Mbaidumap.setmylocationconfigeration (New Mylocationconfiguration (Mcurrentmode, True, Mcurrentmarker));}    );     

Okay, here we are.code please go to the address below to download:Source Code


Android Baidu Map SDK v3.3.0 (ii)---Map positioning and layer display

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.