【发布时间】:2016-05-11 14:05:12
【问题描述】:
对不起,如果这是以前已经问过的问题。我想加载一个带有 2 个标记的地图,起点和终点。建立 2 个标记时,我绘制了要遵循的路径(模式:驱动器)。
当我选择 2 个标记时,我收到此错误:“Uncaught InvalidValueError: in property origin: not a string; and not a LatLng or LatLngLiteral: not an Object” 请帮帮我!
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script>
var map;
var cont=0;
var directionsService = new google.maps.DirectionsService();
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644)
};
map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
google.maps.event.addListener(map, "click", daclick);
function daclick(evento){
//var cont=0;
if(cont==0){
var latitudOrigen = evento.latLng.lat();
var longitudOrigen = evento.latLng.lng();
var myLatlngOrigen = new google.maps.LatLng(latitudOrigen,longitudOrigen);
var markerOrigen = new google.maps.Marker({
position: myLatlngOrigen,
map: map,
title: "Origen"
});
cont=1;
}else{
var latitudDestino = evento.latLng.lat();
var longitudDestino = evento.latLng.lng();
var myLatlngDestino= new google.maps.LatLng(latitudDestino,longitudDestino);
var markerDestino = new google.maps.Marker({
position: myLatlngDestino,
map: map,
title: "Destino"
});
cont=0;
}
var request = {
origin:latitudOrigen,
destination:myLatlngDestino,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
【问题讨论】:
标签: javascript google-maps google-maps-api-3