Я записал этот невинный код JavaScript, который позволяет пользователю создать два маркера и вывести маршрут на печать между ними. Это не работает, вместо этого, это дает странную ошибку:
Uncaught TypeError: Cannot read property 'ya' of undefined
Может кто-то предлагать меня что случилось здесь:
// called upon a click
GEvent.addListener(map, "click", function(overlay,point) {
if (isCreateHeadPoint) {
// add the head marker
headMarker = new GMarker(point,{icon:redIcon,title:'Head'});
map.addOverlay(headMarker);
isCreateHeadPoint = false;
} else {
// add the tail marker
tailMarker = new GMarker(point,{icon:greenIcon,title:'Tail'});
map.addOverlay(tailMarker);
isCreateHeadPoint = true;
// create a path from head to tail
direction.load("from:" + headMarker.getPoint().lat()+ ", " + headMarker.getPoint().lng()+ " to:" + tailMarker.getPoint().lat() + "," + tailMarker.getPoint().lng(), { getPolyline: true, getSteps: true });
// display the path
map.addOverlay(direction.getPolyline());
}
});
Следуя из вашего решения , вам может вообще не понадобиться использовать map.addOverlay (direction.getPolyline ())
. В следующем примере полилиния добавляется на карту автоматически:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps GDirections</title>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false"
type="text/javascript"></script>
</head>
<body onunload="GUnload()">
<div id="map" style="width: 400px; height: 300px"></div>
<script type="text/javascript">
var map = new GMap2(document.getElementById("map"));
var directions = new GDirections(map);
var isCreateHeadPoint = true;
var headMarker, tailMarker;
map.setCenter(new GLatLng(51.50, -0.12), 12);
GEvent.addListener(map, "click", function(overlay,point) {
if (isCreateHeadPoint) {
// add the head marker
headMarker = new GMarker(point);
map.addOverlay(headMarker);
isCreateHeadPoint = false;
}
else {
// add the tail marker
tailMarker = new GMarker(point);
map.addOverlay(tailMarker);
isCreateHeadPoint = true;
// create a path from head to tail
directions.load("from:" + headMarker.getPoint().lat()+ ", " +
headMarker.getPoint().lng() +
" to:" + tailMarker.getPoint().lat() + "," +
tailMarker.getPoint().lng(),
{ getPolyline: true, getSteps: true });
}
});
</script>
</body>
</html>
Снимок экрана:
О, я понял ... по какой-то странной причине я подумал, что direction.load () - это блокирующий вызов. Следующие действия работают:
// called upon a click
GEvent.addListener(map, "click", function(overlay,point) {
if (isCreateHeadPoint) {
// add the head marker
headMarker = new GMarker(point,{icon:redIcon,title:'Head'});
map.addOverlay(headMarker);
isCreateHeadPoint = false;
} else {
// add the tail marker
tailMarker = new GMarker(point,{icon:greenIcon,title:'Tail'});
map.addOverlay(tailMarker);
isCreateHeadPoint = true;
// create a path from head to tail
direction.load("from:" + headMarker.getPoint().lat()+ ", " + headMarker.getPoint().lng()+ " to:" + tailMarker.getPoint().lat() + "," + tailMarker.getPoint().lng(), { getPolyline: true, getSteps: true });
}
});
// called when the direction.load() returns
GEvent.addListener(direction,"load", function() {
// display the path
map.addOverlay(direction.getPolyline());
});