javascript - Multiple markers in Google Maps API v3 that link to different pages when clicked - Stack Overflow

I have a map with multiple markers, which I populate via an array. Each marker is clickable and should

I have a map with multiple markers, which I populate via an array. Each marker is clickable and should take the user to a different url per marker.

The problem is that all the markers, while displaying the correct title, all use the url of the last entry in the array.

Here is my code:

var myOptions = { zoom: 9, center: new google.maps.LatLng(40.81940575,-73.95647955), mapTypeId: google.maps.MapTypeId.TERRAIN }
var map = new google.maps.Map(document.getElementById("bigmap"), myOptions);
setMarkers(map, properties);

var properties = [
['106 Ft Washington Avenue',40.8388485,-73.9436015,'Mjg4'],
['213 Bennett Avenue',40.8574384,-73.9333426,'Mjkz'],
['50 Overlook Terrace',40.8543752,-73.9362542,'Mjky'],
['850 West 176 Street',40.8476012,-73.9417571,'OTM='],
['915 West End Avenue',40.8007478,-73.9692155,'Mjkx']];

function setMarkers(map, buildings) {
    var image = new google.maps.MarkerImage('map_marker.png', new google.maps.Size(19,32), new google.maps.Point(0,0), new google.maps.Point(10,32));
    var shadow = new google.maps.MarkerImage('map_marker_shadow.png', new google.maps.Size(28,32), new google.maps.Point(0,0), new google.maps.Point(10,32));
    var bounds = new google.maps.LatLngBounds;
    for (var i in buildings) {
        var myLatLng = new google.maps.LatLng(buildings[i][1], buildings[i][2]);
        bounds.extend(myLatLng);
        var marker = new google.maps.Marker({ position: myLatLng, map: map, shadow: shadow, icon: image, title: buildings[i][0] });
        google.maps.event.addListener(marker, 'click', function() { window.location = ('detail?b=' + buildings[i][3]); });
    }
    map.fitBounds(bounds);
}

Using this code, clicking any marker take the user to 'detail?b=Mjkx'

What am I doing wrong?

I have a map with multiple markers, which I populate via an array. Each marker is clickable and should take the user to a different url per marker.

The problem is that all the markers, while displaying the correct title, all use the url of the last entry in the array.

Here is my code:

var myOptions = { zoom: 9, center: new google.maps.LatLng(40.81940575,-73.95647955), mapTypeId: google.maps.MapTypeId.TERRAIN }
var map = new google.maps.Map(document.getElementById("bigmap"), myOptions);
setMarkers(map, properties);

var properties = [
['106 Ft Washington Avenue',40.8388485,-73.9436015,'Mjg4'],
['213 Bennett Avenue',40.8574384,-73.9333426,'Mjkz'],
['50 Overlook Terrace',40.8543752,-73.9362542,'Mjky'],
['850 West 176 Street',40.8476012,-73.9417571,'OTM='],
['915 West End Avenue',40.8007478,-73.9692155,'Mjkx']];

function setMarkers(map, buildings) {
    var image = new google.maps.MarkerImage('map_marker.png', new google.maps.Size(19,32), new google.maps.Point(0,0), new google.maps.Point(10,32));
    var shadow = new google.maps.MarkerImage('map_marker_shadow.png', new google.maps.Size(28,32), new google.maps.Point(0,0), new google.maps.Point(10,32));
    var bounds = new google.maps.LatLngBounds;
    for (var i in buildings) {
        var myLatLng = new google.maps.LatLng(buildings[i][1], buildings[i][2]);
        bounds.extend(myLatLng);
        var marker = new google.maps.Marker({ position: myLatLng, map: map, shadow: shadow, icon: image, title: buildings[i][0] });
        google.maps.event.addListener(marker, 'click', function() { window.location = ('detail?b=' + buildings[i][3]); });
    }
    map.fitBounds(bounds);
}

Using this code, clicking any marker take the user to 'detail?b=Mjkx'

What am I doing wrong?

Share Improve this question edited Jan 24, 2014 at 4:33 henrywright 10.3k25 gold badges97 silver badges153 bronze badges asked Oct 25, 2009 at 20:30 DaveDave 1611 gold badge4 silver badges8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

The problem is that your click handler is using the i variable from the outer function.

Executing a loop doesn't make a separate copy of the variable in each iteration, so your callback function is using the same value of i for all of the callbacks.

To fix it, write a separate function to generate the callback.

For example:

function buildClickHandler(i) {
    return function() { window.location = ('detail?b=' + buildings[i][3]); };
}

for(var i = 0; i < buildings.length; i++) {
    //...
    google.maps.event.addListener(marker, 'click', buildClickHandler(i));
}

(You'll need to define buildClickHandler inside setMarkers so it can use the buildings parameter)

By the way, when looping through an array, you shouldn't use a for in loop, because it will also iterate over any additional members on the array object.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745435541a4627590.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信