gpt4 book ai didi

javascript - 在 Drupal Gmap map 上添加事件监听器时遇到问题

转载 作者:行者123 更新时间:2023-11-28 02:47:09 24 4
gpt4 key购买 nike

我正在尝试编写一个 Drupal 模块,将 UTM 坐标字段添加到位置表单中,以便它们能够与 GMap map 以及纬度和经度字段正确交互 - 即,当单击 map 时, UTM 坐标与纬度/经度坐标一起进行计算和填充,如果通过在字段中键入来更改纬度/经度坐标,则 UTM 坐标以及 map 也会发生变化,反之亦然。

除了点击 map 之外,我已经完成了翻译和其他所有工作。我正在尝试向 map 添加一个监听器,以便在单击它时,将在纬度/经度字段上触发更改事件,从而触发 UTM 字段的更新。但是,我似乎无法让监听器工作。这是我到目前为止所拥有的(这是我的模块的 js 文件的片段,location_utm.js):

$(document).ready(function() {
var themap = document.getElementById("gmap-auto1map-gmap0");

Drupal.gmap.addHandler('gmap', function (themap) {
var obj = this;

var clickListener = GEvent.addListener(obj, "click", function() {

/* when the map gets clicked, trigger change event on the lat/long
fields so that the utm fields get updated too. */
$('#gmap-auto1map-locpick_longitude0').change();
$('#gmap-auto1map-locpick_latitude0').change();



});
});
});

我已经尝试了这段代码的许多不同的细微变化,但似乎无法得到正确的结果。我很感激任何建议。

最佳答案

请查看代码注释:

if (GBrowserIsCompatible()) 
{
Drupal.gmap.addHandler('gmap', function(elem, context) {
var gmap = this;

// Note: GMap module does not support solely
// "locpickchange_dragend" and "locpickchange_click" events
// by default. It combines map clicks, marker drag and
// dragend events in an custom event called "locpickchange".
// Binding on those said solely triggers relies on a GMap
// locpick widget patch which is included.
//
gmap.bind('locpickchange', function(context) {

// Note: The coordinations stored in gmap.vars.latitude and
// gmap.vars.longitude are for the previous location of the
// locpicker marker, we need to use gmap.locpick_coord which
// is pretty live!
//
if (gmap.locpick_coord) {

// TODO: Implement the logic.
// Current latitude: gmap.locpick_coord.lat()
// Curren longitude: gmap.locpick_coord.lng()

}
});
}

如果您想使用locpickchange_clicklocpickchange_dragend,这里是脏GMap模块的补丁:

From 2fb5a1ca71e1470e5413f10fb83ce959cd1d8634 Mon Sep 17 00:00:00 2001
From: Sepehr Lajevardi <me@sepehr.ws>
Date: Fri, 8 Jul 2011 14:38:27 +0430
Subject: [PATCH] Extends the locpick widget event system by adding
lockpickchange_click and locpickchange_dragend custom
events.

---
js/locpick.js | 18 ++++++++++++++++++
1 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/js/locpick.js b/js/locpick.js
index d5aae9c..7c207f9 100644
--- a/js/locpick.js
+++ b/js/locpick.js
@@ -16,6 +16,18 @@ Drupal.gmap.addHandler('gmap', function (elem) {
}
});

+ // Bind triggering of a map click on our custom events.
+ obj.bind("locpickchange_dragend", function () {
+ if (obj.locpick_coord) {
+ GEvent.trigger(obj.map, "click", null, obj.locpick_coord);
+ }
+ });
+ obj.bind("locpickchange_click", function () {
+ if (obj.locpick_coord) {
+ GEvent.trigger(obj.map, "click", null, obj.locpick_coord);
+ }
+ });
+
obj.bind("locpickremove", function () {
obj.map.removeOverlay(obj.locpick_point);
obj.locpick_coord = null;
@@ -40,10 +52,16 @@ Drupal.gmap.addHandler('gmap', function (elem) {
GEvent.addListener(obj.locpick_point, 'dragend', function () {
obj.locpick_coord = obj.locpick_point.getLatLng();
obj.change('locpickchange', binding);
+ // Also trigger a locpickchange_dragend event
+ // so we can bind on just marker dragends.
+ obj.change('locpickchange_dragend', binding);
});
obj.locpick_coord = point;
obj.map.panTo(point);
obj.change('locpickchange', binding);
+ // Also trigger a locpickchange_click event
+ // so we can bind on just map clicks.
+ obj.change('locpickchange_click', binding);
}
else {
// Unsetting the location
--
1.7.6

关于javascript - 在 Drupal Gmap map 上添加事件监听器时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4652982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com