Use of Baidu map API mylocationoverlay (Android)

Source: Internet
Author: User

Http://www.linuxidc.com/Linux/2011-07/38867p2.htm

This article describes how to use mylocationoverlay In the Baidu map API. In the old name, mylocation is interpreted as "My location", while overlay means "layer" or "covering". mylocationoverlay is used to mark its location on the map. It is very similar to itemizedoverlay, except that mylocationoverlay only marks one vertex.

Marking the user's current location on a map is actually a GPS Positioning Application. First, obtain the longitude and latitude of the user's current location through GPS positioning, and then mark the point represented by this latitude and longitude on the map. In fact, in addition to marking our location on a map, we usually need to: "If my location changes, it must be reflected on the map in real time ".
Next we will implement the above functions step by step, mainly through mylocationoverlay combined with locationlistener.
1) Create the 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 to inherit from 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 an activity (inheriting from Com. baidu. mapapi. mapactivity) ** @ author Liufeng * @ date 2011-05-02 */public class mylocationactivity extends mapactivity implements locationlistener {private bmapmanager mapmanager; private mklocationmanager mlocationmanager = NULL; private mapview; private mapcontroller; @ override public void oncreate (bundle SA Vedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // initialize mapactivity mapmanager = new bmapmanager (getapplication (); // set the first parameter of the init Method to the requested API key mapmanager. init ("285b450ebab2a92293e85502150ada7f03c777c4", null); super. initmapactivity (mapmanager); mlocationmanager = mapmanager. getlocationmanager (); // registers the location update event mlocationmanager. requestlocationupdates (this); // use GPS Positioning mlocationmanager. enableprovider (INT) mklocationmanager. mk_gps_provider); mapview = (mapview) findviewbyid (R. id. map_view); // sets the map mode to traffic map mapview. settraffic (true); // set to enable the built-in zoom control mapview. setbuiltinzoomcontrols (true); // construct a latitude and longitude point geopoint point = new geopoint (INT) (26.597239*1e6), (INT) (106.720397*1e6 )); // obtain the map controller object to control mapview mapcontroller = mapview. getcontroller (); // sets the map's center mapcon Troller. setcenter (point); // sets the default map zoom level mapcontroller. setzoom (7); // Add the positioning layer mylocationoverlay = new mylocationoverlay (this, mapview); // register the GPS location update event so that the map can display the current location mylocationoverlay in real time. enablemylocation (); // enable the Magnetic Field Sensor mylocationoverlay. enablecompass (); mapview. getoverlays (). add (mylocationoverlay) ;}@ override protected Boolean isroutedisplayed () {return false ;}@ override protected void ondestroy () {If (mapmanager! = NULL) {mapmanager. Destroy (); 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 to display the current location on the map based on the attributes configured by mylocationoverlay */@ override protected Boolean islocationdisplayed () {return mylocationoverlay. ismylocationenabled ();}/*** this method is triggered when the location changes ** @ Param location current location */@ override public void onlocationchanged (location) {If (location! = NULL) {// convert the current location to the geographic coordinate point final geopoint Pt = new geopoint (INT) (location. getlatitude () * 1000000), (INT) (location. getlongpolling () * 1000000); // sets the current position as the central mapcontroller of the map. setcenter (PT );}}}

Simple explanation: the Code uses mylocationoverlay to mark the current location on the map, implements the listener interface com. Baidu. mapapi. locationlistener, and overwrites its onlocationchanged method to monitor location changes. (Note that the locationlistener is included in the Baidu map API, rather than the built-in Android API)

3) Configure in androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. liufeng. baidumap "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <activity Android: Name = ". mylocationactivity "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" Android. Intent. Category. launcher "/> </intent-filter> </activity> </Application> <uses-SDK Android: minsdkversion =" 4 "/> <! -- Access Network permissions --> <uses-Permission Android: Name = "android. Permission. Internet"/> <! -- Permission for precise access --> <uses-Permission Android: Name = "android. Permission. access_fine_location"/> <! -- Permission to access the network status --> <uses-Permission Android: Name = "android. Permission. access_network_state"/> <! -- Access the WiFi network status --> <uses-Permission Android: Name = "android. Permission. access_wifi_state"/> <! -- Change the permission of the Wi-Fi network --> <uses-Permission Android: Name = "android. Permission. change_wifi_state"/> <! -- Read and write permissions on the memory card --> <uses-Permission Android: Name = "android. Permission. write_external_storage"/> <! -- Permission to read the phone status --> <uses-Permission Android: Name = "android. Permission. read_phone_state"/> </manifest>

At this point, our code compilation has been completed. Next, run it in the simulator to see the effect. It runs in the simulator:

What's going on? It does not mean that the current position can be marked on the map, but it does not seem to have been marked. Yes, it is true that the current position is not marked. Why? As mentioned above, we need to implement a simple Positioning Application that combines GPS with map. Mobile phone GPS Positioning relies on a GPS physical chip on the mobile phone device. It is not achieved by installing a GPS software on the mobile phone. The problem is very obvious, because we are running the above program in the simulator, the simulator may not have a GPS physical chip, of course, it cannot achieve positioning.
Fortunately, ddms provides us with the ability to simulate GPS signals. It can be simulated in three ways: customize longitude and latitude, use GPX files, or kml files (these files are in the format of GPS information records ). In eclipse, click "window"-"Show View"-"other"-"android"-"emulator control" to view the following interface:

This is used to simulate GPS signals. There are three tag items: manual, GPX, and kml, the above three methods correspond to "Custom latitude and longitude", "using GPX file", and "using kml file" to simulate GPS signals respectively. Here we adopt the first method, because it is the simplest, most intuitive, and easy to understand.
Enter a latitude and longitude value (longitude: 106.720397, latitude: 26.597239) and click "send". The simulator will receive this signal, this means that the current location of the simulator device is exactly the location represented by the longitude and latitude we sent, so that the current location can be marked on the map. Is that true? Let's try it out. The input longitude and latitude values are as follows:

Click the send button to check whether the map displayed on the simulator has changed:

We can see that the "current position" has been marked on the map, which is the blue dot. We have not finished our work yet. Please continue.
The first requirement was: "If my location changes, it should be reflected on the map in real time." Do we have implemented this function in our code? Of course. How can we simulate and demonstrate this function? In fact, it is also very simple. We will send another set of different longitude and latitude values to the simulator, which is equivalent to a change in the location of the device. For example, if the longitude and latitude value we resend is "longitude: 106.720397, latitude: 24.597239", let's take a look at the changes in the running results:

We can see that when the position changes, it can always be reflected on the map. All our work is done here.

Note: We recommend that you buy an android real machine for development and testing if you have any requirements, because the simulator cannot simulate all functions on your mobile phone. In many cases, it is very likely that your program has no problem at all, but because the simulator does not support or you do not know how to simulate a function through the simulator, you may repeatedly debug and modify the code, unnecessary waste of time.

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.