admin管理员组文章数量:1323323
I'm trying to create a Google Map with multiple markers on it, that loads an alert when a marker is clicked.
var map = null;
function setupMap() {
map = new GMap2(document.getElementById("map"));
map.setUIToDefault();
map.setCenter(new GLatLng( 0, 0 ), 1);
map.enableDoubleClickZoom();
// Create the marker icon - will be repeated for each icon but
// truncated for brevity in example
var icon1 = new GIcon(G_DEFAULT_ICON);
icon1.image = "uploads/1.jpg";
icon1.shadow = "";
icon1.iconSize = new GSize( 50, 50 );
var latlng = new GLatLng( 0, 0 );
markerOptions = { icon:icon1 };
marker1 = new GMarker( latlng, markerOptions );
map.addOverlay( marker1 );
GEvent.addListener( marker1, "click", loadInfo(1) );
}
function loadInfo( a ) {
alert( a );
}
window.onload = setupMap;
In the working example, I'll pass the marker object to loadInfo() and then load an InfoWindow, but for now, I'm just trying to get the action to happen when the marker is clicked. What's actually happening is that an alert box is loading (with the '1' in it, as expected) when the map loads. Multiple markers don't load multiple alert boxes, and after the initial alert box has loaded (which I don't want) clicking the markers doesn't do anything.
Any help is much appreciated, thanks!
I'm trying to create a Google Map with multiple markers on it, that loads an alert when a marker is clicked.
var map = null;
function setupMap() {
map = new GMap2(document.getElementById("map"));
map.setUIToDefault();
map.setCenter(new GLatLng( 0, 0 ), 1);
map.enableDoubleClickZoom();
// Create the marker icon - will be repeated for each icon but
// truncated for brevity in example
var icon1 = new GIcon(G_DEFAULT_ICON);
icon1.image = "uploads/1.jpg";
icon1.shadow = "";
icon1.iconSize = new GSize( 50, 50 );
var latlng = new GLatLng( 0, 0 );
markerOptions = { icon:icon1 };
marker1 = new GMarker( latlng, markerOptions );
map.addOverlay( marker1 );
GEvent.addListener( marker1, "click", loadInfo(1) );
}
function loadInfo( a ) {
alert( a );
}
window.onload = setupMap;
In the working example, I'll pass the marker object to loadInfo() and then load an InfoWindow, but for now, I'm just trying to get the action to happen when the marker is clicked. What's actually happening is that an alert box is loading (with the '1' in it, as expected) when the map loads. Multiple markers don't load multiple alert boxes, and after the initial alert box has loaded (which I don't want) clicking the markers doesn't do anything.
Any help is much appreciated, thanks!
Share asked May 31, 2009 at 15:28 James InmanJames Inman 1,0024 gold badges17 silver badges32 bronze badges4 Answers
Reset to default 5In your addListener
invocation, you're actually calling loadInfo
instead of passing a reference to it. Try the following instead:
GEvent.addListener( marker1, "click", function() {
loadInfo(1);
});
This will create an anonymous function which wraps your loadInfo
method, calling the method when the anonymous function is executed.
Alternatively, if you weren't using any parameters in loadInfo
, simply removing the parentheses would work too:
GEvent.addListener( marker1, "click", loadInfo);
Something worth keeping in mind when using such function references is the scope in which it will be called. If you were to use the 'this'
reference, you'll run into the situation where 'this'
within the callback function will in fact not refer to the scope in which it was created, but to the scope within which it's being executed which most likely will not contain the fields or methods you're expecting to call, resulting in errors stating Undefined
instead. As Jonathan points out, you'll have to use the call()
and apply()
methods to bind explicitly to ensure your function executes within the correct scope.
You're calling loadInfo in the .addListener, not providing a refernce to it.
GEvent.addListener( marker1, "click", loadInfo(1) );
try:
function wrap(method) {
var args = Array.prototype.slice.apply(arguments,1);
return function () {
return method.apply(this,args);
}
}
GEvent.addListener( marker1, "click", wrap(loadInfo,1) );
see http://www.alistapart./articles/getoutbindingsituations for more information on binding arguments to functions. also see https://developer.mozilla/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/apply for more information on the very useful apply() (also remended you look at call() as well)
loadInfo(1)
means execute this function immediately, but you need to pass a callback function to the GEvent.addListener() method. For that, you can use anonymous function:
GEvent.addListener( marker1, "click", function() { loadInfo(1) });
Even though GEvent.addListener( marker1, "click", function() { loadInfo(1); }); was used, am not getting link to the marker to move next
本文标签: javascriptGoogle Mapsload window on marker clickStack Overflow
版权声明:本文标题:javascript - Google Maps - load window on marker click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742140410a2422557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论