如何在地图框中创建多边形区域

时间:2018-09-05 13:07:56

标签: java android mapbox polygon

我使用mapbox创建了android应用;新增,我想按用户拖动多边形区域并显示在地图上;

如何在mapbox中执行此操作? mapbox.com

3 个答案:

答案 0 :(得分:2)

我认为您可以使用此功能:

活动

private MapView mapView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Mapbox access token is configured here. This needs to be called either in your application
    // object or in the same activity which contains the mapview.
    Mapbox.getInstance(this, getString(R.string.mapbox_token));

    // This contains the MapView in XML and needs to be called after the access token is configured.
    setContentView(R.layout.main_activity);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
      @Override
      public void onMapReady(MapboxMap mapboxMap) {
        drawPolygon(mapboxMap);
      }
    });
  }

函数 drawPolygon:

 private void drawPolygon(MapboxMap mapboxMap) {
    List<LatLng> polygon = new ArrayList<>();
    polygon.add(new LatLng(45.522585, -122.685699));
    polygon.add(new LatLng(45.534611, -122.708873));
    polygon.add(new LatLng(45.530883, -122.678833));
    polygon.add(new LatLng(45.547115, -122.667503));
    polygon.add(new LatLng(45.530643, -122.660121));
    polygon.add(new LatLng(45.533529, -122.636260));
    polygon.add(new LatLng(45.521743, -122.659091));
    polygon.add(new LatLng(45.510677, -122.648792));
    polygon.add(new LatLng(45.515008, -122.664070));
    polygon.add(new LatLng(45.502496, -122.669048));
    polygon.add(new LatLng(45.515369, -122.678489));
    polygon.add(new LatLng(45.506346, -122.702007));
    polygon.add(new LatLng(45.522585, -122.685699));
    mapboxMap.addPolygon(new PolygonOptions()
      .addAll(polygon)
      .fillColor(Color.parseColor("#CD0000")));
  }

希望对您有帮助。

答案 1 :(得分:0)

mapbox api提供了处理地图点击的功能 MapboxMap.setOnMapClickListener(OnMapClickListener)  (https://www.mapbox.com/android-docs/api/map-sdk/6.4.0/) 这将为您提供位置。

我建议您使用这些位置逐步创建折线,一旦用户完成多边形,然后通过关闭形状将折线转换为多边形。

用户交互示例:https://www.mapbox.com/android-docs/maps/examples/#user-interaction

答案 2 :(得分:-1)

this.map.addSource('maine', {
        'type': 'geojson',
        'data': {
                  'type': 'Feature',
                  'geometry': {
                                'type': 'Polygon',
                                'coordinates': [
                                    [
                                      [long1, lat1],
                                      [long2, lat2],
                                      [long3, lat3],
                                    ]
                                  ]
                  } 
        }
      });