Wonder what I am doing wrong, I want to create multiple map objects, with their own polylines based on an encoded latlng, I get it to work with one map, but can't get it right with multiple.
var outerDiv = document.getElementById('myOuterDiv');
var encodedlatlngs = ["ah|qIe{ow@wHagSvuIwfF", "yy`rIg}uu@v}MpnBeK{cY`bKa{@", "ah|qIe{ow@wHagSvuIwfF"];
function initialize() {
for(i = 0; i < encodedlatlngs.length; i++){
var myMap = document.createElement('div');
myMap.className = 'map' + i;
myMap.setAttribute("height", "200");
myMap.setAttribute("width", "400");
initMap(encodedlatlngs[i], myMap.className);
outerDiv.appendChild(myMap);
});
}
google.maps.event.addDomListener(window, "load", initialize);
function initMap(encoded_latlngs,mapID) {
map = new google.maps.Map(document.getElementById(mapID), {
zoom: 10,
center: {lat: 55.570, lng: 9.000},
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var decode2 = google.maps.geometry.encoding.decodePath(encoded_latlngs);
// Construct the polyline.
var polyline = new google.maps.Polyline({
path: decode2,
strokeColor: '#036eff',
strokeOpacity: 0.8,
strokeWeight: 3,
fillOpacity: 0.35
});
polyline.setMap(map);
infoWindow = new google.maps.InfoWindow;
}
initMap
function (document.getElementById
only works for ids, not classNames).myMap.setAttribute("style", "height: 200px; width: 400px;");
code snippet:
var encodedlatlngs = ["ah|qIe{ow@wHagSvuIwfF", "yy`rIg}uu@v}MpnBeK{cY`bKa{@", "ah|qIe{ow@wHagSvuIwfF"];
function initialize() {
var outerDiv = document.getElementById('myOuterDiv');
for (i = 0; i < encodedlatlngs.length; i++) {
var myMap = document.createElement('div');
myMap.id = 'map' + i;
myMap.setAttribute("style", "height: 200px; width: 400px;");
outerDiv.appendChild(myMap);
initMap(encodedlatlngs[i], myMap.id);
};
}
google.maps.event.addDomListener(window, "load", initialize);
function initMap(encoded_latlngs, mapID) {
map = new google.maps.Map(document.getElementById(mapID), {
zoom: 10,
center: {
lat: 55.570,
lng: 9.000
},
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var decode2 = google.maps.geometry.encoding.decodePath(encoded_latlngs);
// Construct the polyline.
var polyline = new google.maps.Polyline({
path: decode2,
strokeColor: '#036eff',
strokeOpacity: 0.8,
strokeWeight: 3,
fillOpacity: 0.35
});
polyline.setMap(map);
infoWindow = new google.maps.InfoWindow;
}
html,
body {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="myOuterDiv"></div>