Вам просто нужно сгенерировать маркеры с какой-то фабрикой, например:
function initMarkers(map, markerData) {
var newMarkers = []
// Here's where all the really verbose code goes. Loop through `markerData` to
// create each marker. See the full code in the js fiddle
return newMarkers;
}
function initialize_google_map() {
//Here the call to initMarkers() is made with the necessary data for each marker.
//All markers are then returned as an array into the markers variable, Usually you'd
//get the data from server or something, here it's just shown inline.
var markers = initMarkers(map, [
{ latLng: new google.maps.LatLng(49.47216, -123.76307), address: "Address 1", state: "State 1" },
{ latLng: new google.maps.LatLng(49.47420, -123.75703), address: "Address 2", state: "State 2" },
{ latLng: new google.maps.LatLng(49.47530, -123.78040), address: "Address 3", state: "State 3" }
]);
}
Посмотрите полный пример с HTML и прочее в этом jsfiddle .