Android Baidu Map SDK v3_3_0 (iv)---cover

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/tanzuai/article/details/43833125

features to be implemented in this blog post

first on:

And that's what we're going to achieve in the end.

1. First, let's introduce the following features to be implemented:

A. Changing the location of coordinates

B. Changing the icon for coordinates

C. Deleting icons

D. Coordinates change once every time

Clear. Clarity of the covering

2. Below we pass the example, each resolves the above function

2.1 First we initialize the variables to be used

               /** * Mapview is the map master control */private Mapview mmapview;/** * Baidu map */private baidumap mbaidumap;/** * Marker is indicated as marked */private Marker Mmarkera;private Marker mmarkerb;private Marker mmarkerc;private Marker mmarkerd;//private Marker mmarkere;/** * Information window */PR Ivate Infowindow minfowindow;/** *  Initialize global bitmap information, when not in time recycle */private bitmapdescriptor bda;private Bitmapdescriptor bdb;private bitmapdescriptor bdc;private bitmapdescriptor bdd;private BitmapDescriptor bd;private Bitmapdescriptor Bdground;       /**     * Initialize layout control     */private void Initofflinelayout () {Mmapview = (Mapview) Findviewbyid (R.id.bmapview);    Mbaidumap = Mmapview.getmap ();    Mapstatusupdate MSU = Mapstatusupdatefactory.zoomto (14.0f);    Mbaidumap.setmapstatus (MSU);}
2.2 Initialize the overlay and generate the overlay corresponding behavior, finally set the status of the map

       /** * Initialize overlay */public void Initoverlay () {///(latlng represents coordinates where the first parameter is a dimension, the first parameter is longitude) latlng LlA = new LATLNG (39.9631 75, 116.400244); LATLNG LlB = new Latlng (39.942821, 116.369199); LATLNG LlC = new Latlng (39.939723, 116.425541); LATLNG LlD = new Latlng (39.906965, 116.401394);//latlng lltext = new Latlng (39.86923, 116.397428);//This is where the icon is converted to an object BDA = Bitma Pdescriptorfactory.fromresource (R.drawable.icon_marka); BdB = Bitmapdescriptorfactory.fromresource ( R.DRAWABLE.ICON_MARKB); BdC = Bitmapdescriptorfactory.fromresource (R.DRAWABLE.ICON_MARKC); BdD = Bitmapdescriptorfactory.fromresource (R.DRAWABLE.ICON_MARKD); bd = Bitmapdescriptorfactory.fromresource ( r.drawable.icon_gcoding); bdground = Bitmapdescriptorfactory.fromresource (R.drawable.ground_overlay);// Defines four different types of overlays overlayoptions OoA = new markeroptions (). Position (LlA). Icon (BdA). ZIndex (9). Draggable (true);// Overlayoptions Map Overlay Selection Mmarkera = (Marker) (Mbaidumap.addoverlay (OoA))//addoverlay Add Overlay object Overlayoptions OoB = on the current layer New Markeroptions (). Position (LlB). Icon (BdB). ZIndex (5), Mmarkerb = (Marker) (Mbaidumap.addoverlay (OoB)), overlayoptions OoC = new Markeroptions (). Position (LlC). Icon (BdC). Perspective (False). Anchor (0.5f, 0.5f). Rotate (+). ZIndex (7); Mmarkerc = (Marker) ( Mbaidumap.addoverlay (OoC));//Add a,b,c three coordinates to the list arraylist<bitmapdescriptor> giflist = new arraylist< Bitmapdescriptor> (); Giflist.add (BdA); Giflist.add (BdB); Giflist.add (BdC); overlayoptions OoD = new Markeroptions () . Position (LlD). Icons (giflist). ZIndex (0). period (10);//Every 10 milliseconds change under mark (since the v3.3.0 version, the SDK provides the ability to add animation to marker) Mmarkerd = ( Marker) (Mbaidumap.addoverlay (OoD));//Build Text option object to add text to the map//overlayoptions textoption = new Textoptions ()//. BgC Olor (0XAAFFFF00)//. FontSize (+)//. FontColor (0XFFFF00FF)//. Text ("Baidu Map SDK")//. Rotate ( -30)//. PO  Sition (Lltext); Add the text object on the map and display//mmarkere = (Marker) (Mbaidumap.addoverlay (textoption));//Add ground overlaylatlng southwest = new L ATLNG (39.92235, 116.380338); LATLNG northeast = new Latlng (39.947246, 116.414977); Latlngbounds bounds = new Latlngbounds.builder (). Include (northeast). Include (southwest). Build (); overlayoptions Ooground = new Groundoverlayoptions (). Positionfrombounds (bounds). Image (Bdground). Transparency (0.8f); Mbaidumap.addoverlay (Ooground);//Generate Change map state mapstatusupdate u = mapstatusupdatefactory.newlatlng (Bounds.getCenter ()) //NEWLATLNG set map New center point//set map state mbaidumap.setmapstatus (u);}

2.3 Setting coordinates and monitoring events for the map

       /** * Set coordinates and MAP listener events */private void Initoverlaylistener () {//Set coordinates click event Mbaiduma P.setonmarkerclicklistener (New Onmarkerclicklistener () {public boolean Onmarkerclick (final Marker Marker) {Button    button = New button (Getapplicationcontext ());    Button.setbackgroundresource (R.drawable.popup);    Oninfowindowclicklistener listener = null;    if (marker = = Mmarkera | | marker = = mmarkerd) {button.settext ("change location");    Listener = new Oninfowindowclicklistener () {public void Oninfowindowclick () {latlng LL = marker.getposition (); LATLNG llnew = new Latlng (Ll.latitude + 0.005, ll.longitude + 0.005);//change the dimension of coordinates and longitude marker.setposition (llnew);//Set coordinates    Location Mbaidumap.hideinfowindow ();//Hide message window}};    LATLNG ll = marker.getposition ();    Minfowindow = new Infowindow (Bitmapdescriptorfactory.fromview (button), ll, -47, listener); Mbaidumap.showinfowindow (Minfowindow);//Display the message window} else if (marker = = Mmarkerb) {button.settext ("changeIcon "); Button.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {Marker.seticon (BD);//Change the coordinates of the icon Mbai    Dumap.hideinfowindow ();//Hide Message Window}});    LATLNG ll = marker.getposition (); Minfowindow = new Infowindow (button, ll,-47),//Set message window Mbaidumap.showinfowindow (minfowindow),//Display message window} else if (Marke    r = = Mmarkerc) {button.settext ("delete"); Button.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {marker.remove ();//delete coordinate mbaidumap.    Hideinfowindow ();//Hide Message Window}});    LATLNG ll = marker.getposition (); Minfowindow = new Infowindow (button, ll,-47);//Set message window Mbaidumap.showinfowindow (minfowindow);//Display message window} return True    ;                }    }); Map Click event Mbaidumap.setonmapclicklistener (New Onmapclicklistener () {@Overridepublic Boolean onmappoiclick (Mappoi arg 0) {return false;} @Overridepublic void Onmapclick (latlng arg0) {Mbaidumap.hideinfowindow ();}});}

2.4 Set the purge and reset buttons corresponding events

/** * Clear All overlay *  * @param view */public void Clearoverlay (view view) {mbaidumap.clear ();//Clear all coordinates on map}/** * re-add over Lay *  * @param view */public void Resetoverlay (view view) {Clearoverlay (null); Initoverlay ();}

2.5 to conserve power, set the map's life cycle (remember to recycle the bitmap resource in the OnDestroy () method)

The life cycle of the @Overrideprotected void OnPause () {//Mapview is synchronized with the activity and calls Mapview.onpause () Mmapview.onpause () when the activity is suspended; Super.onpause ();} The life cycle of the @Overrideprotected void Onresume () {//Mapview is synchronized with the activity and calls Mapview.onresume () Mmapview.onresume when activity resumes ( ); Super.onresume ();} The life cycle of the @Overrideprotected void OnDestroy () {//Mapview is synchronized with the activity and calls Mapview.destroy () when the activity is destroyed Mmapview.ondestroy (); Super.ondestroy ();//recovery of bitmap Resources bda.recycle (); bdb.recycle (); bdc.recycle (); bdd.recycle (); bd.recycle (); Bdground.recycle ();}

All right! Introduction Finished! Don't you think it's easy!

The following is the source code of this blog:

Source Code

Android Baidu Map SDK v3_3_0 (iv)---cover

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.