我想在地图上绘制自由手多边形。我从简单的Google地图开始,然后绘制多边形,它可以正常工作,但现在我正在寻找用户如何通过单击地图上的点并在地图上的点中间拉伸标记来绘制多边形多边形。
现在我的多边形地图看起来像:
我想实现:
这是我的代码:
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
Button save_field;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
// Retrieve the content view that renders the map.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
FrameLayout Frame_map = (FrameLayout) findViewById(R.id.frame_map);
Button btn_draw_State = (Button) findViewById(R.id.btn_draw_State);
final Boolean[] Is_MAP_Moveable = {false}; // to detect map is movable
// Button will change Map movable state
btn_draw_State.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Is_MAP_Moveable[0] = !Is_MAP_Moveable[0];
}
});
}
public GoogleMap getmMap() {
return mMap;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
/*polygon should be declared as member of the fragment class if you want just one polygon at a time*/
final List<LatLng> latLngList = new ArrayList<>(); // list of polygons
final List<Marker> markerList = new ArrayList<>();
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(final LatLng latLng) {
MarkerOptions markerOptions = new MarkerOptions(); //create marker options
markerOptions.position(latLng);
markerOptions.title(latLng.latitude + ":" + latLng.longitude);
mMap.clear();
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
Marker marker = mMap.addMarker(markerOptions);
latLngList.add(latLng);
markerList.add(marker);
Polygon polygon = null;
if (polygon != null ) polygon.remove(); // remove the previously drawn polygon
PolygonOptions polygonOptions = new PolygonOptions().addAll(latLngList).clickable(true);
polygon = mMap.addPolygon(new PolygonOptions().addAll(latLngList).fillColor(Color.BLUE).strokeColor(Color.RED));//add new polygon
}
});
save_field = findViewById(R.id.save);
save_field.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MapActivity.this, Save_Fields.class));
finish();
}
});
}
}
我已经在这个主题上做了大量的研究和开发,但是并没有在Google Maps中实现这种事情的完美方法。如果有人知道该方法,请帮助我找出解决方案。先感谢您:)
error: package rx.functions does not exist
此错误