Custom marker and infoWindow form
1 shows the layout of the map xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
2. the Activity and custom marker InfoWindow (code comments detail)
/ ** * the Created by YyyyQ ON 2020/3/24 * / public class the MainActivity the extends AppCompatActivity the implements AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnMapClickListener { Private the MapView the mapView = null ; Private amap amap; Private UiSettings UiSettings; // store All points of latitude and longitude LatLngBounds.Builder boundsBuilder = new new LatLngBounds.Builder (); // current click marker Private marker clickMaker; // custom form View infoWindow = null ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mapView = findViewById(R.id.map); mapView.onCreate(savedInstanceState); if (aMap == null) { aMap = mapView.getMap(); uiSettings = aMap.getUiSettings(); //设置地图属性 setMapAttribute(); } //模拟数据源 List<Map<String, String>> list = getData(); //Through the bad add custom marker on the map for ( int I = 0; I <list.size (); I ++ ) { of LatLng latLng = new new of LatLng (Double.parseDouble (List.get (I) .get ( "Latitude") ), Double.parseDouble (List.get (I) .get ( "longitude" ))); the MarkerOptions markerOption = new new the MarkerOptions (); markerOption.position (latLng); markerOption.title (List.get (I) .get ( "title" )); markerOption.snippet (list.get (i) .get ( "Content" )); // custom point mark icon picture file icon for the next drawable markerOption.icon (BitmapDescriptorFactory.fromBitmap (BitmapFactory.decodeResource (getResources (), R.drawable.ditu_yiliao))); markerOption.draggable ( to false ); aMap.addMarker (markerOption); // all marker include latitude and longitude to the boundsBuilder boundsBuilder .include (latLng); } // update state map aMap.animateCamera (CameraUpdateFactory.newLatLngBounds (boundsBuilder.build (), 10 )); } / ** * set the map attribute * / Private void setMapAttribute () { // set default zoom level aMap.moveCamera (CameraUpdateFactory.zoomTo (12)); // hide the lower right corner of the zoom button uiSettings.setZoomControlsEnabled ( false ); // set the marker click event listener aMap.setOnMarkerClickListener ( the this ); // set a custom message window aMap.setInfoWindowAdapter ( the this ); // Set the map click event listener aMap.setOnMapClickListener ( the this ); } / ** * analog data source * / Private List <the Map <String, String >> the getData () { List <the Map <String, String List >> = new new the ArrayList <> ( ); for (int i = 1; i < 11; i++) { Map<String, String> map = new HashMap<>(); map.put("title", "标题NO." + i); map.put("content", "这是第" + i + "个marker的内容"); map.put("latitude", (43 + Math.random() + "").substring(0, 9)); map.put("longitude", (125 + Math.random() + "").substring(0, 10)); list.add(map); } return list; booleanpublic @Override* / * marker click event/ ** } onMarkerClick (Marker marker) { clickMaker = marker; // click on the marker to show the current custom form marker.showInfoWindow (); // Returns true if the interface has responded thing, no need to continue the transfer return true ; } / ** * Custom monitor infoWindow window event callback * / @Override public View getInfoWindow (marker marker) { IF (infoWindow == null ) { infoWindow = LayoutInflater.from ( the this ) .inflate (R.layout.amap_info_window, null ); } the render (marker, infoWindow ); returninfoWindow; } / ** * custom infoWindow window * / Private void the render (Marker marker, View infoWindow) { the TextView title = infoWindow.findViewById (R.id.info_window_title); the TextView Content = infoWindow.findViewById (R.id.info_window_content ); title.setText (marker.getTitle ()); content.setText (marker.getSnippet ()); } / ** * not modify the entire background and border InfoWindow, return null * / @Override public View getInfoContents (marker marker ) { return null ; } / ** * Click on the event map * Click on the map to show the area so that the current form of hidden * / @Override public void onMapClick (of LatLng latLng) { // determine whether the current marker information window displays IF (clickMaker! = Null && clickMaker.isInfoWindowShown ()) { clickMaker.hideInfoWindow (); } } }
3. Custom form layout infoWindow
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/infowindow" android:orientation="vertical"> <!--@drawable/infowindow为.9图--> <TextView android:id="@+id/info_window_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:textColor="#333" android:textSize="14sp" /> <TextView android:id="@+id/info_window_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:textColor="#333" android:textSize="12sp" /> </LinearLayout>