首页 > 解决方案 > How to "round" GeoJSON coordinates using mapbox

问题描述

I am developing a react-native-mapbox-gl app on Windows 10. I need to "round" GeoJSON points uploaded to the app so that they appear in the center of a house block. For example:

Figure 1: A marker is added on the northeaster corner of a block Figure 1: A marker is added on the northeaster corner of a block

Figure 2: The marker is "rounded" / "moved" to the geographical center of the block Figure 2: The marker is

Since it's unfeasible to manually create a GeoJSON geometry for every street there is, is there a way to achieve this?

标签: androidiosmapboxgeojsonreact-native-mapbox-gl

解决方案


If you encode the boundaries of a particular block as a Polygon feature, you can then use Turf.js's centroid method to calculate the center of said polygon. This will return a coordinate that can be used for the location of the marker.

If you don't have the geographic bounds of these features, one option would be to use the Mapbox Tilequery API to query for all building features within a particular radius, and then create a LatLngBounds object containing these buildings to use as the Polygon defining the boundaries of the block.


推荐阅读