谷歌将api v2映射到v3

时间:2013-11-28 12:08:19

标签: javascript google-maps google-maps-api-3

我一直在尝试将v2转换为v3地图应用程序,该应用程序计算输入的距离和输入的地址以及存储的lat / Lon。启动并运行没有任何运气。真的很感激一些帮助。

<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更改,但它根本没有给我留下距离。

由于

艾伦

1 个答案:

答案 0 :(得分: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;
}