Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
Created July 14, 2013 00:27
Show Gist options
  • Save ChrisLTD/5992716 to your computer and use it in GitHub Desktop.
Save ChrisLTD/5992716 to your computer and use it in GitHub Desktop.
Google Maps API V3 map w/ multiple markers, info boxes and auto center
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-39.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
var markers = new Array();
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
function AutoCenter() {
// Create a new viewpoint bound
var bounds = new google.maps.LatLngBounds();
// Go through each...
$.each(markers, function (index, marker) {
bounds.extend(marker.position);
});
// Fit these bounds to the map
map.fitBounds(bounds);
}
AutoCenter();
</script>
</body>
</html>
@barsenovic
Copy link

really useful! thanks man

@VeggieVampire
Copy link

great stuff!!!! full code too :)

@ilkersurmeli
Copy link

This really nice but my markers has location with a search. So how can I get location info for each to an info box.
here is my code
var map;
var service;
var marker;
var infowindow;
function handleSearchResults(results, status)
{
console.log(results);
for (var i = 0; i<results.length; i++){
marker = new google.maps.Marker({
position: results[i].geometry.location,
map: map,
icon: 'Book-Red.png'
});
}

        }
        function performSearch()
        {
            var request = {
                bounds: map.getBounds(),
                name: "Kirjasto"
            }
            service.nearbySearch(request, handleSearchResults);
        }

        function initialise(location)
        {
            var mapholder = document.getElementById("mapholder");
                mapholder.style.height='500px';
                mapholder.style.width='100%';
                mapholder.style.margin='0px';
                mapholder.style.padding='0px';
            console.log.lacation;
            var currentLocation = new google.maps.LatLng(location.coords.latitude, location.coords.longitude);
            var mapOptions = {
                 center: currentLocation,
                 zoom: 13,
                 mapTypeId: google.maps.MapTypeId.ROADMAP

            };
                map = new google.maps.Map(mapholder,mapOptions);

                marker = new google.maps.Marker({
                position: currentLocation,
                map: map,
                title: 'Olet täällä'
            });


              google.maps.event.addListener(marker, 'click', function() {
              infowindow.open(map,marker);
              });

                service = new google.maps.places.PlacesService(map);
                google.maps.event.addListener(map,'bounds_changed', performSearch);

            var circleOptions = {
                strokeColor: "#0000FF",
                strokeOpacity: 0.8,
                strokeWeight: 1.5,
                fillColor: "#0000FF",
                fillOpacity: 0.20,
                map:map,
                center: currentLocation,
                radius:2000
            };
                circle = new google.maps.Circle(circleOptions);


        }
        $(document).ready(function() {
            navigator.geolocation.getCurrentPosition(initialise);
        });

@tapanswain-zz
Copy link

I like the multiple info winodw . please help in on multiple hyperlink with multiple marker code.

@0785178663
Copy link

it work for me, but I want to add a blue marker on the current position, so with those below I can location my current position:
function success(position)
{
var googleLatLng = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var mapOtn = {
zoom : 15,
center : googleLatLng,
maTyptId : google.maps.MapTypeId.ROADMAP
}

var Pmap     = document.getElementById("map")

var map      = new google.maps.Map(Pmap, mapOtn);
addMarker(map, googleLatLng, "Devapp.com", "Hello! <br><br>Choose your Driver</br>");

}

function addMarker(map, googleLatLng, title, content){
var markerOptn = {
position : googleLatLng,
map : map,
title : title,
animation : google.maps.Animation.BOUNCE

};

var marker     =  new google.maps.Marker(markerOptn);

var infoWindow =  new google.maps.InfoWindow({ content: content, 
                                               position: googleLatLng});
google.maps.event.addListener(marker, "click", function(){

    infoWindow.open(map);
});

}

/Set multiple markers on my map/


/Set multiple markers on my map/

function fail(error)
{
var errorType = {
0: "Unknown Error",
1: "Permission denied by the user",
2: "Position of the user not available",
3: "Request timed out"
};

var errMsg = errorType[error.code];

if (error.code == 0 || error.code == 2) {
    errMsg = errMsg+" - "+error.message;
}
$("p").html(errMsg);

}

Please any help!

@elamigosam
Copy link

is this code still relevant in 2020?

I am having a hard time figuring out a auto zoom on multiple markers map.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment