【问题标题】:How to draw free hand polygon in Google map in Android?如何在 Android 的 Google 地图中绘制徒手多边形?
【发布时间】:2019-11-22 22:28:11
【问题描述】:

我想在 android studio 上的地图上手绘多边形,但不知道该怎么做 我已经有了地图 ublic 类 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));
}}

【问题讨论】:

  • 看看this问题。

标签: android google-maps polygon draw


【解决方案1】:

如果免提是指用户触摸地图创建的多边形,我建议您拦截地图对象上的触摸或单击事件并获取确切单击位置的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

            }
        });
}}

【讨论】:

    猜你喜欢
    • 2014-09-15
    • 1970-01-01
    • 2020-04-13
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多