首页 新闻 会员 周边

google map api开发:如何发送坐标得到目的地的行车路线

0
悬赏园豆:10 [已解决问题] 解决于 2013-03-27 11:46

是有关google map api的问题,现在我已经 根据手机的gps功能得到所处位置的经纬度,如何
获取到从该坐标到一个地方名称(或坐标)的行车路线呢?

补充一下使用场景:为某酒店开发一个app,客人行车路程上想便捷查询到到达酒店的路线,于是使用app向酒店服务端发送位置信息(坐标),现在我编写服务器程序,收到客人的坐标以后,需要把含有显示行车路线的google地图的页面的url返回给客人。在网上找到的样例是从地名到地名的写法,我想实现的是从坐标到地名(或者坐标也可以)的路线查询。

lindping的主页 lindping | 初学一级 | 园豆:3
提问于:2013-03-25 14:17
< >
分享
最佳答案
0

获取两点间的实际行车路径主要是使用谷歌地图的DirectionsService服务。

//显示实际行车路线
function showpath() {
var directionsService = new google.maps.DirectionsService(); //实例构造
var directionsDisplay = new google.maps.DirectionsRenderer({ markerOptions: {
'map': map
}//行车路线
});
directionsDisplay.setMap(map);
var request = {
origin: pointArr[0], //起点
destination: pointArr[temp], //终点
optimizeWaypoints: false, //为true,重新排列中间路标顺序,最大程度降低路线整体成本
travelMode: google.maps.TravelMode.DRIVING, //驾车路线
unitSystem: google.maps.UnitSystem.METRIC//单位为米
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response); //将返回的路线信息赋给directionsDisplay,显示在地图上
}
else {
}
});
}

收获园豆:10
allon6318 | 小虾三级 |园豆:858 | 2013-03-26 09:22

你这里的起点和终点填什么呢?地名吗? 我想填经纬度。可以实现吗

lindping | 园豆:3 (初学一级) | 2013-03-26 11:58

@lindping: 填写的就是经纬度哦,比如

var pointArr[0] = new google.maps.LatLng(22.566758, 113.885292);
var pointArr[temp] = new google.maps.LatLng(22.489697, 113.917221);

allon6318 | 园豆:858 (小虾三级) | 2013-03-26 15:13

@allon6318: 

 

new google.maps.DirectionsService() 执行报错,没有这个对象,我的页面引用是

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=xxxx"
type="text/javascript"></script>

lindping | 园豆:3 (初学一级) | 2013-03-27 10:41

@lindping: V2已经被CUT了,我用的是V3版本的

allon6318 | 园豆:858 (小虾三级) | 2013-03-27 10:50

@allon6318: 我改为v3版本,可以用了,但是你的代码不是完整的代码,还有一些错误。

pointArr数组没有初始化,另外 map应该是div的一个id吧?这些我都改了,代码也能正确执行,

监视directionsDisplay.setDirections(response)里的response也有正确数据,执行后页面无任何变化,一片空白,是否缺少其他像初始化的一些操作?

附上我代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Geocoding</title> <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">

var map = document.getElementById("map");
function showpath() {
var directionsService = new google.maps.DirectionsService(); //实例构造
var directionsDisplay = new google.maps.DirectionsRenderer({ markerOptions: {
'map': map
}//行车路线
});
var pointArr1 = new google.maps.LatLng(22.566758,113.885292);
var pointArr2 = new google.maps.LatLng(31.238508612629097,121.46677494049072);
directionsDisplay.setMap(map);
var request = {
origin: pointArr1, //起点
destination: pointArr2, //终点
optimizeWaypoints: false, //为true,重新排列中间路标顺序,最大程度降低路线整体成本
travelMode: google.maps.TravelMode.DRIVING, //驾车路线
unitSystem: google.maps.UnitSystem.METRIC//单位为米
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response); //将返回的路线信息赋给directionsDisplay,显示在地图上
}
else {
}
});
}


</script>

</head>
<body onload="showpath()" >
<div id="map" style="width: 500px; height: 300px">
</div>
</body>
</html>

lindping | 园豆:3 (初学一级) | 2013-03-27 11:16

@lindping: 你留个邮箱给我,我晚上做个DEMO给你

allon6318 | 园豆:858 (小虾三级) | 2013-03-27 13:47

@allon6318: 你好,代码我已调整好,能正常显示,但我还想在地图下方加上文字描述行车路线,就是像在google地图上查询 从xx到xx的时候左边显示的文字描述路线。

lindping | 园豆:3 (初学一级) | 2013-03-29 15:45

@lindping: 这个功能并不能通过简单调用API实现,你自己下个JSAPI文档研究一下吧。好久没用过,我也搞不清了。。。

allon6318 | 园豆:858 (小虾三级) | 2013-04-01 09:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册