使用 JavaScript 打开一个新窗口(谷歌地图),删除以前的事件处理程序
Opening a new window (google maps) using javascript, remove previous event handler
我正在尝试使用地理位置。我正在使用HTML
和javascript
来做到这一点。我想做的是在获得用户的位置后,它将打开一个新选项卡,该选项卡将转到显示方向的谷歌地图。起点是用户的位置,终点已经给出。我在谷歌地图网址中使用了saddr
和daddr
。我正在使用 Chrome 浏览器进行测试,并且我已经允许获取我的位置。
这是我的代码
对于 HTML
<span class="glyphicon glyphicon-road" id="getdir1" onclick="getLocation()" style="font-size:15px;"> Get Direction1</span>
<span class="glyphicon glyphicon-road" id="getdir2" onclick="getLocation()" style="font-size:15px;"> Get Direction2</span>
<span class="glyphicon glyphicon-road" id="getdir3" onclick="getLocation()" style="font-size:15px;"> Get Direction3</span>
<p id="geo-error"></p>
这是我的JavaScript位置提供是我的终点的地址。我手动编码了它。
<script>
var x = document.getElementById("geo-error");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var latlon = position.coords.latitude + "," + position.coords.longitude;
$("#getdir1").click() = function(){
window.open('http://maps.google.com/maps?saddr='+latlon+"&daddr=LocationProvided1");
}
$("#getdir2").click(function(){
window.open('http://maps.google.com/maps?saddr='+latlon+"&daddr=LocationProvided2");
});
$("#getdir3").click(function(){
window.open('http://maps.google.com/maps?saddr='+latlon+"&daddr=LocationProvided3");
});
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
我的问题是当我第一次单击它时,它将打开一个窗口,当我再次单击它时,它将打开两个显示位置的窗口。我很困惑为什么当我第二次单击它时它会同时打开两个窗口?
您可以使用 Jquery off
取消以前的处理程序。
http://api.jquery.com/off/
.off() 方法删除附加了 .on().
我经常使用它来避免由于多个函数调用一次又一次地附加同一个处理程序而导致堆叠处理程序的问题。
例:
$(myElem).off("click").on("click", function (evt) {...});
注意不要删除其他插件的现有处理程序myElem
问题是你通过onclick和$('element').click()绑定点击事件
我已经修改了您的代码,例如通过将元素传递给函数来简化业务,您甚至不需要jQuery。请检查并让我知道
var x = document.getElementById("geo-error");
function getLocation(address) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) { //call back success
var latlon = position.coords.latitude + "," + position.coords.longitude;
window.open('http://maps.google.com/maps?saddr=' + latlon + '&daddr=' + address)
}, function(error) { //call back error
switch (error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
});
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
<span class="glyphicon glyphicon-road" id="getdir1" onclick="getLocation('LocationProvided1')" style="font-size:15px;"> Get Direction1</span>
<span class="glyphicon glyphicon-road" id="getdir2" onclick="getLocation('LocationProvided2')" style="font-size:15px;"> Get Direction2</span>
<span class="glyphicon glyphicon-road" id="getdir3" onclick="getLocation('LocationProvided3')" style="font-size:15px;"> Get Direction3</span>
<p id="geo-error"></p>
发现了我的错误
我添加了.off('click'),就像@Christophe Roussy所说的那样,在我的window.open()
中也应该有一个名称参数。
window.open(url, name)
每次我调用 URL 时,它都会转到我输入的名称。由于我放置了一个 NAME 参数,它将覆盖它打开的窗口的 URL。
我输入相同的名称值。
- 删除特定的谷歌地图标记
- 删除谷歌地图热图的光晕效果
- 删除谷歌地图地点服务中的标记
- 用于删除谷歌地图标记的反向动画
- 如何删除谷歌地图中的特定多边形添加事件监听器右键单击
- 动态删除谷歌地图中的特定标记
- 刷新/删除谷歌地图API上的标记
- 按标签删除谷歌地图标记
- 可以从元素中删除谷歌地图吗?
- 通过下拉选择删除谷歌地图标记
- 如何获得id时点击并删除?谷歌地图V3
- 删除谷歌地图自定义标记上的鼠标
- 我怎样才能完全删除谷歌地图弹出气球
- 加入搜索框&删除谷歌地图API Javascript中的标记
- 删除谷歌地图圆圈/形状
- 删除谷歌地图中绘制的多边形
- 无法删除谷歌地图中的事件侦听器
- 如何删除谷歌地图公司信息
- 删除谷歌地图的DataLayer顶点
- 不能删除谷歌地图标记