這是我的地圖代碼。 但是當你運行它時,一切都很好。然後,如果你點擊「Let's Go!」你得到這個錯誤:
Assertion failed: InvalidValueError: in property origin: not a string; and not a LatLng or LatLngLiteral: not an Object; and not an ObjectAb @ maps.googleapis.com/maps-api-v3/api/js/22/11a/main.js:16 index.html:7
和Google Maps API最快路線
Uncaught TypeError: Cannot read property 'N' of null
有誰知道如何解決這個問題???如果是的話請幫忙
HTML:
<div id="floating-panel">
<input id="toAddress" type="textbox" value="">
<input id="submit" type="button" value="Lets Go!">
</div>
<div id="map"></div>
的javascript:
var currentPosition;
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {lat: 33.8, lng: -84.4}
});
var geocoder = new google.maps.Geocoder();
var trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setMap(map);
var infoWindow = new google.maps.InfoWindow({map: map});
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
currentPosition = new google.maps.LatLng(parseFloat(pos.geolocation.lat), parseFloat(pos.geolocation.lng));
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
document.getElementById('submit').addEventListener('click', function() {
// geocodeAddress(geocoder, map);
var directionsService = new google.maps.DirectionsService;
directionsService.route({
origin: currentPosition,
destination: document.getElementById('toAddress').value,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
});
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
請提供一個[最小,完整,已測試和可讀的示例](http://stackoverflow.com/help/mcve)來說明問題。在發佈的代碼中沒有HTML(「Let's Go」)。 – geocodezip
我添加了html – Thayne