[英]Get coordinates from Google Maps API 3 with rightclick event
I have this simple maps code, but for some reason on the rightclick event the alert is not triggered. 我有这个简单的地图代码,但是由于某些右键事件,警报不会触发。 Could someone help spotting the problem and explaining it? 有人可以帮助发现问题并进行解释吗?
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script>
var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644)
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addListener(map, 'rightclick', function(event) {
var lat = event.latLng.lat();
var lng = event.latLng.lng();
alert("Lat=" + lat + "; Lng=" + lng);
});
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
You create map
inside the function initialize
, so you must add the listener also in initialize
, otherwise map
will be undefined
at the moment when you try to add the listener. 您在函数initialize
创建了map
,因此必须在initialize
也添加侦听initialize
,否则当您尝试添加侦听器时map
将是undefined
的。
<script>
var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644)
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
google.maps.event.addListener(map, 'rightclick', function(event) {
var lat = event.latLng.lat();
var lng = event.latLng.lng();
alert("Lat=" + lat + "; Lng=" + lng);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
Here is an example 这是一个例子
<head>
<style>
html, body, #map-canvas {
height: 400px;
margin: 0px;
padding: 0px
}
#display {
width: 100%;
}
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?">
</script>
<script>
var map;
var myLocation = { // Belgium
'latitude': 50.5,
'longitude': 4.5
};
var myLatlng = new google.maps.LatLng(myLocation.latitude, myLocation.longitude);
var mapOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
function initialize() {
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var displayElement = document.getElementById('display');
// Add a right click event
// @see http://stackoverflow.com/questions/7168394/google-map-v3-context-menu
var contextMenu = google.maps.event.addListener(
map,
"rightclick",
function( event ) {
// just as an example, I write the data to the input
displayElement.value =
'mouse: ' + event.pixel.x +','+ event.pixel.y +
' ||| coordinates: ' + event.latLng.lat() +','+ event.latLng.lng() ;
}
);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<input id="display">
<div id="map-canvas"></div>
</body>
You need to define right click function. 您需要定义右键单击功能。
/*
1 = Left mouse button
2 = Centre mouse button
3 = Right mouse button
*/
$([selector]).mousedown(function(e) {
if (e.which === 3) {
/* Right mouse button was clicked! */
}
});
See How to distinguish between left and right mouse click with jQuery 请参阅如何使用jQuery区分鼠标左键和鼠标右键
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.