-1
我试图使用任何代码片段来选择最短的标记到我目前的位置,以便我可以给他/她发送一条消息,但我无法理解他们中的任何一个。 所以我写了我自己的一个,它给了我一个正确的答案,但我担心它是否会用很多标记工作良好。优步示例谷歌地图JS API
那就是:
// data
var json1 = [
{
"id": "34324"
, "title": "Stockholm"
, "lat": 59.3
, "lng": 18.1
, "description": "Stockholm is the capital and the largest city of Sweden"
}
, {
"id": "4343212"
, "title": "Oslo"
, "lat": 59.9
, "lng": 10.8
, "description": "Oslo is a municipality, and the capital and most populous city of Norway"
}
]
var tempArray = json1;
var resultsarray = [];
for (var i = 0; i < tempArray.length; i++) {
var xx = json1[i];
var point = new plugin.google.maps.LatLng(xx.lat, xx.lng);
var distance = plugin.google.maps.geometry.spherical.computeDistanceBetween(MyLocation, point);
resultsarray.push(distance);
};
var min = Math.min.apply(Math, resultsarray);
var b = 0;
for (var e = 0; e < resultsarray.length; e++) {
if (resultsarray[e] == min) {
b = e;
break;
}
};
var theshortestone = tempArray[b];
我怎么样?好吗 ?你怎么看待这件事 ? –
@BishoyWadie我认为这取决于你需要处理的数据量。例如,您可以改善脚本的性能,在您的循环中删除对Google Maps API的调用并改用距离函数,但最终取决于您将如何使用它。 –