h_h
h_h

Reputation: 1231

Google maps API multiple markers

i am using following code to show google map and also create a marker on the map. Its working well. I just need to put multiple markers on the same map.

<!DOCTYPE html>
<html>
<head>
<script
src="http://maps.googleapis.com/maps/api/js?    
key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false">
</script>

<script>
var myCenter=new google.maps.LatLng(51.508742,-0.120850);

function initialize()
{
var mapProp = {
center:myCenter,
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

var marker=new google.maps.Marker({
position:myCenter,
});

marker.setMap(map);

var infowindow = new google.maps.InfoWindow({
content:"Hello World!"
});

infowindow.open(map,marker);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body> 
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>
</html>

Can anyone help me to drop multiple markers on the same map by using multiple addresses. Thanks

Upvotes: 0

Views: 4377

Answers (1)

Shiridish
Shiridish

Reputation: 4962

This is a basic question and several examples can be found in the web. You basically need an array to hold the various markers lat lng's. Then use a loop to place these latlng's on the map just like you do with one marker.

var berlin = new google.maps.LatLng(52.520816, 13.410186);
var neighborhoods = [
new google.maps.LatLng(52.511467, 13.447179),
new google.maps.LatLng(52.549061, 13.422975),
new google.maps.LatLng(52.497622, 13.396110),
new google.maps.LatLng(52.517683, 13.394393)
];
var markers = [];
var map;

function initialize() {
var mapOptions = {
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: berlin
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
drop();
}

function drop() {
 for (var i = 0; i < neighborhoods.length; i++) 
  {
    markers.push(new google.maps.Marker({
    position: neighborhoods[i],
    map: map,
    }));
  }
}

Upvotes: 4

Related Questions