Android AMAP: Multiple Marker + custom pop-up window InfoWindow + anti-encoding Coordinate Transfer address, markerinfowindow

Source: Internet
Author: User

Android AMAP: Multiple Marker + custom pop-up window InfoWindow + anti-encoding Coordinate Transfer address, markerinfowindow

1,

Currently, the latest jar: AMap_Search_V5.7.0_20171229.jar can be downloaded from the official website.

2. Create an object class, which contains four parameters used to describe vertex information-MarketBean

Public class MarketBean {/***** latitude */private double latitude;/***** longitude */private double longpolling;/***** title */private String; /*** content */private String content; public double getLatitude () {return latitude;} public void setLatitude (double latitude) {this. latitude = latitude;} public double getlongpolling () {return longpolling;} public void setlongpolling (double longpolling) {this. longpolling = longpolling;} public String get () {return;} public void set (String) {this. =;} public String getContent () {return content;} public void setContent (String content) {this. content = content;} public MarketBean (double latitude, double longpolling, String, String content) {this. latitude = latitude; this. longpolling = longpolling; this. =; this. content = content ;}}

It can be called during initialization.

/*** Add multiple markets */private List
 
  
MarketList; private void addMoreMarket () {if (marketList = null) {marketList = new ArrayList <> () ;}// simulate 6 false data marketlists. add (new MarketBean (22.675, 114.028, "Title 1", "content 1"); marketList. add (new MarketBean (22.694, 114.099, "title 2", "content 2"); marketList. add (new MarketBean (22.667, 114.041, "Title 3", "content 3"); marketList. add (new MarketBean (22.647, 114.023, "Title 4", "content 4"); marketList. add (new MarketBean (22.688, 114.066, "Title 5", "content 5"); marketList. add (new MarketBean (22.635, 114.077, "Title 6", "content 6"); for (int I = 0; I <marketList. size (); I ++) {aMap. addMarker (new MarkerOptions (). anchor (1.5f, 3.5f ). position (new LatLng (marketList. get (I ). getLatitude (), // set the latitude marketList. get (I ). getlongpolling () // sets the longitude. (marketList. get (I ). get () // set the title. snippet (marketList. get (I ). getContent () // set content //. setFlat (true) // set the Marker to show the location. You can view the effect on the double-finger drop-down map. draggable (true) // set the Marker to be dragged. icon (BitmapDescriptorFactory. fromResource (R. mipmap. ic_launcher);} // sets the custom pop-up window aMap. setInfoWindowAdapter (new WindowAdapter (this); // bind the Information Window and click event aMap. setOnInfoWindowClickListener (new WindowAdapter (this); aMap. setOnMarkerClickListener (new WindowAdapter (this ));}
 

Custom pop-up window (all content updates after custom InfoWindow need to be completed by the user)-WindowAdapter

Public class WindowAdapter implements AMap. infoWindowAdapter, AMap. onMarkerClickListener, AMap. onInfoWindowClickListener {private Context context; private static final String TAG = "WindowAdapter"; public WindowAdapter (Context context) {this. context = context ;}@ Override public View getInfoWindow (Marker marker) {// View of the associated layout = LayoutInflater. from (context ). inflate (R. layout. layout_info_item, null); // Title TextView = (TextView) view. findViewById (R.id.info _); // address information TextView address = (TextView) view. findViewById (R.id.info _ address); // latitude TextView latitude = (TextView) view. findViewById (R.id.info _ latitude); // longitude TextView longpolling = (TextView) view. findViewById (R.id.info _ longpolling );. setText (marker. get (); address. setText (marker. getSnippet (); latitude. setText (marker. getPosition (). latitude + ""); longpolling. setText (marker. getPosition (). longpolling + ""); Log. e (TAG, "getInfoWindow1:" + marker. get (); Log. e (TAG, "getInfoWindow:" + marker. getSnippet (); Log. e (TAG, "getInfoWindow:" + marker. getPosition (). latitude); Log. e (TAG, "getInfoWindow:" + marker. getPosition (). longpolling); return view;} // if you use a custom layout, you do not need to use this method. return null @ Override public View getInfoContents (Marker marker) {return null ;} // interface called back when the marker object is clicked // return true indicates that the interface has responded to the event; otherwise, false @ Override public boolean onMarkerClick (Marker marker) {Log. e (TAG, "Marker clicked"); return false;} // bind Information Window Click Event @ Override public void onInfoWindowClick (Marker marker) {Log. e (TAG, "InfoWindow clicked ");}}

Layout file-layout_info_item

 
     
      
       
        
     
    
   
  
 

Note that you must set the background style (color and image) of InfoWindow. Otherwise, the default background of autonavi is used.

Info_shape:

 
     
      
       
    
   
  
 

Line_shape:

 
     
      
   
  
 

3. uncode coordinates and address translation-(retrieve the current location information from any point on the mobile map)

Map slide listening event:

// Sliding listener aMap. setOnCameraChangeListener (new AMap. onCameraChangeListener () {// latitude and longitude @ Override public void onCameraChange (CameraPosition cameraPosition) {// longitude double longdistance = cameraPosition.tar get. longpolling; // latitude double latitude = cameraPosition.tar get. latitude; Log. e (TAG, "onCameraChange sliding:" + longpolling); Log. e (TAG, "onCameraChange slide:" + latitude);} // latitude and longitude at the end of the slide @ Override public void onCameraChangeFinish (CameraPosition cameraPosition) {// longitude double longpolling = cameraPosition.tar get. longpolling; // latitude double latitude = cameraPosition.tar get. latitude; // the longitude and latitude coordinates at the end of the input. getAddressByLatLonPoint (new LatLonPoint (latitude, longpolling); Log. e (TAG, "onCameraChangeFinish sliding end:" + longpolling); Log. e (TAG, "onCameraChangeFinish slide end:" + latitude );}});} /*** uncode coordinates to address * RegeocodeQuery [Coordinate to address] * GeocodeQuery [Address to coordinate] */private void getAddressByLatLonPoint (final LatLonPoint latLonPoint) {GeocodeSearch geocodeSearch = new GeocodeSearch (this); // a parameter indicates the latitude and longitude, And a two parameter indicates the range of meters, the three parameters indicate the map coordinate system or the GPS coordinate system RegeocodeQuery regeocodeQuery = new RegeocodeQuery (latLonPoint, 2000, GeocodeSearch. AMAP); geocodeSearch. getFromLocationAsyn (regeocodeQuery); // address indicates the address, and the second parameter indicates the city to be queried. You can search both Chinese and Chinese. citycode and adcode are OK GeocodeQuery query = new GeocodeQuery ("Ganzhou City ", "Ganzhou City"); geocodeSearch. getFromLocationNameAsyn (query); // listens to geocodeSearch for the results of the inverse geographic code. setOnGeocodeSearchListener (new GeocodeSearch. onGeocodeSearchListener () {@ Override public void onRegeocodeSearched (RegeocodeResult regeocodeResult, int I) {// if I = 1000) {RegeocodeAddress regeocodeAddress = success. getRegeocodeAddress (); // the location of the current map center (for other information, see regeocodeAddress.) String formatAddress = regeocodeAddress. getFormatAddress (); address. setText (formatAddress); Log. e (TAG, "onCameraChange address for latitude/longitude conversion:" + formatAddress);} else {Log. e (TAG, "No information here...: ") ;}@ Override public void onGeocodeSearched (GeocodeResult geocodeResult, int I) {if (I = 1000) {Log. e (TAG, "onCameraChange query address:" + geocodeResult. getGeocodeQuery (). getLocationName (); Log. e (TAG, "City of onCameraChange query:" + geocodeResult. getGeocodeQuery (). getCity (); Log. e (TAG, "latitude of onCameraChange query:" + geocodeResult. getGeocodeAddressList (). get (0 ). getLatLonPoint (). getLatitude (); Log. e (TAG, "onCameraChange query longitude:" + geocodeResult. getGeocodeAddressList (). get (0 ). getLatLonPoint (). getlongpolling ();} else {Log. e (TAG, "query failed... :");}}});}

Address. setText (formatAddress); add a TextView in the layout and initialize it.

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.