javascript - Two Google Maps on same page with markers - Stack Overflow
I have two maps that I am trying to show on one page, one for element id= mapall
, the other for id=mapall2
.
Both have multiple markers on them that are provided in arrays (var =markers and var = markers2
). At the moment both maps show on the page, but only one of them at a time shows the markers and has the map controls (zoom, full screen). If I refresh the page, then the other map has all of its markers and the other doesn't. How do I get both maps to load simultaneously?
First Map:
<script>
<?php
echo "var markers=$markers;\n";
?>
function initMap() {
var latlng = new google.maps.LatLng(-34.031342, 18.577419); // default location
var myOptions = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE,
mapTypeControl: true
};
var map = new google.maps.Map(document.getElementById('mapall'),myOptions);
var infowindow = new google.maps.InfoWindow(), marker, lat, lng;
for( i = 0; i < markers.length; i++ ) {
lat = (markers[i].GPS1);
lng = (markers[i].GPS2);
name = (markers[i].client_address);
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
name:name,
map: map
});
google.maps.event.addListener( marker, 'click', function(e){
infowindow.setContent( this.name );
infowindow.open( map, this );
}.bind( marker ) );
}
}
</script>
second map:
<script>
<?php
echo "var markers2=$markers2;\n";
?>
function initMap2() {
var latlng = new google.maps.LatLng(-33.92465, 18.84382); // default location
var myOptions = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE,
mapTypeControl: true
};
var map = new google.maps.Map(document.getElementById('mapall2'),myOptions);
var infowindow = new google.maps.InfoWindow(), marker, lat, lng;
for( i = 0; i < markers2.length; i++ ) {
lat = (markers2[i].GPS1);
lng = (markers2[i].GPS2);
name = (markers2[i].client_address);
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
name:name,
map: map
});
google.maps.event.addListener( marker, 'click', function(e){
infowindow.setContent( this.name );
infowindow.open( map, this );
}.bind( marker ) );
}
}
</script>
<script async defer
src=";callback=initMap">
</script>
<script async defer
src=";callback=initMap2">
</script>
and this is what they currently look like (the map on the right should also have markers, provided by markers2
:
I have two maps that I am trying to show on one page, one for element id= mapall
, the other for id=mapall2
.
Both have multiple markers on them that are provided in arrays (var =markers and var = markers2
). At the moment both maps show on the page, but only one of them at a time shows the markers and has the map controls (zoom, full screen). If I refresh the page, then the other map has all of its markers and the other doesn't. How do I get both maps to load simultaneously?
First Map:
<script>
<?php
echo "var markers=$markers;\n";
?>
function initMap() {
var latlng = new google.maps.LatLng(-34.031342, 18.577419); // default location
var myOptions = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE,
mapTypeControl: true
};
var map = new google.maps.Map(document.getElementById('mapall'),myOptions);
var infowindow = new google.maps.InfoWindow(), marker, lat, lng;
for( i = 0; i < markers.length; i++ ) {
lat = (markers[i].GPS1);
lng = (markers[i].GPS2);
name = (markers[i].client_address);
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
name:name,
map: map
});
google.maps.event.addListener( marker, 'click', function(e){
infowindow.setContent( this.name );
infowindow.open( map, this );
}.bind( marker ) );
}
}
</script>
second map:
<script>
<?php
echo "var markers2=$markers2;\n";
?>
function initMap2() {
var latlng = new google.maps.LatLng(-33.92465, 18.84382); // default location
var myOptions = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE,
mapTypeControl: true
};
var map = new google.maps.Map(document.getElementById('mapall2'),myOptions);
var infowindow = new google.maps.InfoWindow(), marker, lat, lng;
for( i = 0; i < markers2.length; i++ ) {
lat = (markers2[i].GPS1);
lng = (markers2[i].GPS2);
name = (markers2[i].client_address);
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
name:name,
map: map
});
google.maps.event.addListener( marker, 'click', function(e){
infowindow.setContent( this.name );
infowindow.open( map, this );
}.bind( marker ) );
}
}
</script>
<script async defer
src="https://maps.googleapis./maps/api/js?key=AIzaSyANS0uqM7qedfDCzPjJ3xoB15vh2DC4Tls&callback=initMap">
</script>
<script async defer
src="https://maps.googleapis./maps/api/js?key=AIzaSyANS0uqM7qedfDCzPjJ3xoB15vh2DC4Tls&callback=initMap2">
</script>
and this is what they currently look like (the map on the right should also have markers, provided by markers2
:
- stackoverflow./questions/4074520/… may be this will help you? – Aram Gevorgyan Commented Oct 8, 2017 at 12:40
1 Answer
Reset to default 4You are including the API twice. That generates the warning:
You have included the Google Maps API multiple times on this page. This may cause unexpected errors
Only include the API one time, put the initialization code for both maps inside a single initialization function.
One option:
<script>
function initialize() {
initMap();
initMap2();
}
</script>
<script async defer
src="https://maps.googleapis./maps/api/js?key=YOUR_API_KEY&callback=initialize">
</script>
proof of concept fiddle
code snippet:
var markers = [{
GPS1: -34.031342,
GPS2: 18.577419,
client_address: "somewhere1"
}];
function initialize() {
initMap();
initMap2();
}
function initMap() {
var latlng = new google.maps.LatLng(-34.031342, 18.577419); // default location
var myOptions = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE,
mapTypeControl: true
};
var map = new google.maps.Map(document.getElementById('mapall'), myOptions);
var infowindow = new google.maps.InfoWindow(),
marker, lat, lng;
for (i = 0; i < markers.length; i++) {
lat = (markers[i].GPS1);
lng = (markers[i].GPS2);
name = (markers[i].client_address);
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
name: name,
map: map
});
google.maps.event.addListener(marker, 'click', function(e) {
infowindow.setContent(this.name);
infowindow.open(map, this);
}.bind(marker));
}
}
var markers2 = [{
GPS1: -33.92465,
GPS2: 18.84382,
client_address: "somewhere2"
}];
function initMap2() {
var latlng = new google.maps.LatLng(-33.92465, 18.84382); // default location
var myOptions = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE,
mapTypeControl: true
};
var map = new google.maps.Map(document.getElementById('mapall2'), myOptions);
var infowindow = new google.maps.InfoWindow(),
marker, lat, lng;
for (i = 0; i < markers2.length; i++) {
lat = (markers2[i].GPS1);
lng = (markers2[i].GPS2);
name = (markers2[i].client_address);
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
name: name,
map: map
});
google.maps.event.addListener(marker, 'click', function(e) {
infowindow.setContent(this.name);
infowindow.open(map, this);
}.bind(marker));
}
}
html,
body {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
#mapall,
#mapall2 {
height: 50%;
width: 100%;
border: red solid 1px;
}
<div id="mapall"></div>
<div id="mapall2"></div>
<script async defer src="https://maps.googleapis./maps/api/js?callback=initialize">
</script>
- SaaS 的历史与变革
- 网络监控火速普及 产品布局有待调整
- [连载]巨头“心血之作”终失败(三):诺基亚N9
- java - Omit super class fields in spring doc open api docs - Stack Overflow
- python - Chromadb: Why do results of collection.query() and collection.get() differ? - Stack Overflow
- swift - Content inside .sheet() being infinitely called - Stack Overflow
- reactjs - Why getting error 'Objects are not valid as a React child' in turborepo ui tests? - Stack Overflow
- How to optimize query performance in a large fact table with billions of rows? - Stack Overflow
- rust - Basic bracket-lib example crashes with “unsafe precondition(s) violated: slice::from_raw_parts” - Stack Overflow
- java - I have an issue with tmcbeans, I can not run projects - Stack Overflow
- starknet - How to generate and verify a STARK proof from a Cairo program locally? - Stack Overflow
- intellij http client - How to use in place variable within handler scripts? - Stack Overflow
- reactjs - How to import svg icons in Nextjs 15 - Stack Overflow
- sharepoint - How to copy site page from one site to another site using Power Automate? - Stack Overflow
- shell - how to change my CONDA_DEFAULT_ENV in Linux? - Stack Overflow
- javascript - How do I change file pathing after installation? - Stack Overflow
- VS Code : How to deactivate matching characters in bold? - Stack Overflow