2013-11-28 193 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; 
}