我使用 Maplace.js,一个用于 jQuery 的 Google map Javascript 插件。 插件中有一个功能可以在 map 上显示标记。
//triggers to show a location in map
Maplace.prototype.ViewOnMap = function (index) {
//view all
if (index === this.view_all_key) {
this.o.beforeViewAll();
this.current_index = index;
if (this.o.locations.length > 0 && this.o.generate_controls && this.current_control && this.current_control.activateCurrent) {
this.current_control.activateCurrent.apply(this, [index]);
}
this.oMap.fitBounds(this.oBounds);
this.CloseInfoWindow();
this.o.afterViewAll();
//specific location
} else {
index = parseInt(index, 10);
if (typeof (index - 0) === 'number' && index > 0 && index <= this.ln) {
try {
google.maps.event.trigger(this.markers[index - 1], 'click');
} catch (err) {
this.debug('ViewOnMap::trigger', err.stack);
}
}
}
return this;
};
我尝试调用此方法但无济于事:
<a href="javascript:Maplace.prototype.ViewOnMap(2)">Link</a>
如何通过带有 key 的链接进行调用?
最佳答案
你可以这样做:
<a class="openmap" href="#">Link</a>
和js:
$('.openmap').on('click', function(e){
e.prevetDefault();
Maplace.prototype.ViewOnMap = function (index) {
//view all
if (index === this.view_all_key) {
this.o.beforeViewAll();
this.current_index = index;
if (this.o.locations.length > 0 && this.o.generate_controls && this.current_control && this.current_control.activateCurrent) {
this.current_control.activateCurrent.apply(this, [index]);
}
this.oMap.fitBounds(this.oBounds);
this.CloseInfoWindow();
this.o.afterViewAll();
//specific location
} else {
index = parseInt(index, 10);
if (typeof (index - 0) === 'number' && index > 0 && index <= this.ln) {
try {
google.maps.event.trigger(this.markers[index - 1], 'click');
} catch (err) {
this.debug('ViewOnMap::trigger', err.stack);
}
}
}
return this;
};
});
关于javascript - js :Calling a function on click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26746089/