2010-11-12 83 views
4

我有緯度/經度爲2個地方。如何從google maps api獲取路線? 我想顯示地圖和方向文本。谷歌地圖方向使用緯度/經度

+1

http://code.google.com/apis/maps/documentation/directions/ – 2010-11-12 10:36:59

回答

11
<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
    var directionDisplay; 
    var directionsService = new google.maps.DirectionsService(); 
    var map; 

    function initialize() { 
    directionsDisplay = new google.maps.DirectionsRenderer(); 
    var myOptions = { 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    directionsDisplay.setMap(map); 

    var start = '37.7683909618184, -122.51089453697205'; 
    var end = '41.850033, -87.6500523'; 
    var request = { 
     origin:start, 
     destination:end, 
     travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }; 
    directionsService.route(request, function(response, status) { 
     if (status == google.maps.DirectionsStatus.OK) { 
     directionsDisplay.setDirections(response); 
     var myRoute = response.routes[0]; 
     var txtDir = ''; 
     for (var i=0; i<myRoute.legs[0].steps.length; i++) { 
      txtDir += myRoute.legs[0].steps[i].instructions+"<br />"; 
     } 
     document.getElementById('directions').innerHTML = txtDir; 
     } 
    }); 
    } 
</script> 
</head> 
<body onload="initialize()"> 
<div id="directions" style="width:500px;height:500px;float:left"></div> 
<div id="map_canvas" style="width:500px;height:500px;"></div> 
</body> 
</html> 
1

還是使用以前的代碼:

… 
var start = new google.maps.LatLng('37.7683909618184', '-122.51089453697205'); 
var end = new google.maps.LatLng('41.850033', '-87.6500523'); 
var request = { 
    origin:start, 
    destination:end, 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
}; 
directionsService.route(request, function(response, status) { 
    … 
} 
… 

這樣的座標直接承認的,而不是從字符串進行評估。

0

又一解決方案:傳遞一個對象等,以出發地和目的地

{lat: 39.904309, lng: 116.385871}