Ø 知识点:地图点击事件---方法重用
Ø 实现详情:绘制点线面时,都要对地图进行点击,新建不同的类实现地图点击接口
** 点击各种按钮(搜索、点、线、面) */ private DrawPointEventListener dPo=new DrawPointEventListener(); private DrawPolylineEventListener dPl=new DrawPolylineEventListener(); private DrawPolygonEventListener dPg=new DrawPolygonEventListener(); @Override public void onClick(View v) { clearGetLoc(); switch (v.getId()){ case R.id.search: search(); break; case R.id.point: aMap.clear(); aMap.setOnMapClickListener(dPo);///设置地图点击监听事件 break; case R.id.polyline: aMap.clear(); aMap.setOnMapClickListener(null); latLngs = new ArrayList<LatLng>(); aMap.setOnMapClickListener(dPl); /*drawPolyLine.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { //长按事件 aMap.addPolyline(new PolylineOptions().addAll(latLngs).width(10).color(Color.argb(255, 1, 1, 1))); return true; } });*/ break; case R.id.polygon: aMap.clear(); aMap.setOnMapClickListener(null); latLngs = new ArrayList<LatLng>(); aMap.setOnMapClickListener(dPg); /* drawPolygon.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { //长按事件 if (latLngs.size() >= 3) { aMap.addPolygon(new PolygonOptions() .addAll(latLngs).strokeWidth(10).strokeColor(Color.argb(255, 1, 1, 1)) .fillColor(Color.LTGRAY)); return true; } else return false; } });*/ break; default: break; } }
public class DrawPointEventListener implements AMap.OnMapClickListener { @Override public void onMapClick(LatLng latLng) { double latitude = latLng.latitude; double longitude = latLng.longitude; MarkerOptions otMarkerOptions = new MarkerOptions(); otMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.gps2)); otMarkerOptions.position(latLng); otMarkerOptions.title("我拖拖拖!"); otMarkerOptions.draggable(true); aMap.addMarker(otMarkerOptions); } } public class DrawPolylineEventListener implements AMap.OnMapClickListener { @Override public void onMapClick(LatLng latLng) { double latitude = latLng.latitude; double longitude = latLng.longitude; MarkerOptions otMarkerOptions = new MarkerOptions(); otMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.gps2)); otMarkerOptions.position(latLng); otMarkerOptions.draggable(true); aMap.addMarker(otMarkerOptions); latLngs.add(new LatLng(latitude,longitude)); if(latLngs.size()>=2){ aMap.addPolyline(new PolylineOptions().addAll(latLngs).width(10).color(Color.argb(255, 1, 1, 1))); } } } public class DrawPolygonEventListener implements AMap.OnMapClickListener { @Override public void onMapClick(LatLng latLng) { double latitude = latLng.latitude; double longitude = latLng.longitude; MarkerOptions otMarkerOptions = new MarkerOptions(); otMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.gps2)); otMarkerOptions.position(latLng); otMarkerOptions.draggable(true); aMap.addMarker(otMarkerOptions); latLngs.add(new LatLng(latitude,longitude)); if (latLngs.size() >= 3) { aMap.addPolygon(new PolygonOptions() .addAll(latLngs).strokeWidth(10).strokeColor(Color.argb(255, 1, 1, 1)) .fillColor(Color.LTGRAY)); } } }