gpt4 book ai didi

javascript - Google Maps API - 获取最接近邮政编码的点

转载 作者:数据小太阳 更新时间:2023-10-29 05:50:14 25 4
gpt4 key购买 nike

我正在使用 Google Maps API v3 和 javascript 将 map 添加到网站。

我有一个地址列表,并已成功将它们绘制在 map 上。当用户输入邮政编码时, map 会重新以他们的位置为中心,显示离该点最近的标记。现在,我需要创建一个 ListView ,显示距离他们的邮政编码最近的 3 或 5 个位置,并提供行车路线链接。我被困住了……欢迎提出建议。

最佳答案

通常的解决方案是使用 google.maps.geometry.spherical library computeDistanceBetween(from:LatLng, to:LatLng, radius?:number) 方法将数字减少到大约 10,然后使用 distance matrix将行驶距离返回到这些位置,以便结果可以按行驶距离(实际行驶距离)排序,并在请求限制内按实际行驶距离减少到最近的 3 到 5 个位置。

example (finds the 3 closest places from a list)(从 FusionTables“披萨店”示例中借用的数据)

  function codeAddress() {
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
if (customerMarker) customerMarker.setMap(null);
customerMarker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
closest = findClosestN(results[0].geometry.location,10);
// get driving distance
closest = closest.splice(0,3);
calculateDistances(results[0].geometry.location, closest,3);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}

function findClosestN(pt,numberOfResults) {
var closest = [];
document.getElementById('info').innerHTML += "processing "+gmarkers.length+"<br>";
for (var i=0; i<gmarkers.length;i++) {
gmarkers[i].distance = google.maps.geometry.spherical.computeDistanceBetween(pt,gmarkers[i].getPosition());
document.getElementById('info').innerHTML += "process "+i+":"+gmarkers[i].getPosition().toUrlValue(6)+":"+gmarkers[i].distance.toFixed(2)+"<br>";
gmarkers[i].setMap(null);
closest.push(gmarkers[i]);
}
closest.sort(sortByDist);
return closest;
}

function sortByDist(a,b) {
return (a.distance- b.distance)
}

function calculateDistances(pt,closest,numberOfResults) {
var service = new google.maps.DistanceMatrixService();
var request = {
origins: [pt],
destinations: [],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
};
for (var i=0; i<closest.length; i++) request.destinations.push(closest[i].getPosition());
service.getDistanceMatrix(request, function (response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var outputDiv = document.getElementById('side_bar');
outputDiv.innerHTML = '';

var results = response.rows[0].elements;
for (var i = 0; i < numberOfResults; i++) {
closest[i].setMap(map);
outputDiv.innerHTML += "<a href='javascript:google.maps.event.trigger(closest["+i+"],\"click\");'>"+closest[i].title + '</a><br>' + closest[i].address+"<br>"
+ results[i].distance.text + ' appoximately '
+ results[i].duration.text + '<br><hr>';
}
}
});
}

example above with "Get Directions" link in the infoWindow

关于javascript - Google Maps API - 获取最接近邮政编码的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17280787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com