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
168 views
in Technique[技术] by (71.8m points)

javascript - Multiple markers with info windows map error

I have this working map https://jsfiddle.net/m9ugbc7h/4/ then i tryed to integrate multiple markers with info windows following step by step this tutorial http://wrightshq.com/playground/placing-multiple-markers-on-a-google-map-using-api-3/ so now i got this new version of the map https://jsfiddle.net/m9ugbc7h/5/ but it doesn't work

Here the description how i think the added code works

This is the text for two info windows in order

var infoWindowContent = [
    ['<div class="info_content">' +
    '<h3>Ventura</h3>' +
    '<p>Ventura P</p>' +
     '</div>'],
    ['<div class="info_content">' +
    '<h3>Selvatica</h3>' +
    '<p>Selvatica p</p>' +
    '</div>']
];

This adds a number to every marker listed before

// Loop through our array of markers & place each one on the map  
for( i = 0; i < markers.length; i++ ) {
    var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
    bounds.extend(position);
    marker = new google.maps.Marker({
        position: position,
        map: map,
        title: markers[i][0]
    });

And this one asigns text 1 to marker 1, text 2 to marker 2, etc.

 // Allow each marker to have an info window    
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infoWindow.setContent(infoWindowContent[i][0]);
            infoWindow.open(map, marker);
        }
    })(marker, i));
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You have few issues with the code:

First

Console error google is not defined caused by:

 var bounds = new google.maps.LatLngBounds();

at the top of the file before google js is loaded. Move it inside jQuery(document).ready(function($){:

jQuery(document).ready(function($){
    bounds = new google.maps.LatLngBounds();

Second

// Loop through our array of markers & place each one on the map 

Why? They are already placed on the map, you just want to attach them InfoWindow:

 //marker = new google.maps.Marker({
 //    position: position,
 //    map: map,
 //     title: markers[i][0]
 //});
 var marker = markers[i];

In the above code is the root of your problem (info windows not showing up): you are creating new Marker object instead of using the existing one from the markers array.

Third

Console error: too much recursion

caused by:

 // Automatically center the map fitting all markers on the screen
 map.fitBounds(bounds);

The issue with this one is most probably that bounds are undefined at this moment. Not sure why, but too much recursion is usualy caused by that. I have commented that out, so you will have to take a look how to pass valid value in fitBounds.

Fourth

You have sintax error here:

var infoWindow = new google.maps.InfoWindow(), marker, i;

You probably meant:

var infoWindow = new google.maps.InfoWindow();

Working fiddle: https://jsfiddle.net/m9ugbc7h/7/


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

...