簡體   English   中英

如何在地圖框中創建多邊形區域

[英]How to create a polygon area in mapbox

我使用 mapbox 創建了 android 應用程序; 新的,我想按用戶繪制多邊形區域並顯示在地圖上;

我怎樣才能在 mapbox 中做到這一點? mapbox.com

我想你可以使用這個功能:

活動

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")));
  }

我希望這可以幫助你。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM