我正在尝试通过谷歌地图javascript v2 .What获取给定方向的持续时间和距离我正在做的是单击按钮我正在发送ajax请求,它返回来自服务器端的经度和纬度信息(从gpx文件) den我再次使用地图重新填充div,从新的路点地图开始
<head>
<script src="http://maps.google.com/maps?file=api&v=2&" type="text/javascript"></script>
<script type="text/javascript">
var map;
var directionsPanel;
var directions;
var points_lt = [12.9172543,12.9326889,12.9761378,12.9896399];
var points_lg = [77.6230109,77.6131779,77.6017195,77.5928038];
function initialize() {
map = new GMap2(document.getElementById("map_canvas"));
var wp = new Array(2);
wp[0] = new GLatLng(12.9172543,77.6230109);
wp[1] = new GLatLng(13.0144572,77.5689697);
map.setUIToDefault();
// load directions
directionsPanel = document.getElementById("route");
directions = new GDirections(map, directionsPanel);
directions.loadFromWaypoints(wp);
}
function ChangeIt(){
var cur_lt=null;
var cur_lg=null;
var cal=$.ajax({
type: "GET",
url: "./Handler.php?q=now",
dataType: "html"
});
cal.done(function(msg){
if(msg == 0){
return false;
}
var Gpoint=msg.split("|");
cur_lt=Gpoint[0];
cur_lg=Gpoint[1];
var wp = new Array(2);
wp[0] = new GLatLng(cur_lt,cur_lg);
wp[1] = new GLatLng(13.0144572,77.5689697);
document.getElementById("map_canvas").innerHTML="";
map = null;
directionsPanel.innerHTML = "" ;
map = new GMap2(document.getElementById("map_canvas"));
map.setUIToDefault();
directions = new GDirections(map,directionsPanel);
directions.loadFromWaypoints(wp);
alert(directions.getDuration().html);
});
}
$(document).ready(function(){
var a= function(){
ChangeIt();
};
$("#bt1").click(function(){
ChangeIt();
});
//setInterval(a, 3000);
});
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<input type="button" name="bt1" value="Change Route" id="bt1">
<div id="map_canvas" style="width: 600px; height: 400px ;left:350px"></div>
<div id="route" style="width: 25%; height:480px; float:right; border; 1px solid black;"> </div>
<br/>`
但当我尝试getDistance或getDuration时,我没有得到任何警告,我认为我返回的是null或一些不期望的东西。请帮助.I我是谷歌地图的新手,如果我做错了请告诉我
发布于 2012-11-07 20:17:37
请注意:
自2010年5月19日起,谷歌地图JavaScript API第2版已被正式弃用。V2应用编程接口将继续工作到2013年5月19日
我强烈建议您迁移到Google API V3。
发布于 2012-11-07 20:18:04
Directions服务是异步的。在数据从服务器返回之前,您无法访问结果。有关可用事件的描述,请参阅the documentation ("load“似乎很可能,但我上次使用v2已经有一段时间了)。
请注意,Google Maps API v2已被弃用,实际上不应该用于新的开发。
https://stackoverflow.com/questions/13269489
复制相似问题