谷歌地图方向使用纬度/经度 [英] Google map direction using lat/lon

查看:107
本文介绍了谷歌地图方向使用纬度/经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个地方经纬度。如何从google maps api获取路线?
我想显示地图和路线文本。

解决方案

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

函数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 =directionsstyle =width:500px; height:500px; float:left>< / div>
< div id =map_canvasstyle =width:500px; height:500px;>< / div>
< / body>
< / html>


I have lat/lon for 2 places. How to get directions from google maps api? I want to show map and directions text.

解决方案

<!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>

这篇关于谷歌地图方向使用纬度/经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆