2013-11-28 235 views
0

我一直在嘗試將v2轉換爲v3地圖應用程序,該應用程序計算輸入的地址與存儲的經緯度之間的距離。沒有任何運氣來啓動和運行。真的很感謝一些幫助。谷歌地圖api v2到v3

<script type="text/javascript" src="js/addHTMLControls2.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#order").validate(); 
    addInput(); 
}); 
</script> 

<script type="text/javascript"> 

var geocoder, location1, location2; 

function initialize() { 
    geocoder = new google.maps.Geocoder(); 
} 




function showLocation() { 
      document.getElementById('address').value=  document.getElementById('address2').value; 


    /*coordinate system*/ 

geocoder.geocode(document.forms[0].address1.value, 
function (response) { 
if (!response || response.Status.code != 200) 
    {alert("Sorry, we were unable to geocode the first address");} 
else 
    {location1 = 
    {lat: response.Placemark[0].Point.coordinates[1], 
    lon: response.Placemark[0].Point.coordinates[0], 
    address: response.Placemark[0].address 
    }; 

geocoder.geocode(document.forms[0].address2.value, function (response) { 
       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}; 
        calculateDistance(); 
       } 
      }); 
     } 
    }); 
} 

function calculateDistance() 
{ 
try 
{  
    var map; 
    var directionsPanel; 
    var directions; 
    var glatlng1 = new google.maps.LatLng(location1.lat, location1.lon); 
    var glatlng2 = new google.maps.LatLng(location2.lat, location2.lon); 
    var miledistance = glatlng1.distanceFrom(glatlng2, 3959).toFixed(1); 
    var kmdistance = (miledistance * 1.609344).toFixed(1); 
    map = new google.maps.Map(document.getElementById("map_canvas")); 
    map.setCenter(new google.maps.LatLng(location1.lat, location1.lon), 15); 
    directionsPanel = document.getElementById("route"); 
    directions = new GDirections(map, directionsPanel); 
    document.getElementById('route').innerHTML=''; 
    document.getElementById('map_canvas').innerHTML=''; 
    /* location1.address+ */ 
    //alert(document.getElementById('hdnLat').value); 
    document.getElementById('distance').value = directions.load("from:"+  document.getElementById('hdnLat').value +"," + document.getElementById('hdnLan').value + "  to: "+location2.address, {travelMode:G_TRAVEL_MODE_DRIVING}); 


/*   document.getElementById('results').innerHTML = '<strong>Address 1: </strong>' + location1.address + ' (' + location1.lat + ':' + location1.lon + ')<br /><strong>Address 2: </strong>' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')<br /><strong>Distance: </strong>' + miledistance + ' miles (or ' + kmdistance + ' kilometers)';*/ 

     setTimeout('adjustdistance(document.getElementById("route").innerHTML)', 8000); 
     ; 
     document.getElementById('distance').value= 'calculating...'; 

    } 
    catch (error) 
    { 
     alert(error); 
    } 
} 

function adjustdistance(htmlcode) 
{ 
var ht1; 
ht1 = htmlcode; 
var tyu; 
tyu= parseReturnedXML(ht1,'$Route.summaryHtml','table jstcache'); 

document.getElementById('distance').value=tyu; 


} 

function parseReturnedXML(strToParse, strStart, strFinish) 
{ 

var str; 
str=strToParse; 
var str1; 
var str2; 
str = str.replace(strStart,'~'); 
str = str.replace(strFinish,'~'); 

str=str.replace(/(<([^>]+)>)/ig,""); 
str = str.replace(',',''); 
str = str.replace('&nbsp;',''); 

str1= str.indexOf('km ('); 
str2=" km(s)"; 
if(str1=='-1') 
{ 
str1=str.indexOf('mi ('); 
str2=" miles"; 
} 
var str4; 
var str5; 
str4 = parseInt(str1) - 8; 
str5 = parseInt(str1) + 2; 
str = str.substring(str4,str5); 
str = str.replace(/[a-zA-Z]/g,""); 
str = str.replace(/^\s+/,""); 

str = str+str2; 


return str; 

} 



</script> 

我已經更新了各種V3的變化,但其根本不適合我返回的距離。

感謝

艾倫

+0

谷歌地圖具有優異的代碼樣本 - 天氣預報https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple,路線:https://developers.google.com/ maps/documentation/javascript/examples/directions-simple – Adam

+0

您對geocoder.geocode()服務失敗的請求,或者您的calculateDistance函數不工作?後者我假設,因爲'GDirections'是一個API v2函數 – duncan

回答

0

請參考鏈接。 http://jsfiddle.net/xJ26V/1/

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) { 
    var R = 6371; // Radius of the earth in km 
    var dLat = deg2rad(lat2-lat1); // deg2rad below 
    var dLon = deg2rad(lon2-lon1); 
    var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) + 
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2) 
    ; 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; // Distance in km 
    return d; 
}