Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Map API V3: How to add Custom data to markers

People also ask

How do I make a custom marker map?

Create Custom Maps using Google MapsClick on the “Your Places” option in the menu. Click on the “Maps” Tab in the top right. Click on the “CREATE MAP” link at the bottom of the menu. Once you are on the map creation page, click the marker icon to add a marker to the page.


As a Google Marker is a JavaScript object, you may add custom information in the form key: value, where key is a valid string. They are called object properties and can be approached in many different ways. The value can be anything legal, as simple as numbers or strings, and also functions, or even other objects. Three simple ways: in the declaration, dot notation and square brackets

var markerA = new google.maps.Marker({
    map: map,
    position: new google.maps.LatLng(0, 0),
    customInfo: "Marker A"
});

var markerB = new google.maps.Marker({
    map: map,
    position: new google.maps.LatLng(-10, 0)
});
markerB.customInfo = "Marker B";

var markerC = new google.maps.Marker({
    map: map,
    position: new google.maps.LatLng(-20, 0)
});
markerC['customInfo'] = "Marker C";

Then to retrieve it in a similar manner:

google.maps.event.addListener(markerA, 'click', function() {
    alert(this.customInfo);
});

You can add your own custom properties to the markers (just be careful not to conflict with the API's properties).