【发布时间】:2018-09-05 13:07:55
【问题描述】:
我使用 mapbox 创建了 android 应用;新,我想按用户绘制多边形区域并显示在地图上;
如何在 mapbox 中做到这一点? mapbox.com
【问题讨论】:
标签: java android mapbox polygon
我使用 mapbox 创建了 android 应用;新,我想按用户绘制多边形区域并显示在地图上;
如何在 mapbox 中做到这一点? mapbox.com
【问题讨论】:
标签: java android mapbox polygon
我认为你可以使用这个功能:
活动
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],
]
]
}
}
});
【讨论】:
mapbox-android-sdk:9.5.0中的地图(MapboxMap)对象的可用选项