Android Baidu map SDK v3_3_0 (4), androidv3_3_0

Source: Internet
Author: User

Android Baidu map SDK v3_3_0 (4), androidv3_3_0

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

Functions to be implemented in this blog

First:

The above is what we will achieve.

1. First, we will introduce the functions to be implemented:

A. Change the coordinates

B. Change the coordinate icon

C. Delete the icon

D. The coordinates change at intervals.

Clear. clear the covering

2. the above functions will be parsed one by one through instances.

2.1 First, initialize the variables to be used.

/*** MapView is the map master control */private MapView mMapView;/*** Baidu map */private BaiduMap mBaiduMap; /*** Marker indicates the mark */private Marker mMarkerA; private Marker mMarkerB; private Marker mMarkerC; private Marker mMarkerD; // private Marker mMarkerE; /***** information window */private InfoWindow mInfoWindow;/***** initialize global bitmap information. when not in use, recycle */private BitmapDescriptor statement; 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 cover, generate the corresponding behavior of the cover, and finally set the MAP status

/*** Initialize the cover */public void initOverlay () {// (LatLng indicates that the first parameter of the coordinate position is the dimension, and the first parameter is the longitude) latLng llA = new LatLng (39.963175, 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); // here the icon is converted to the object Signature = BitmapDescriptorFactory. 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 OverlayOptions ooA = new MarkerOptions (). position (llA ). icon (icon ). zIndex (9 ). draggable (true); // OverlayOptions map covering type selection mMarkerA = (Marker) (mBaiduMap. addOverlay (ooA); // addOverlay adds the OverlayOptions ooB = new MarkerOptions () to the current layer (). 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 (30 ). zIndex (7); mMarkerC = (Marker) (mBaiduMap. addOverlay (ooC); // Add coordinates A, B, and C to the ArrayList <BitmapDescriptor> giflist = new ArrayList <BitmapDescriptor> (); giflist. add (response); giflist. add (bdB); giflist. add (bdC); OverlayOptions ooD = new MarkerOptions (). position (llD ). icons (giflist ). zIndex (0 ). period (10); // tag every 10 milliseconds (since v3.3.0, the SDK provides the ability to add animations to Marker) mMarkerD = (Marker) (mBaiduMap. addOverlay (ooD); // construct a text Option object to add text to a map // OverlayOptions textOption = new TextOptions ()//. bgColor (0xAAFFFF00 )//. fontSize (24 )//. fontColor (0xFFFF00FF )//. text ("Baidu map SDK ")//. rotate (-30 )//. position (llText); // Add the Text object to the map and display it // mMarkerE = (Marker) (mBaiduMap. addOverlay (textOption); // add ground overlayLatLng southwest = new LatLng (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); // generates the map state change MapStatusUpdate u = MapStatusUpdateFactory. newLatLng (bounds. getCenter (); // newLatLng sets the new map center point // sets the MAP status mBaiduMap. setMapStatus (u );}

2.3 set monitoring events for coordinates and maps

/*** Set coordinate and map listening events */private void initOverlayListener () {// set coordinate Click Event mBaiduMap. 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. longpolling + 0.005); // Changes the Coordinate Dimension and longitude marker. setPosition (llNew); // sets the Coordinate Position of the mBaiduMap. hideInfoWindow (); // hide the message window}; LatLng ll = marker. getPosition (); mInfoWindow = new InfoWindow (BitmapDescriptorFactory. fromView (button), ll,-47, listener); mBaiduMap. showInfoWindow (mInfoWindow); // Display message window} else if (marker = mMarkerB) {button. setText ("change icon"); button. setOnClickListener (new OnClickListener () {public void onClick (View v) {marker. setIcon (bd); // The mBaiduMap icon that changes coordinates. hideInfoWindow (); // hide the message window}); LatLng ll = marker. getPosition (); mInfoWindow = new InfoWindow (button, ll,-47); // sets the message window mBaiduMap. showInfoWindow (mInfoWindow); // Display message window} else if (marker = mMarkerC) {button. setText ("delete"); button. setOnClickListener (new OnClickListener () {public void onClick (View v) {marker. remove (); // Delete the coordinate mBaiduMap. hideInfoWindow (); // hide the message window}); LatLng ll = marker. getPosition (); mInfoWindow = new InfoWindow (button, ll,-47); // sets the message window mBaiduMap. showInfoWindow (mInfoWindow); // Display message window} return true ;}}); // map Click Event mBaiduMap. setOnMapClickListener (new OnMapClickListener () {@ Overridepublic boolean onMapPoiClick (MapPoi arg0) {return false ;}@ Overridepublic void onMapClick (LatLng arg0) {mBaiduMap. hideInfoWindow ();}});}

2.4 set the event corresponding to the clear and reset buttons

/*** Clear all Overlay ** @ param view */public void clearOverlay (View view) {mBaiduMap. clear (); // clear all coordinates on the map}/*** re-add Overlay ** @ param view */public void resetOverlay (View view) {clearOverlay (null ); initOverlay ();}

2.5 to save power, set the corresponding map lifecycle (remember to recycle bitmap Resources in the onDestroy () method)

@ Overrideprotected void onPause () {// synchronize the life cycle of MapView with Activity. When activity is suspended, MapView must be called. onPause () mMapView. onPause (); super. onPause () ;}@ Overrideprotected void onResume () {// synchronize the life cycle of MapView with Activity. MapView must be called when activity is restored. onResume () mMapView. onResume (); super. onResume () ;}@ Overrideprotected void onDestroy () {// The Life Cycle of MapView is synchronized with the Activity. When the activity is destroyed, MapView must be called. destroy () mMapView. onDestroy (); super. onDestroy (); // reclaim the bitmap resource token. recycle (); bdB. recycle (); bdC. recycle (); bdD. recycle (); bd. recycle (); bdGround. recycle ();}

Okay! Introduction complete! Is it easy!

The following is the source code of this blog:

Source code

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.