【问题标题】:get driving distance for a zipcode to multiple zipcodes in v3在 v3 中获取一个邮政编码到多个邮政编码的行驶距离
【发布时间】:2013-01-01 19:17:31
【问题描述】:
        fxnGetNearestDealer: function () {

            //Gets LatLng for given Zipecode and displays the dealer.
            fxnGetLatLngforZip(function () {
                //Gets The Nearest Dealers Index out of dealers in the site.
                fxnGetNearDealerIndex(function (distance) {
                    alert(distance);
                    //Gets Html To Display NearestDealer Details.
                    sHtml = fxnGetHtmlforNearestDealer();

                    //Displays Nearest Dealer Details in the span with Id "spanDNAddr".
                    document.getElementById('spanDNAddr').innerHTML = sHtml;
                });
            });
        };

fxnGetLatLngforZip = function (callback) {
         var oGeocoder = new google.maps.Geocoder(),
             iZipcode = document.getElementById('txtZipCode').value;

         //Boundary checks
         if (!iZipcode) { return; }

         oGeocoder.geocode({ 'address': iZipcode }, function (result, status) {
             if (status == google.maps.GeocoderStatus.OK) {
                 g_oLatLng = result[0].geometry.location;
                 callback();
             }
             else {
                 //Can use status its self to display the error message.
                 document.getElementById('spanDNAddr').innerHTML = "No Results Found Enter Valid ZipCode";
             }
         });
     };

fxnGetNearDealerIndex = function (callback) {
        //Boundary Checks
        if (!g_oLatLng) { return; }

        var oDealerLatlng = null,
            dDistance = null,
            tempindex = null,
            dTemp = null;

        for (var iAddrIdx = 0; iAddrIdx < g_oAddrs.length; iAddrIdx++) {
            oRequest = {
                origin: g_oLatLng,
                destination: new google.maps.LatLng(g_oAddrs[iAddrIdx].latitude, g_oAddrs[iAddrIdx].longitude),
                travelMode: google.maps.TravelMode.DRIVING
            };

            g_oDirections.route(oRequest, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    dDistance = response.routes[0].legs[0].distance.value;
                    if (!dTemp) {
                        dTemp = dDistance;
                    }
                    else if (dDistance < dTemp) {
                        dTemp = dDistance;
                        g_iNearIndex = iAddrIdx;
                    }
                }
            });
        }
        callback(dTemp);
    };

在上面的函数中“fxnGetNearestDealer”是我试图获取给定邮政编码的 latlng 的起点,因为这将是一个异步调用,我使用回调它工作正常..在我必须进行另一个异步调用之后计算给定的邮政编码 latlng 和在 forloop 中迭代的每个 latlng 之间的行驶距离。并获得最小值。我最终像上面那样写了......这个问题它永远不会返回它在警报中给出 null 的任何值。如果我在萤火虫中看到它会迭代所有创建好的请求,但它永远不会进入“g_oDirections.route”函数,因为它是异步的,我使用了回调但它没有工作....任何解决方法请... ...

【问题讨论】:

  • 大多数邮政编码不是点。它们要么是路线要么是多边形,有些在农村地区很大,所以谈论邮政编码之间的距离是没有意义的。您可能需要使用完整地址。
  • @Marcelo,用户要输入邮政编码。我们不能强迫他输入地址。我有目的地地址。你的意思是要传递原点地址而不是 latlng 对象? ,会有什么不同吗..
  • 它不会对您处理异步调用的方式产生任何影响,但是一旦您开始工作,就会更加准确。查看邮政编码可以有多大,以及标记的位置:maps.google.com/…(纬度在山上,离最近的城镇很远)
  • 好的,谢谢,我会尝试做这个改变,意思是你能帮我处理循环内的异步调用......正如我在问题中提到的......
  • 我发布了一个通用示例。请注意,回调是一个单独的函数,(不是匿名的,也不是内联的)。

标签: javascript google-maps-api-3 driving-distance


【解决方案1】:

下面的例子是通用的。它展示了如何从曼哈顿的一个点到多个邮政编码。

var dirService = new google.maps.DirectionsService();

//Hold all destinations in an array
var destinations = ['10001', '10002', '10003'];

//Start from somewhere in Manhattan
var startLocation = new google.maps.latLng(40.769102, -73.971176);



function drivingDistance(start, end) {
var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode.DRIVING
    };

   //Use a separate named function as callback(not anonymous inline)
    dirService.route(request, routeCallback);

}

//callback function
function routeCallback(result, status) {
    if (status == google.maps.DirectionsStatus.OK){

        alert(result.routes[0].legs[0].duration.text);

           //alternative:
           //alert(result.routes[0].legs[0].distance.text);

        //If success then remove the first destination from the array and execute the next request
        destinations.shift();
        getnextRoute();
    }
}

function getnextRoute(){
   if(destinations.length){
        drivingDistance(startLocation, destinations[0]);
  }
}


// Start executing
getnextRoute();

【讨论】:

  • 这里也应该可以使用 DistanceMatrixRequest
  • @Dr.Molle,确实是的,并且代码结构是相同的,尽管我相信 OP 的主要问题是处理多个异步请求。也来自他的另一个问题:stackoverflow.com/questions/14351436/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多