在Android Studio中的Google地图中绘制自由手多边形

问题描述 投票:1回答:1

我想在android studio的地图上绘制手绘多边形,但我不知道该怎么办我已经有地图公共类MapsActivity扩展FragmentActivity实现OnMapReadyCallback {

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));
}}
google-maps android-studio polygon draw
1个回答
0
投票
[如果是免手的意思是用户触摸地图创建的多边形,我建议您拦截地图对象上的触摸或单击事件,并获取确切单击位置的LatLng。然后将该点添加到多边形的点列表中。您可以决定:在每次有新的点击或其他想法时重新绘制多边形。

在以下示例中,我拦截点击,并为每次新点击重新绘制具有当前存储点的多边形。我可以决定拥有任意数量的多边形,但现在只需要一个。

@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> latLngs = new ArrayList<>(); // list of polygons final GoogleMap X = this.mMap; // the map this.googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { @Override public void onMapClick(LatLng latLng) { latLngs.add(latLng);//add the point to the list if (polygon != null ) polygon.remove(); // remove the previously drawn polygon polygon = X.addPolygon(new PolygonOptions().addAll(latLngs).fillColor(Color.BLUE).strokeColor(Color.RED));//add new polygon } }); }}

© www.soinside.com 2019 - 2024. All rights reserved.