我是Android开发的新手。我正在尝试做多个多边形。我做到了,但是第二个多边形从第一个多边形的末端开始。我列出了我关注的问题以及显示我的结果的图像。
How to draw free hand polygon in Google map V2 in Android?
这是我的代码:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private static final LatLng MainLocation = new LatLng(40.828070, -111.904610);
FrameLayout fram_map;
FloatingActionButton fab;
Boolean Is_MAP_Moveable = false;
Projection projection;
public double latitude;
public double longitude;
PolygonOptions rectOptions;
ArrayList < LatLng > val = new ArrayList < > ();
ArrayList < ArrayList < LatLng >> val2 = new ArrayList < > ();
Polygon polygon;
String LogName = "XEL";
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
fram_map = (FrameLayout) findViewById(R.id.fram_map);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Is_MAP_Moveable) {
Is_MAP_Moveable = false;
if (val2.isEmpty()) {
val2.add(0, val);
} else {
val2.add(val2.size(), val);
}
} else {
Is_MAP_Moveable = true;
}
}
});
fram_map.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (Is_MAP_Moveable) {
float x = event.getX();
float y = event.getY();
int x_co = Math.round(x);
int y_co = Math.round(y);
projection = mMap.getProjection();
Point x_y_points = new Point(x_co, y_co);
LatLng latLng = mMap.getProjection().fromScreenLocation(x_y_points);
latitude = latLng.latitude;
longitude = latLng.longitude;
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
val.add(new LatLng(latitude, longitude));
case MotionEvent.ACTION_MOVE:
val.add(new LatLng(latitude, longitude));
case MotionEvent.ACTION_UP:
Draw_Map();
break;
}
} else {
Toast.makeText(MainActivity.this, "Please start the zone", Toast.LENGTH_SHORT).show();
}
return Is_MAP_Moveable;
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
@Override
public void onPolygonClick(Polygon polygon) {
Toast.makeText(MainActivity.this, polygon.getPoints().toString(), Toast.LENGTH_SHORT).show();
}
});
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MainLocation,
20));
}
public void Draw_Map() {
/* for (int i = 0; i < val2.size(); i++) {
rectOptions = new PolygonOptions();
rectOptions.addAll(val2.get(i));
rectOptions.strokeColor(Color.BLUE);
rectOptions.strokeWidth(7);
rectOptions.fillColor(Color.CYAN);
polygon = mMap.addPolygon(rectOptions);
polygon.setClickable(true);
}*/
rectOptions = new PolygonOptions();
rectOptions.addAll(val);
rectOptions.strokeColor(Color.BLUE);
rectOptions.strokeWidth(7);
rectOptions.fillColor(Color.CYAN);
polygon = mMap.addPolygon(rectOptions);
polygon.setClickable(true);
}
}
答案 0 :(得分:0)
从概述上,我可以提出几点建议。如果您执行这些操作,则可能会解决该问题:
现在是否要使用单独的onClick。使用onTouch。
//declare globally
float init_x, init_y;
float tolerance = 5;
fram_map.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
init_x = event.getX();
init_y = event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
float x = event.getX();
float y = event.getY();
Is_MAP_Moveable = Math.abs(init_x - x) > tolerance || Math.abs(init_y - y) > tolerance;
if(!Is_MAP_Moveable) {
int x_co = Math.round(x);
int y_co = Math.round(y);
projection = mMap.getProjection();
Point x_y_points = new Point(x_co, y_co);
LatLng latLng = mMap.getProjection().fromScreenLocation(x_y_points);
latitude = latLng.latitude;
longitude = latLng.longitude;
val.add(new LatLng(latitude, longitude));
Draw_Map();
} else {
Toast.makeText(MainActivity.this, "Please start the zone", Toast.LENGTH_SHORT).show();
}
break;
}
return Is_MAP_Moveable;
}
});
不需要使用LatLng-val2的ArrayList中的ArrayList。使用val绘制多边形。
在合并任何新多边形之前,请清除地图上的所有多边形,因为您要推送ArrayList中的元素。添加新的多边形之前,请先使用polygon.remove();
。