Hey all I am in need of some help with adding to my array for the Google Maps API V3 .
Below is my code to populate the map with location marks:
var geocoder;
var map;
var infowindow = null;
var addresses = [];
var theMarkers = [];
function initialize() {
var centerMap = new google.maps.LatLng(45.3517923, 6.3101660);
geocoder = new google.maps.Geocoder();
addresses.push("11111","22222","33333");
for(i in addresses) {
var address = addresses[i];
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var bounds = new google.maps.LatLngBounds();
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var tmpAddress = results[0].formatted_address;
tmpAddress = tmpAddress.split(',');
theMarkers.push([tmpAddress[0], results[0].geometry.location.lat(), results[0].geometry.location.lng(), i, 'This location is ' + tmpAddress[0]]);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
/*theMarkers = [
['city1', 00.0000, -00.000000, 1, 'This is HTML Test 1'],
['city2', 00.00000, -00.000000000, 2, 'This is HTML Test 2'],
['city3', 00.00000, -00.000000, 3, 'This is HTML Test 3']
];*/
var mapOptions = {
zoom: 0,
center: centerMap,
panControl: false,
zoomControl: false,
scaleControl: false,
streetViewControl: false,
mapTypeControl: false
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
setZoom(map, theMarkers);
setMarkers(map, theMarkers);
infowindow = new google.maps.InfoWindow({
content: "Loading..."
});
}
function setZoom(map, markers) {
var boundbox = new google.maps.LatLngBounds();
for ( var i = 0; i < markers.length; i++ )
{
boundbox.extend(new google.maps.LatLng(markers[i][1], markers[i][2]));
}
map.setCenter(boundbox.getCenter());
map.fitBounds(boundbox);
}
function setMarkers(map, markers) {
for (var i = 0; i < markers.length; i++) {
var site = markers[i];
var site = markers[i];
var siteLatLng = new google.maps.LatLng(site[1], site[2]);
var marker = new google.maps.Marker({
position: siteLatLng,
map: map,
title: site[0],
zIndex: site[3],
html: site[4],
draggable: false,
//Markers drop on the map
animation: google.maps.Animation.DROP
});
google.maps.event.addListener(marker, "click", function () {
infowindow.setContent(this.html);
infowindow.open(map, this);
//marker.setAnimation(google.maps.Animation.BOUNCE);
});
}
}
Although it works without errors above I no longer have the zoom in area around the markers that I used to have when setting the theMarkers statically.
When using the theMarkers.push inside the for(i in addresses) {} the map looks like this:
But when I manually make theMarkers after finishing the for(i in addresses) {} :
theMarkers = [
['city1', 00.0000, -00.000000, 1, 'This is HTML Test 1'],
['city2', 00.00000, -00.000000000, 2, 'This is HTML Test 2'],
['city3', 00.00000, -00.000000, 3, 'This is HTML Test 3']
];
Then the map looks like this:
Which I am in need of getting it to do within the loop.
The geocoder is asynchronous, you need to set the zoom in the last callback to run. Simplest fix, move the bounds.extend/fitBounds into the callback and make the bounds global:
bounds.extend(results[0].geometry.location);
geocodedResults++;
if (geocodedResults >= (addresses.length - 1)) map.fitBounds(bounds);
updated initialize function:
function initialize() {
var centerMap = new google.maps.LatLng(45.3517923, 6.3101660);
geocoder = new google.maps.Geocoder();
addresses = ["New York, NY", "Newark, NJ", "Philadelphia, PA"];
for (var i = 0; i < addresses.length; i++) {
var address = addresses[i];
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var tmpAddress = results[0].formatted_address;
tmpAddress = tmpAddress.split(',');
theMarkers.push([tmpAddress[0], results[0].geometry.location.lat(), results[0].geometry.location.lng(), i, 'This location is ' + tmpAddress[0]]);
bounds.extend(results[0].geometry.location);
geocodedResults++;
if (geocodedResults >= (addresses.length - 1)) map.fitBounds(bounds);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
var mapOptions = {
zoom: 0,
center: centerMap,
panControl: false,
zoomControl: false,
scaleControl: false,
streetViewControl: false,
mapTypeControl: false
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
setMarkers(map, theMarkers);
infowindow = new google.maps.InfoWindow({
content: "Loading..."
});
}
code snippet:
var map; var bounds = new google.maps.LatLngBounds(); var infowindow = null; var addresses = []; var theMarkers = []; var geocodedResults = 0; function geocodeAddress(address, i) { geocoder.geocode({ 'address': address }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); var tmpAddress = results[0].formatted_address; tmpAddress = tmpAddress.split(','); theMarkers.push([tmpAddress[0], results[0].geometry.location.lat(), results[0].geometry.location.lng(), i, 'This location is ' + tmpAddress[0]]); bounds.extend(results[0].geometry.location); geocodedResults++; if (geocodedResults >= (addresses.length - 1)) map.fitBounds(bounds); } else { alert('Geocode was not successful for the following reason: ' + status); } }); } function initialize() { var centerMap = new google.maps.LatLng(45.3517923, 6.3101660); geocoder = new google.maps.Geocoder(); addresses = ["New York, NY", "Newark, NJ", "Philadelphia, PA"]; for (var i = 0; i < addresses.length; i++) { var address = addresses[i]; geocodeAddress(address, i); } var mapOptions = { zoom: 0, center: centerMap, panControl: false, zoomControl: false, scaleControl: false, streetViewControl: false, mapTypeControl: false }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); // setZoom(map, theMarkers); setMarkers(map, theMarkers); infowindow = new google.maps.InfoWindow({ content: "Loading..." }); } function setZoom(map, markers) { var boundbox = new google.maps.LatLngBounds(); for (var i = 0; i < markers.length; i++) { boundbox.extend(new google.maps.LatLng(markers[i][1], markers[i][2])); } map.setCenter(boundbox.getCenter()); map.fitBounds(boundbox); } function setMarkers(map, markers) { for (var i = 0; i < markers.length; i++) { var site = markers[i]; var siteLatLng = new google.maps.LatLng(site[1], site[2]); var marker = new google.maps.Marker({ position: siteLatLng, map: map, title: site[0], zIndex: site[3], html: site[4], draggable: false, //Markers drop on the map animation: google.maps.Animation.DROP }); google.maps.event.addListener(marker, "click", function() { infowindow.setContent(this.html); infowindow.open(map, this); //marker.setAnimation(google.maps.Animation.BOUNCE); }); } } google.maps.event.addDomListener(window, 'load', initialize);
<script src="http://maps.googleapis.com/maps/api/js"></script> <div id="map-canvas" style="height: 500px; width:500px;"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.