如何在Android的Google地图中绘制徒手多边形?
我想在android studio的地图上画一个徒手多边形,但我不知道该怎么做 我已经有地图了 公共类MapsActivity扩展了FragmentActivity在MapreadyCallback上的实现{如何在Android的Google地图中绘制徒手多边形?,android,google-maps,polygon,draw,Android,Google Maps,Polygon,Draw,我想在android studio的地图上画一个徒手多边形,但我不知道该怎么做 我已经有地图了 公共类MapsActivity扩展了FragmentActivity在MapreadyCallback上的实现{ private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVie
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}}
如果“免提”是指用户触摸地图创建的多边形,我建议您拦截地图对象上的触摸或单击事件,并获取准确单击位置的
LatLng
。然后将该点添加到多边形的点列表中。您可以决定:每次有新的单击或单击时重新绘制多边形你脑子里还想着别的事情
在下面的示例中,我截取单击并为每次新单击使用当前存储的点重新绘制多边形。我可以决定拥有任意数量的多边形,但我现在只想要一个
@覆盖
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
/*如果一次只需要一个多边形,则应将多边形声明为fragment类的成员*/
最终列表latLngs=new ArrayList();//多边形列表
final GoogleMap X=this.mMap;//地图
this.googleMap.setOnMapClickListener(新的googleMap.OnMapClickListener(){
@凌驾
公共无效的点击(LatLng LatLng){
latLngs.add(latLng);//将点添加到列表中
if(polygon!=null)polygon.remove();//删除以前绘制的多边形
polygon=X.addPolygon(new polygonooptions().addAll(latLngs).fillColor(Color.BLUE).strokeColor(Color.RED));//添加新多边形
}
});
}}
如果免提指的是用户触摸地图创建的多边形,我建议您截取地图对象上的触摸或单击事件,并获取准确单击位置的LatLng
。然后将该点添加到多边形的点列表中。你可以做任何决定:每当你脑海中出现新的点击或其他事情时,重新绘制多边形
在下面的示例中,我截取单击并在每次新单击时使用当前存储的点重新绘制多边形。我可以决定拥有任意多个多边形,但我现在只想要一个
@覆盖
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
/*如果一次只需要一个多边形,则应将多边形声明为fragment类的成员*/
最终列表latLngs=new ArrayList();//多边形列表
final GoogleMap X=this.mMap;//地图
this.googleMap.setOnMapClickListener(新的googleMap.OnMapClickListener(){
@凌驾
公共无效的点击(LatLng LatLng){
latLngs.add(latLng);//将点添加到列表中
if(polygon!=null)polygon.remove();//删除以前绘制的多边形
polygon=X.addPolygon(new polygonooptions().addAll(latLngs).fillColor(Color.BLUE).strokeColor(Color.RED));//添加新多边形
}
});
}}
看一看问题。看一看问题。