1
嗨,我想做什麼似乎幾乎是不可能的......我想獲得三個最近的地點標記對象......這甚至可能嗎? ..它並不一定是100%準確的...但至少做什麼...我發現在網絡上,並試圖是這樣的:Google maps api v2 - 在地圖上找到最近/最近的標記/點數?
function find_closest_marker(lat1, lon1) {
for(var y = 1; y < 4; y++){
var pi = Math.PI;
var R = 6371; //equatorial radius
for(var i=0;i<markers.length; i++) {
//alert(markers.length);
var closest = -1;
var distances = [];
var lat2 = points[i].lat().toFixed(5);
var lon2 = points[i].lng().toFixed(5);
var chLat = lat2-lat1;
var chLon = lon2-lon1;
var dLat = chLat*(pi/180);
var dLon = chLon*(pi/180);
var rLat1 = lat1*(pi/180);
var rLat2 = lat2*(pi/180);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(rLat1) * Math.cos(rLat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
distances[i] = d;
if (closest == -1 || d < distances[closest]) {
closest = i;
}
}
var markerImg = "";
if(markers[closest].markerType == 1){
markerImg="/files/billeder/../Templates/Designs/Ideal2011/images/Map/dot_red.jpg"
} else if(markers[closest].markerType == 2){
markerImg="/files/billeder/../Templates/Designs/Ideal2011/images/Map/dot_lblue.jpg"
}else if(markers[closest].markerType == 3){
markerImg="/files/billeder/../Templates/Designs/Ideal2011/images/Map/dot_dblue.jpg"
}else if(markers[closest].markerType == 4){
markerImg="/files/billeder/../Templates/Designs/Ideal2011/images/Map/dot_green.jpg"
}
$('.nearestPlace'+y).html(
"<img src='"+markerImg+"' alt='"+markers[closest].address+"' />"+
"<div class='CompanyName'><strong>"+markers[closest].title+"</strong></div>"+
markers[closest].address+"<br/>"+
markers[closest].postby+"<br/>"+
"Tlf.: "+markers[closest].phone+"<br/>"+
markers[closest].fax+
markers[closest].web+
markers[closest].email
);
//markers.pop(markers[closest]);
//points.pop(points[closest]);
//markers[closest] = "";
//points[closest]= "";
//alert(closest);
//markers.slice(closest,closest+1);
//points.slice(closest,closest+1);
//alert(markers[closest].title)
delete markers[closest];
delete points[closest]
}
}
如何,你可以從一些評論見代碼我已經嘗試了很多不同的東西!非他們似乎爲我工作!它給出了第一個位置(甚至不是正確的一個),然後就打破了,第二個和第三個都沒做什麼......
任何人都可以在這看到錯誤嗎?或者可能更好地知道或可以寫一個代碼來獲得所需的功能?
lat1,lon1 - 當前點!
我在300不大的區域有300個標記,所以腳本不應該很難找到最近的3個。
我創建了這樣的東西,但在v3和只有一個標記。也許你可以從中獲得一些想法http://galengrover.com/projects/PHP-Google-Maps/examples/markers_find_closest.php – Galen