【发布时间】:2019-02-19 14:30:05
【问题描述】:
我已经用<Drawing Manager /> 实现了react-google-maps。
不确定如何使用它从onPolylineComplete 返回的对象。似乎也没有记录。我想检查该区域中出现的其他一些位置 - 如果它们出现,我可以填充标记。
所以流程是:
- 用户应该在一个区域周围画一条线(例如,伦敦芬斯伯里马戏团)。
- 这调用
fetchNewsUpdateMarkers() - 调用 API 的对象,每个对象都有 lat 和一个 lng 值。
3.5 我通过它们进行映射,它们的 lat-lng 值被转换为 google
LatLng对象和... - 我运行
containsLocation()并将true返回到markers数组。
所以这一切都是为了从我的 onPolylineComplete 返回的 obj 中获取一组多边形区域坐标 ????
代码:
<DrawingManager
defaultOptions={{
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.POLYLINE,
],
},
}}
onPolylineComplete={(obj) => props.fetchNewsUpdateMarkers(obj)};
/>
然后……
fetchNewsUpdateMarkers: (obj) => {
console.log(obj); // <-- this logs out as below
let markers = [];
let searchArea = new google.maps.Polygon({ paths: polygonCoords }); // <--that I hope to get from 'obj' somehow!
let arrayOfAPIlocations = [
{ lat: 51.518420, lng: -0.088690 },
{ lat: 51.518110, lng: -0.087970 },
{ lat: 51.517601, lng: -0.180250 }
];
// ^^ only the *last* obj in this array is West London.
// The others are in the same district of East London.
// So maybe something like:
arrayOfAPIlocations.map(each => {
let convertedLatLng = new google.maps.LatLng(each.lat, each.lng);
if (google.maps.geometry.poly.containsLocation(convertedLatLng, searchArea)) {
markers.push(each);
}
return markers;
});
}
这是目前从 'obj' 中出来的 ????????:
【问题讨论】:
-
onPolygonComplete 返回一个Polygon,它有一个
getPaths()方法。
标签: javascript reactjs google-maps react-google-maps