在Google地图上查找最近的图钉 [英] Find nearest pins on google map
问题描述
我一直在使用JavaScript Google Maps API构造以下地图: http://springhillfarm. com.au/locations-map/,现在我想在用户在搜索栏中搜索其位置时列出最近的5个图钉.
I have been using the javascript Google Maps API to construct the following map: http://springhillfarm.com.au/locations-map/ and now I would like to list the nearest 5 pins when a user searches their location in the search bar.
尝试进入北埃平"之类的郊区,使用以下功能,地图可以很好地移动:
Try out entering a suburb like "North Epping" and the map will move around well using the following function:
$("#searchclick").click(function(){
var address = document.getElementById('searchbar').value;
var address = address + " Australia";
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude2 = results[0].geometry.location.lat();
var longitude2 = results[0].geometry.location.lng();
var relocate = new google.maps.LatLng(latitude2, longitude2);
map.setCenter(relocate);
map.setZoom(12);
} //when status is OK
}); // geocode
});
但是现在我真的希望它返回最接近的5个引脚.
But now I really want it to return the closest 5 pins.
这在JavaScript中怎么可能?
How possible is this in javascript?
推荐答案
我在您的生产代码中注意到-我可能会添加/应该对其进行重大优化! -您可以使用以下信息找到自己的位置和一堆标记:
I notice in your production code - which I might add could/should be heavily optimised! - you have your location and a bunch of markers using the following:
var myLatlng = new google.maps.LatLng(-37.5759571, 143.8064523);
var marker0 = new google.maps.Marker({ position: new google.maps.LatLng(-37.7994512, 144.9643374), map: map, title:"Toothpicks, 720 Swanston Street Carlton 3052 VIC", icon:image });
var marker1 = new google.maps.Marker({ position: new google.maps.LatLng(-31.9097004, 115.8485327), map: map, title:"Good Life Shop, Shop 7 Dog Swamp Shopping Centre, Yokine WA 6060", icon:image });
等...
如果您在数组中有这些标记,例如:
If you had those markers in an array, eg:
var markers = [ marker0, marker1, etc... ]
一种简单的方法是使用一些毕达哥拉斯来获取您当前位置和所有标记之间的距离.像这样:
a simple approach would be to use a bit of pythagoras to get the distance between your current location and all the markers. Something like:
// make a new array of markers since you probably don't want to modify the existing array
var markersByDistance = [];
for ( var i = 0; i < markers.length; i++ ) {
var marker = markers[i];
// using pythagoras does not take into account curvature,
// but will work fine over small distances.
// you can use more complicated trigonometry to
// take curvature into consideration
var dx = myLatlng.longitude - marker.longitude;
var dy = myLatlng.latitude - marker.latitude;
var distance = Math.sqrt( dx * dx + dy * dy );
markersByDistance[ i ] = marker;
markersByDistance[ i ].distance = distance;
}
// function to sort your data...
function sorter (a,b) {
return a.distance > b.distance ? 1 : -1;
}
// sort the array... now the first 5 elements should be your closest points.
markersByDistance.sort( sorter );
这一切可能毫无意义,因为Google Maps API可能会为您本机执行此操作.
This all may be pointless since Google Maps API might do this natively for you.
这篇关于在Google地图上查找最近的图钉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!