控制键+在谷歌地图上点击事件 [英] Control key + click event on google map
问题描述
我想在谷歌地图上添加一个点击事件,获取基于lat / lng的地址。 Click事件正常工作,但是当我添加检查Alt键被按下时,地理编码逻辑不再有效。
google。 maps.event.addListener(map,'click',function(event){
if(event.altKey){
var geocoder = new google.maps.Geocoder );
var myLat = event.latLng.lat()。toFixed(5);
var myLng = event.latLng.lng()。toFixed(5);
var arrAddress = ;
var thisAddress =Latitude:+ myLat +< br />+
Longitude:+ myLng +< br />;
var i;
geocoder.geocode({'latLng':event.latLng},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
arrAddress = results [0] ['address_components'];
$ .each(arrAddress,funct ion(i,address_component){
if(address_component.types [0] ==route||
address_component.types [0] ==street_number||
address_component.types [0] ==locality||
address_component.types [0] ==postal_code||
address_component.types [0] ==administrative_area_level_1){
thisAddress + = address_component.types [0] +:+ address_component.long_name +< br />;
}
});
fnPlaceMarkers(event.latLng,thisAddress,adhoc);
};
});
};
});
你看看谷歌地图发送给我们的onClick事件。这就是为什么它总是返回false并跳过你的逻辑。
解决方法是使用jquery提供的keydown和keyup回调来存储正在按下的键。
keys = {};
$(document).keydown(function(event){
keys [event.which] = true;
});
$(document).keyup(function(event){
delete keys [event.which];
});
所以你可以检查 alt键
,根据 18 的值代码rel =nofollow>这个网站,代替 event.altKey
。
I would like to add a click event on google maps that gets address based on lat/lng. The click event works correctly, however when I add check for alt key being pressed, the geocode logic no longer works.
google.maps.event.addListener(map, 'click', function(event) {
if (event.altKey) {
var geocoder= new google.maps.Geocoder();
var myLat = event.latLng.lat().toFixed(5);
var myLng = event.latLng.lng().toFixed(5);
var arrAddress = "";
var thisAddress = "Latitude :" + myLat + "<br/>" +
"Longitude: " + myLng + "<br/>";
var i;
geocoder.geocode({'latLng': event.latLng}, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
arrAddress = results[0]['address_components'];
$.each(arrAddress, function (i, address_component) {
if (address_component.types[0] == "route" ||
address_component.types[0] == "street_number" ||
address_component.types[0] == "locality" ||
address_component.types[0] == "postal_code" ||
address_component.types[0] == "administrative_area_level_1") {
thisAddress += address_component.types[0] +":" + address_component.long_name + "<br/>";
}
});
fnPlaceMarkers(event.latLng,thisAddress,"adhoc");
};
});
};
});
}
There are no altKey value if you look at the onClick event that Google Maps sent to us. That's why it always returns false and skip your logic.
A work around would be to store the keys that are being pressed using the keydown and keyup call back provided by jquery.
keys = {};
$(document).keydown(function (event) {
keys[event.which] = true;
});
$(document).keyup(function (event) {
delete keys[event.which];
});
so you can check if the alt key
, which happen to has a value of 18
according to this site, is pressed in replace of the event.altKey
.
这篇关于控制键+在谷歌地图上点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!