Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
896 views
in Technique[技术] by (71.8m points)

google maps api infobox plugin and multiple markers

How can I create multiple markers with different content when using infobox plugin http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/examples/infobox-basic.html

I was creating var marker1, var marker2 etc but I think this is not really nice method and I have the same content in all infowindows...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You just need to generate the markers with a factory of some sort, e.g:

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" }
    ]);
}

Check out the full example with HTML and whatnot in this jsfiddle.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...