Ron*_*vdb 1 javascript polygon google-maps-api-3
我已经使用 Google Maps API 基于 lng/lat 坐标数组绘制多边形,但我需要一种方法来获取多边形的长度(以米为单位),这可以使用 Google Maps API 吗?
我无法使用 DirectionService 来绘制路线并以这种方式获取实例,因为部分路径是越野的,并且在使用 DirectionService 时会更改路径:(
这就是我创建多边形的方法:
var poly = new google.maps.Polygon({
paths: waypoints,
strokeColor: '#00FF00',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#00FF00',
fillOpacity: 0
});
poly.setMap(map);
Run Code Online (Sandbox Code Playgroud)
其中 waypoints 是这样的对象数组:
[
{lat: 25.774, lng: -80.190},
{lat: 18.466, lng: -66.118},
{lat: 32.321, lng: -64.757},
{lat: 25.774, lng: -80.190}
]
Run Code Online (Sandbox Code Playgroud)
为此,您可以使用几何库,您需要通过以下方式将其包含在 API 调用中:
https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&libraries=geometry
Run Code Online (Sandbox Code Playgroud)
要获取 Polygon 的路径,请使用waypoints数组或从 Polygon 对象获取它:
var path = poly.getPath(); // To retrieve the first path or
var paths = poly.getPaths(); // To retrieve all the paths
Run Code Online (Sandbox Code Playgroud)
然后将其传递给 GeometrycomputeLength函数:
var length = google.maps.geometry.spherical.computeLength(path);
Run Code Online (Sandbox Code Playgroud)
该函数将为您提供以米为单位的路径长度。
如果您的多边形有多个路径,请确保您获得适当的路径。
| 归档时间: |
|
| 查看次数: |
1358 次 |
| 最近记录: |