Android using the Baidu map Api:mylocationoverlay

Source: Internet
Author: User

This article mainly explains the use of Mylocationoverlay in the Baidu Map API. Therefore, the meaning of mylocation Chinese is "my position", while overlay is "layer" or "cover" means, the role of Mylocationoverlay is used to mark the map of their place. It's very similar to using Itemizedoverlay, except that Mylocationoverlay only has one point.

Marking the user's current position on the map is actually a GPS positioning application. First, through GPS positioning to obtain the user's current location of latitude and longitude, and then the latitude represented by the point on the map. In fact, in addition to the map on the location of their own, we usually have such a demand: "If my position changes, to be able to immediately reflect on the map."

Below we will step by step to achieve the above desired functionality, mainly through the mylocationoverlay combined with Locationlistener to achieve.

1 Create a layout file Res/layout/main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=  
"http://schemas.android.com/apk/" Res/android "
    android:orientation=" vertical "
    android:layout_width=" fill_parent "
    android:layout_" height= "Fill_parent"
    >  
    <com.baidu.mapapi.mapview android:id= "@+id/map_view"
        android:layout_ Width= "Fill_parent"
        android:layout_height= "fill_parent"
        android:clickable= "true"
    />  
</ Linearlayout>

2) Create an activity inheritance Com.baidu.mapapi.MapActivity

Package com.liufeng.baidumap;  
Import android.location.Location;  
      
Import Android.os.Bundle;  
Import Com.baidu.mapapi.BMapManager;  
Import Com.baidu.mapapi.GeoPoint;  
Import Com.baidu.mapapi.LocationListener;  
Import Com.baidu.mapapi.MKLocationManager;  
Import com.baidu.mapapi.MapActivity;  
Import Com.baidu.mapapi.MapController;  
Import Com.baidu.mapapi.MapView;  
      
Import Com.baidu.mapapi.MyLocationOverlay; /** * Create activity (inherited com.baidu.mapapi.MapActivity) * * @author Liufeng * @date 2011-05-02/public class Mylocat  
    Ionactivity extends Mapactivity implements Locationlistener {private Bmapmanager mapmanager;  
    Private Mklocationmanager Mlocationmanager = null;  
      
    Private Mylocationoverlay Mylocationoverlay;  
    Private Mapview Mapview;  
      
    Private Mapcontroller Mapcontroller;  
        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview(R.layout.main);  
        Initialize mapactivity Mapmanager = new Bmapmanager (Getapplication ());  
        The first parameter of the Init method is required to fill in the requested API Key Mapmanager.init ("285B415EBAB2A92293E85502150ADA7F03C777C4", null);  
      
        Super.initmapactivity (Mapmanager);  
        Mlocationmanager = Mapmanager.getlocationmanager ();  
        Register Location Update Event mlocationmanager.requestlocationupdates (this);  
      
        Using GPS positioning Mlocationmanager.enableprovider ((int) mklocationmanager.mk_gps_provider);  
        Mapview = (Mapview) Findviewbyid (R.id.map_view);  
        Set map mode for traffic map mapview.settraffic (true);  
      
        Set enable built-in zoom control Mapview.setbuiltinzoomcontrols (TRUE);  
      
        Construct a latitude and longitude point = geopoint dot = new Geopoint ((int) (26.597239 * 1E6), (int) (106.720397 * 1E6));  
        Obtain the Map controller object, which is used to control mapview Mapcontroller = Mapview.getcontroller (); Set the center of the map Mapcontroller.setcenter (point);  
        Set the map default zoom level Mapcontroller.setzoom (7);  
        Add positioning layer Mylocationoverlay = new Mylocationoverlay (this, mapview);  
        Registered GPS Location Update events, so that the map real-time display of the current position mylocationoverlay.enablemylocation ();  
        Open magnetic field sensing sensor mylocationoverlay.enablecompass ();  
    Mapview.getoverlays (). Add (Mylocationoverlay);  
    @Override protected Boolean isroutedisplayed () {return false; } @Override protected void OnDestroy () {if (Mapmanager!= null) {Mapmanager.des  
            Troy ();  
        Mapmanager = null;  
        } Mlocationmanager = null;  
    Super.ondestroy (); } @Override protected void OnPause () {if (Mapmanager!= null) {Mapmanager.stop (  
        );  
    } super.onpause (); } @Override protected void Onresume () {if (Mapmanager!= null) {MapmanAger.start ();  
    } super.onresume (); /** * Determines whether the current position is displayed on the map, based on the properties of mylocationoverlay configuration * * * @Override protected Boolean Islocationd  
    Isplayed () {return mylocationoverlay.ismylocationenabled (); /** * Triggers this method when position changes * * @param location Current position */@Override public void Onlocationchanged (Location Location) {if (Location!= null) {//Convert the current position to a geographical coordinate point FINA  
            L Geopoint pt = new Geopoint ((int) (Location.getlatitude () * 1000000), (int) (Location.getlongitude () * 1000000));  
        Set the current position to the center of the Map Mapcontroller.setcenter (PT); }  
    }  
}

The simple explanation: The code is through the Mylocationoverlay on the map to mark the current location, by implementing the Listener interface Com.baidu.mapapi.LocationListener and rewrites its onlocationchanged method to listen for changes in location. (Note that Locationlistener is in the Baidu Map API, not android itself)

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.