问题
I have two google maps containers on my page.
the first - id="map" is just a normal "display map" the second - id="map2" is a search map where the user types in the input and the map refreshes to where the user typed.
these maps use the same api key but their callbacks differ.
if I add both of the following get and error in console saying duplicate api calls
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" type="text/javascript"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initAutocomplete" type="text/javascript"></script>
but the callbacks differ for each maps usage?
so how do i go about getting both maps to work, because currently I either get the on to work then the other wont work
my code is as follows:
<div id="map"></div>
<div id="map2"></div>
<script>
function initMap() {
var paarl = {lat: -33.725853, lng: 18.988628};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
scrollwheel: false,
center: paarl
});
var marker = new google.maps.Marker({
position: paarl,
map: map
});
}
</script>
<script>
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map2'), {
center: {lat: -33.8688, lng: 151.2195},
zoom: 13,
scrollwheel: false,
mapTypeId: 'roadmap'
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var 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(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" type="text/javascript"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initAutocomplete" type="text/javascript"></script>
回答1:
Don't use two google maps library request
the simplest way is invoke the second function inside the fist initMap
<script>
function initMap() {
var paarl = {lat: -33.725853, lng: 18.988628};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
scrollwheel: false,
center: paarl
});
var marker = new google.maps.Marker({
position: paarl,
map: map
});
initAutocomplete();
}
</script>
or eventually use a onload event for load the second
来源:https://stackoverflow.com/questions/41889362/add-two-google-maps-with-different-callbacks