计算行驶距离 Google Maps API

发布于 2024-10-31 19:30:24 字数 2569 浏览 0 评论 0原文

对于用户输入地址的网站,我试图找到距离他最近的位置,用户可以在其中提取订购的商品。

根据用户的地址,我可以将可能的接送地点缩小到 2 到 5 个之间。因此,我想计算用户的地址(A 点)与可能的接送地点之间的距离。

此处的演示只需两个地址即可正常工作。我已尽可能地调整代码以处理两个以上的地址。我在此处发布了我的JS代码,因为我似乎无法在SO中正确格式化它。

代码中有两个警报。第一个警报正确显示不同的接载位置。但第二个警报始终显示最后的上车位置。

谁能解释为什么?


HTML:

<p id="hello">Hello World</p>

JavaScript:

var geocoder, location1, location2, gDir;

function initialize(counter) {    
    if( counter == 0 ){
        geocoder = new GClientGeocoder();
        gDir = new GDirections();
    }
    GEvent.addListener(gDir, "load", function() {
        var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
        var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
        $("#results").append('<strong>Driving Distance: </strong>' + drivingDistanceKilometers + ' kilometers<br /><br />');        
    });
}

function getDistance(agency_add, counter) {
    initialize(counter);
    geocoder.getLocations(agency_add, function (response) {    
        if (!response || response.Status.code != 200) {
            alert("Sorry, we were unable to geocode the address" + agency_add);
        }
        else {
            location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
            //alert("ONE: "+location1.address);
            geocoder.getLocations(document.forms[0].address1.value, function (response) {
                //alert("TWO: "+location1.address);
                if (!response || response.Status.code != 200) {alert("Sorry, we were unable to geocode the second address");}
                else {                        
                    location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};                    
                    gDir.load('from: ' + location1.address + ' to: ' + location2.address);
                }
            });
        }
    });
}


$(document).ready(function(){
    //put each agency address in an array
    var agencies = [];    
    $(".agency_field").each(function(index) {
        agencies.push($(this).val());
    });

    for (var i = 0; i < agencies.length; i++){
        var res = getDistance(agencies[i], i);
    }    
});

For a website where a user enters his address, I'm trying to find the location closest to him where the user can collect the ordered goods.

Based on the user's address I can narrow down the possible pick up locations to between 2 and 5. So I'd like to calculate the distance between user's address (point A) and the possible pick up locations.

The demo here works fine with just two addresses. I've adapted the code as much as I can to work with more than two addresses. I posted my JS code here since I can't seem to properly format it in SO.

In the code are two alerts. The first alert correctly shows the different pick up locations. But the second alert always shows the LAST pickup location.

Can anyone explain why?


HTML:

<p id="hello">Hello World</p>

JavaScript:

var geocoder, location1, location2, gDir;

function initialize(counter) {    
    if( counter == 0 ){
        geocoder = new GClientGeocoder();
        gDir = new GDirections();
    }
    GEvent.addListener(gDir, "load", function() {
        var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
        var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
        $("#results").append('<strong>Driving Distance: </strong>' + drivingDistanceKilometers + ' kilometers<br /><br />');        
    });
}

function getDistance(agency_add, counter) {
    initialize(counter);
    geocoder.getLocations(agency_add, function (response) {    
        if (!response || response.Status.code != 200) {
            alert("Sorry, we were unable to geocode the address" + agency_add);
        }
        else {
            location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
            //alert("ONE: "+location1.address);
            geocoder.getLocations(document.forms[0].address1.value, function (response) {
                //alert("TWO: "+location1.address);
                if (!response || response.Status.code != 200) {alert("Sorry, we were unable to geocode the second address");}
                else {                        
                    location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};                    
                    gDir.load('from: ' + location1.address + ' to: ' + location2.address);
                }
            });
        }
    });
}


$(document).ready(function(){
    //put each agency address in an array
    var agencies = [];    
    $(".agency_field").each(function(index) {
        agencies.push($(this).val());
    });

    for (var i = 0; i < agencies.length; i++){
        var res = getDistance(agencies[i], i);
    }    
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ花落谁相伴 2024-11-07 19:30:24

您在循环内调用 geocoder.getLocations 。 geocoder.getLocations 异步运行。当它在处理第一个请求的同时收到第二个请求时,它会取消第一个请求。

如果您想要多线程 geocoder.getLocations 您需要创建它的多个实例。

you are calling geocoder.getLocations inside a loop. geocoder.getLocations runs asynchronously. when it receives the 2nd request while still processing the first, it cancels the first request.

If you want to multi-thread geocoder.getLocations you need to create multiple instances of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文