在v3中获得一个邮政编码到多个邮政编码的驾驶距离


        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"是从那里开始的,我试图获得给定zipcode的latlng,因为它将是一个异步调用,我已经使用回调,它工作得很好……之后,我必须进行另一个异步调用来计算给定zipcode latlng和每个latlng在for循环中迭代之间的驾驶距离……并获得最小值。最后我写了....问题是它永远不会返回任何值,它在alert中给出null。如果我在firebug中看到它迭代所有创建好的请求,但它从不进入"g_oDirections"。路由"函数,因为它是异步的,我使用回调,但它没有工作....关于plz的任何工作......

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

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();

最新更新