Android Baidu Map-a map with a known GPS latitude longitude value of one or a group of coverings-OPEN development experience Library-360 Secure Browser 8.1

Source: Internet
Author: User

describe a concept, a map overlay: All overlays or overlays to the map, collectively referred to as map overlays. such as callouts, vector graphic elements (including: Polylines and polygons, and circles), positioning icons, and so on. Overlays have their own geographic coordinates, and when you drag or zoom the map, they move accordingly.

requirements to be fulfilled: if we know the GPS latitude and longitude of the Beijing Tian ' an gate (building): 39.915, 116.404, you want to mark it on the Baidu map.

steps to achieve these requirements:

First, the preparatory work:

1, create Android project, and import Baidu map need to use the jar package and so file.

2, in the Androidmanifest file to add the use of Baidu Map SDK required permissions and screen configuration.

3. Add the Mapview of Baidu map to the layout file layouts.

4. In the subclass that inherits the activity class:

A. Create and initialize the map engine management object;

B. Get the Mapview object that represents the map display view through the component ID and set the appropriate properties.

(example: Enable built-in zoom controls, set allowable map zoom levels, etc.)

c. rewrite the activity's lifecycle callback Method Onresume (), OnPause (), and OnDestroy () to manage the map engine management class objects and display object lifecycles.

second, on the map to mark the Beijing Tian ' an door:

1, want to mark a building on the map, must have a logo it?

Gets the icon object that identifies the building on the map:

       Gets the icon that is used to label a geographic coordinate point on the map        drawable drawable = This.getresources (). getdrawable (R.drawable.icon_marka);

2. Add overlays on the base map (add layer)

A. Write the cover class, define a class yourself, inherit from Itemizedoverlay

Note: Starting with 2.0.0, the SDK does not support direct inheritance of overlay, and users can add overlays by inheriting Itemizedoverlay.

B. In the custom overlay class ( inherited from Itemizedoverlay), declare a collection to hold the cover:

/** Overlay list */        private ArrayList

declaring a variable of type double stores the latitude and longitude values of Beijing Tian ' an gate:

Declaring a variable of type double stores the latitude and longitude value of Beijing Tian ' an gate        private double mLat1 = 39.915;//point1 latitude        private double mLon1 = 116.404;//Point 1 longitude

C. In the constructor, convert the GPS latitude and longitude value to a geographic coordinate point stored as an integer in the form of a micro degree

  */*  NOTE: The parameter list of the Geopoint object construction method: The first is the parameter to indicate latitude, the second is longitude                                    (we are usually by latitude so called, think should be longitude in front of, hehe.)                                      in the online search, the value of the official GPS is latitude and longitude, that is, latitude is in the front, has not been too much attention. */            GeoPoint geoPoint1 = new GeoPoint ((int) (MLAT1 * 1E6), (int) (MLon1 * 1E6));

Constructs a Overlayitem object and adds it to the Moverlaylist collection

Moverlaylist.add (New Overlayitem (GeoPoint1, "Point1", "point1"));

the method that must be called:

                 /* Official Explanation: A tool method that performs all operations on a new itemizedoverlay.             * Do not understand what the meaning, but must call this method, or the program run Error *            /populate ();

d. Returns a Overlayitem object that is removed from the specified list collection.

  /         * Returns a Overlayitem object that is removed from the specified list collection.         * Once the data is in the Moverlaylist set, the method is called in the         constructor of Myoverlayitem before calling it populate ();         */        @Override        protected overlayitem createItem (int index) {            return Moverlaylist.get (index);        }

E. Getting the size of the current overlay list

  @Override public        int size () {            return moverlaylist.size ();        }

complete code for the custom overlay class:

/**     * contains a covering list of coverings class     * @author Android_ls */    final class Myoverlayitem extends Itemizedoverlay

Create an overlay (Myoverlayitem) object and add it to the overlay list:

  Mmapview.getoverlays (). Add (New Myoverlayitem (drawable));

3. Refresh the map

Mmapview.refresh ();

Run as follows:



Full code:

Package Com.android.baidu.map;import Java.util.arraylist;import Android.app.activity;import Android.graphics.drawable.drawable;import Android.os.bundle;import Android.widget.toast;import Com.baidu.mapapi.bmapmanager;import Com.baidu.mapapi.mkgenerallistener;import Com.baidu.mapapi.map.itemizedoverlay;import Com.baidu.mapapi.map.mkevent;import Com.baidu.mapapi.map.mapcontroller;import Com.baidu.mapapi.map.mapview;import Com.baidu.mapapi.map.OverlayItem; Import com.baidu.platform.comapi.basestruct.geopoint;/** * Buildings with known GPS latitude and longitude values on the map * Scenario: If we know the GPS latitude longitude of the Beijing Cheonan (building): 39.915, 116.404, want to mark it on the map. * @author Android_ls * */public class Baidumapoverlayactivity extends Activity {/** Map engine management class */private Bmapmanager MB    Mapmanager = null;    /** Show Map view*/private Mapview mmapview = null;     /** * The study found that when applying for key: The application name must be written MY_APP_ application name (that is, "my_app_" is necessary). * The service provided by the Baidu Map SDK is free and the interface is not limited in usage times.     Before you can use the SDK, you need to apply for a key (key). * */public static final String Baidu_map_key = "07418aec69baab7104c6230a6120C580dffaeebb ";        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Note: Initialize the Bmapmanager object before calling Setcontentview, or you will get an error Mbmapmanager = new Bmapmanager (This.getapplicationcontext ()); Mbmapmanager.init (Baidu_map_key, New Mkgenerallistener () {@Override public void Ongetnetworkstat E (int ierror) {if (Ierror = = Mkevent.error_network_connect) {Toast.maketext (Baidumapov ErlayActivity.this.getApplicationContext (), "Your network is wrong!"                ", Toast.length_long). Show ();  }} @Override public void ongetpermissionstate (int ierror) {if (Ierror = = mkevent.error_permission_denied) {//Authorization key error: Toast.maketext (baidumapoverlayactivi Ty.this.getApplicationContext (), "Please enter the correct authorization in the Demoapplication.java file key!        ",                    Toast.length_long). Show ();        }            }        });        Setcontentview (R.layout.main);        Mmapview = (Mapview) This.findviewbyid (R.id.bmapsview);        Set enable built-in zoom control Mmapview.setbuiltinzoomcontrols (TRUE);        Gets the map controller, which you can use to control panning and zooming mapcontroller Mmapcontroller = Mmapview.getcontroller (); Sets the zoom level of the map.         The range of values for this value is [3,18].        Mmapcontroller.setzoom (13);        Gets the icon that is used to label a geographic coordinate point on the map drawable drawable = this.getresources (). getdrawable (R.drawable.icon_marka);        Create an overlay (Myoverlayitem) object and add it to the overlay list mmapview.getoverlays (). Add (New Myoverlayitem (drawable));    Refresh Map Mmapview.refresh (); }/** * contains a cover list covering class * @author Android_ls */FINAL class Myoverlayitem extends Itemizedoverlay

third, on the map to mark a few points near Beijing Tian ' an gate:

starting with 2.0.0, the SDK does not support direct inheritance of overlay. Displays one or a set of overlays on the map, which can be added by inheriting Itemizedoverlay.

on the basis of the above explanation, we can modify some code of the covering class. Directly on the code:

  Class Myoverlayitem extends Itemizedoverlay

to optimize your code:

gpsponit entity classes:

Package com.android.baidu.map.entity;/**  * Class Name: Ponit.java * Function Description: Store GPS latitude, longitude value * @author android_ls* Date Created: 2013-2-10 Afternoon 07:43:47 * @version V1.0  */public class Gpsponit {    private double mLat;//Latitude    private double mlon;//Longitude 
   
    public Double Getmlat () {        return mLat;    }    public void Setmlat (double mLat) {        This.mlat = mLat;    }    Public double Getmlon () {        return mlon;    }    public void Setmlon (double mlon) {        This.mlon = Mlon;    }    Public Gpsponit (double mLat, double mlon) {        This.mlat = MLat;        This.mlon = Mlon;    }    Public Gpsponit () {    }    @Override the public    String toString () {        return "Ponit [mlat=" + MLat + ", mlon=" + Mlon + "]";    }}
   

The optimized activity class:

Package Com.android.baidu.map;import Java.util.arraylist;import Android.app.activity;import Android.graphics.drawable.drawable;import Android.os.bundle;import Android.widget.toast;import Com.android.baidu.map.entity.gpsponit;import Com.baidu.mapapi.bmapmanager;import Com.baidu.mapapi.mkgenerallistener;import Com.baidu.mapapi.map.itemizedoverlay;import Com.baidu.mapapi.map.mkevent;import Com.baidu.mapapi.map.mapcontroller;import Com.baidu.mapapi.map.MapView; Import Com.baidu.mapapi.map.overlayitem;import com.baidu.platform.comapi.basestruct.geopoint;/** * A set of buildings marked with a known GPS latitude and longitude value on a map * @author Android_ls * */public class Baidumapoverlayitemsactivity extends Activity {/** Map engine management class    */private Bmapmanager mbmapmanager = null;    /** Show Map view*/private Mapview mmapview = null;     /** * The study found that when applying for key: The application name must be written MY_APP_ application name (that is, "my_app_" is necessary). * The service provided by the Baidu Map SDK is free and the interface is not limited in usage times.     Before you can use the SDK, you need to apply for a key (key). * */public static final String Baidu_map_key = "07418aec69baab7104c6230a6120c580dffaEebb ";        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Note: Initialize the Bmapmanager object before calling Setcontentview, or you will get an error Mbmapmanager = new Bmapmanager (This.getapplicationcontext ()); Mbmapmanager.init (Baidu_map_key, New Mkgenerallistener () {@Override public void Ongetnetworkstat E (int ierror) {if (Ierror = = Mkevent.error_network_connect) {Toast.maketext (Baidumapov ErlayItemsActivity.this.getApplicationContext (), "Your network is wrong!"                ", Toast.length_long). Show ();  }} @Override public void ongetpermissionstate (int ierror) {if (Ierror = = mkevent.error_permission_denied) {//Authorization key error: Toast.maketext (BAIDUMAPOVERLAYITEMSA Ctivity.this.getApplicationContext (), "Please enter the correct authorization in the Demoapplication.java file key!     ",                        Toast.length_long). Show ();        }            }        });        Setcontentview (R.layout.main);        Mmapview = (Mapview) This.findviewbyid (R.id.bmapsview);        Set enable built-in zoom control Mmapview.setbuiltinzoomcontrols (TRUE);        Gets the map controller, which you can use to control panning and zooming mapcontroller Mmapcontroller = Mmapview.getcontroller (); Sets the zoom level of the map.         The range of values for this value is [3,18].                Mmapcontroller.setzoom (13);        TODO building a set of data gpsponit GP1 = new Gpsponit (39.90923, 116.397428);        Gpsponit GP2 = new Gpsponit (39.9022, 116.3922);        Gpsponit GP3 = new Gpsponit (39.917723, 116.3722);                Gpsponit GP4 = new Gpsponit (39.915, 116.404);        /** Store GPS latitude, longitude value array */gpsponit[] Mgpsponit = new Gpsponit[4];        Mgpsponit[0] = GP1;        MGPSPONIT[1] = GP2;        MGPSPONIT[2] = GP3;                MGPSPONIT[3] = GP4;        drawable drawable = This.getresources (). getdrawable (R.drawable.icon_marka); Create an overlay (Myoverlayitem) object and add it to the overlayList of Mmapview.getoverlays (). Add (New Myoverlayitem (drawable, mgpsponit));    Refresh Map Mmapview.refresh (); } final class Myoverlayitem extends Itemizedoverlay

Run as follows:

Android Baidu Map-a map with a known GPS latitude longitude value of one or a group of coverings-OPEN development experience Library-360 Secure Browser 8.1

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.