2013-01-18 22 views
-1
 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); 
    }; 

多郵編爲起點從那裏我想獲得經緯度給定郵編因爲這將是一個異步調用,我用了回調它的工作正常..在我必須做另一個異步調用來計算給定的zipcode latlng和forLoop中迭代的每個latlng之間的驅動距離。並獲得最小值。我結束了像上面寫的....它永遠不會返回的問題它在alert中給出null的任何值。如果我在螢火蟲中看到它迭代所有時間請求創建好,但它永遠不會進入「g_oDirections.route」函數,因爲它是異步我使用回調,但它沒有工作....任何工作圍繞PLZ ... ...獲得行駛距離的郵政編碼,以在上述功能「fxnGetNearestDealer」 V3

+1

大多數郵政編碼都不是分數。它們要麼是路線,要麼是多邊形,有些在農村地區是巨大的,所以談論郵政編碼之間的距離是沒有意義的。您可能需要使用完整地址。 – Marcelo

+0

@Marcelo,用戶要輸入郵編。我們不能堅持他輸入地址。我得到了目標地址。你的意思是想傳遞地址來代替latlng對象的原點? ,它會有什麼區別。 – mandava

+0

它將不會對你處理異步調用的方式產生任何影響,但是,一旦你得到它的工作,將會更加準確。查看郵政編碼的大小以及放置標記的位置:https://maps.google.com/maps?q=84647+&hl=zh-CN&ll=39.504041,-111.350555&spn=0.338013,0.727158&sll=37.0625,- 95.677068&sspn = 44.204685,93.076172&hnear = Mt + Pleasant,+ Utah + 84647&t = m&z = 11(latLng在山上,離最近的城鎮很遠) – Marcelo

回答

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

DistanceMatrixRequest也應該可以在這裏 –

+0

@ Dr.Molle,的確,並且代碼結構將是相同的,儘管我相信OP有處理多個異步請求的主要問題。另外從他的另一個問題:http://stackoverflow.com/questions/14351436/returning-latitude-and-longitude-in-google-maps-v3 – Marcelo

相關問題