正确显示地图,dierction和距离,但脚本停止,我的网站的其余部分未在Chrome中显示。 它能是什么?
我找到了错误的原因。 下面的代码是正确的。 这一个:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
don`t likes that:
<script src='http://maps.googleapis.com/maps/api/js?key=AIzaSyCsWAeW4gm1P1f-pyYhiAkkE&libraries=places'></script>
in one file.
I put the first one in a different file. Everything works fine again.
The only question is why the error came suddenly, over night?
代码:
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(49.744006,8.820419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map- canvas'), mapOptions);
var land = document.getElementById('stland').value;
/* if ( document.getElementById('ziland').value != land ) */
//alert(land);
var input = /** @type {HTMLInputElement} */ ( document.getElementById('start'));
var options = {
types: ['geocode'],
types: ['(cities)'],
componentRestrictions: {country: land}
};
var autocomplete = new google.maps.places.Autocomplete(input,options);
var land2 = document.getElementById('ziland').value;
var input2 = /** @type {HTMLInputElement} */(document.getElementById('end'));
var options2 = {
types: ['geocode'],
types: ['(cities)'],
//regions:['postal_code'],
componentRestrictions: {country: land2}
};
var autocomplete2 = new google.maps.places.Autocomplete(input2,options2);
autocomplete.bindTo('bounds', map);
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map
});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
infowindow.close();
marker.setVisible(false);
input.className = '';
var place = autocomplete.getPlace();
if (!place.geometry) {
// Inform the user that the place was not found and return.
input.className = 'notfound';
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
marker.setIcon(/** @type {google.maps.Icon} */({
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(35, 35)
}));
marker.setPosition(place.geometry.location);
marker.setVisible(true);
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.open(map, marker);
});
// Sets a listener on a radio button to change the filter type on Places
// Autocomplete.
function setupClickListener( types) {
var radioButton = document.getElementById('changetype-geocode');
google.maps.event.addDomListener(radioButton, 'click', function() {
autocomplete.setTypes(types);
});
}
setupClickListener('changetype-all', []);
setupClickListener('changetype-establishment', ['establishment']);
setupClickListener('changetype-geocode', ['geocode']);
}
google.maps.event.addDomListener(window, 'load', initialize);
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize1() {
directionsDisplay = new google.maps.DirectionsRenderer();
var frankfurt = new google.maps.LatLng(50.127182,8.678398);
var mapOptions = {
zoom:5,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: frankfurt
}
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
directionsDisplay.setMap(map);
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
computeTotalDistance(directionsDisplay.directions);
});
}
function calcRoute() {
//setTimeout( 3000);
var start = document.getElementById('stland').value + ' ' + document.getElementById('stplz').value + ' ' + document.getElementById('start').value;
var end = document.getElementById('ziland').value + ' ' + document.getElementById('ziplz').value + ' ' + document.getElementById('end').value;
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
for (i = 0; i < myroute.legs.length; i++) {
total += myroute.legs[i].distance.value;
}
total = total / 1000;
//document.getElementById('entfernung').innerHTML = total + ' km';
document.anfrager1.entfernung.value = total + ' km';
}