2015-10-06 100 views
2

我正在將我的客戶端網站與Google API集成,我有一項任務是使用用戶的英國郵政編碼驗證用戶地址,並獲取地址,我已使用https://ideal-postcodes.co.uk/谷歌地圖API和谷歌地圖位置顯示在PHP中的不同位置

所以,當一個用戶輸入一個郵政編碼時,就會發送一個ajax請求到這個郵政編碼的URL中,並附上用戶輸入的郵政編碼。然後它從該郵編中返回一組地址,用戶選擇其中的一個。

現在,此郵編網址也會返回該特定郵政編碼的經緯度。下一步,我使用了Google Maps API,它顯示了從源到目的地的方向,距離和路線。

這一切都正常,但對於某些郵編,它顯示錯誤的位置。

當我在Google地圖中查看郵編時,它顯示正常;因此不存在長期的經濟問題。有相反的東西,我一直無法找到。

請幫我一把。我嘗試過搜索和使用許多不同的編碼方式,但仍然保持不變。

這是我的JS代碼,其中我計算了多個距離。

var map; 
var latc; 
var lonc; 
var latd; 
var lond; 
var latr; 
var lonr; 
var directionsService; 
var directionsDisplay; 
var orgin; 
var destinations; 
var midway; 
var waypoints = []; 
var service; 
var stat = false; 
function initMap() 
{ 
    map = null; 
    waypoints=[]; 
    directionsDisplay = null; 
    directionsService = null; 
    orgin = ""; 
    destinations = ""; 
    latc = $("#lat").val(); 
    lonc = $("#lon").val(); 
    latd = $("#latd").val(); 
    lond = $("#lond").val(); 
    lonr = $("#lonr").val(); 
    latr = $("#latr").val(); 
    orgin = {lat: parseFloat(latc), lng: parseFloat(lonc)}; 
    if(latr==="0" && lonr==="0") 
    { 
     destinations = {lat: parseFloat(latd), lng: parseFloat(lond)}; 
    } 
    else 
    { 
     stat = true; 
     waypoints.push({ 
      location: new google.maps.LatLng(parseFloat(latd),parseFloat(lonr)), 
      stopover:true 
     }); 
     midway = {lat: parseFloat(latd), lng: parseFloat(lonr)}; 
     destinations = {lat: parseFloat(latr), lng: parseFloat(lonr)}; 
    } 
    console.log(orgin); 
    console.log(destinations); 
    directionsService = new google.maps.DirectionsService; 
    directionsDisplay = new google.maps.DirectionsRenderer; 
    map = new google.maps.Map(document.getElementById('map'), { 
     center: orgin, 
     zoom: 17, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 
    directionsDisplay.setMap(map); 
    calculateAndDisplayRoute(directionsService, directionsDisplay); 
} 
function calculateAndDisplayRoute(directionsService, directionsDisplay) 
{ 
     directionsService.route(
      { 
       origin: orgin, 
       destination: destinations, 
       waypoints: waypoints, 
       durationInTraffic: true, 
       optimizeWaypoints: true, 
       provideRouteAlternatives: true, 
       avoidHighways: false, 
       avoidTolls: false, 
       travelMode: google.maps.TravelMode.DRIVING 
      }, 
      function(response, status) 
      { 
       if (status === google.maps.DirectionsStatus.OK) { 
        directionsDisplay.setDirections(response); 
        distanceandduration(); 
       } 
       else 
       { 
        window.alert('Directions request failed due to ' + status); 
       } 
      } 
     ); 
} 
function distanceandduration() 
{ 
    if(latr==="0" && lonr==="0") 
    { 
     var a = new Array(orgin); 
     var b = new Array(destinations); 
    } 
    else 
    { 
     var a = new Array(orgin,midway); 
     var b = new Array(midway,destinations); 
    } 
    service = new google.maps.DistanceMatrixService(); 
    service.getDistanceMatrix({ 
     origins: a, 
     destinations: b, 
     travelMode: google.maps.TravelMode.DRIVING, 
     unitSystem: google.maps.UnitSystem.METRIC, 
     durationInTraffic: true, 
     avoidHighways: false, 
     avoidTolls: false 
    }, function (response, status) { 
     if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") { 
      var distnumber = []; 
      var durationnumber = []; 
      var dvDistance = document.getElementById("showde"); 
      dvDistance.innerHTML = ""; 
      dvDistance.innerHTML = "<div class='row-fluid'><div class='control-group span12'><div class='controls'>"; 
      for(var i=0;i<response.rows.length;i++) 
      { 
       var distance = ""; 
       var duration = ""; 
       if(i===0) 
       {}else{ 
        dvDistance.innerHTML += "<br/>"; 
       } 
       distnumber[i] = Math.round(parseInt(response.rows[i].elements[i].distance.value)/1000); 
       durationnumber[i] = rectime(response.rows[i].elements[i].duration.value); 
       console.log(response.rows[i].elements[i].duration); 
       distance += response.rows[i].elements[i].distance.text; 
       duration += response.rows[i].elements[i].duration.text; 
       dvDistance.innerHTML += "<h2>Route "+(i+1)+"</h2>"; 
       dvDistance.innerHTML += "Distance: " + distance + "<br />"; 
       dvDistance.innerHTML += "Duration:" + duration; 
       dvDistance.innerHTML += "<hr>"; 
      } 
      dvDistance.innerHTML +="</div></div></div>"; 
      if(latr==="0" && lonr==="0") 
      { 
       enquirydetailreport(distnumber,durationnumber,'oneway'); 
      } 
      else 
      { 
       enquirydetailreport(distnumber,durationnumber,'return'); 
      } 
     } else { 
      alert("Unable to find the distance via road."); 
     } 
    }); 
} 
function rectime(sec) { 
    var hr = Math.floor(sec/3600); 
    var min = Math.floor((sec - (hr * 3600))/60); 
    sec -= ((hr * 3600) + (min * 60)); 
    sec += ''; min += ''; 
    while (min.length < 2) {min = '0' + min;} 
    while (sec.length < 2) {sec = '0' + sec;} 
    hr = (hr)?hr:'00'; 
    return hr+ ':' + min + ':' + sec; 
} 

而且注意,只有幾個特定郵政編碼發生此問題,郵編:

Example : Source : CW10 0QF 
Waypoint : G78 1EP 
Destination : CW10 0QF 

任何幫助表示讚賞。謝謝。

+0

我對你的問題做了一些副本編輯,試圖幫助你得到更好的答案。 – sideshowbarker

回答

0

對不起。我知道了。我剛剛爲來源和目的地錯誤地提供了緯度和長度的值。像源A有latA和lonA,目標B有latB和lonB。但是由於我錯誤地將它寫成了Source A latA和lonA,目標B有latb和lonA->這是我的錯誤。希望這將有助於未來的人。謝謝