0
當我調用Google地圖方向服務時,出現Uncaught InvalidValueError: in property origin: not a string; and not a LatLng or LatLngLiteral: not an Object
錯誤 - 即便如此,方向似乎已添加到地圖中了嗎?調用Google地圖方向時未捕獲的異常服務
這裏是調用當前位置(currentPos
)和位置之間的路線服務不太遠的功能(LatLng(52.705151, -2.741861)
)
function calcRoute() {
var start = currentPos;
var end = new google.maps.LatLng(52.705151, -2.741861);
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
這裏是初始化地圖並調用calcRoute()
函數的代碼:
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var currentPos;
var destinationPos;
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 14
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
directionsDisplay.setMap(map);
var infoWindow = new google.maps.InfoWindow();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
currentPos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var marker = new google.maps.Marker({
position: currentPos,
map: map,
title: "You are here"
});
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png')
map.setCenter(currentPos);
$.ajax({
type: 'GET',
url: $('#AddMarkersToMap').val(),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
addMarkers(data, infoWindow);
},
error: function() {
alert("Error");
}
});
}, function() {
handleNoGeolocation(true);
});
} else {
handleNoGeolocation(false);
}
calcRoute();
}
一些谷歌搜索建議的例外可能引起因爲地圖沒有加載,所以我試圖把calcRoute
這樣調用它並沒有幫助:
$(function() {
calcRoute()
});
完美的,謝謝你解釋得這麼好 – NRKirby