【问题标题】:Google maps api v2 to v3Google 将 api v2 映射到 v3
【发布时间】:2013-11-28 12:08:18
【问题描述】:

我一直在尝试将 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 更改,但它根本没有为我返回距离。

谢谢

艾伦

【问题讨论】:

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


【解决方案1】:

请参考链接。 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 2015-12-06
    相关资源
    最近更新 更多