[英]How to obtain Latitude and Longitude when clicking on a rectangle in Google Maps?
I have created rectangle in Google map and would like to obtain Latitude and Longitude in code when a user clicks on the rectangle. 我已经在Google地图中创建了矩形,当用户单击矩形时,我想在代码中获取纬度和经度。
When I click on outside the rectangle I obtain Latitude and Longitude. 当我在矩形外部单击时,将获得纬度和经度。 When I click on the rectangle however, I'm unable to get Latitude and Longitude.
但是,当我单击矩形时,我无法获得纬度和经度。
Here is My code: 这是我的代码:
var rectangle;
var map;
var infoWindow;
function initialize() {
function calcBounds(center, size) {
var n = google.maps.geometry.spherical.computeOffset(center, size.height / 2, 0).lat(),
s = google.maps.geometry.spherical.computeOffset(center, size.height / 2, 180).lat(),
e = google.maps.geometry.spherical.computeOffset(center, size.width / 2, 90).lng(),
w = google.maps.geometry.spherical.computeOffset(center, size.width / 2, 270).lng();
return new google.maps.LatLngBounds(new google.maps.LatLng(s, w),
new google.maps.LatLng(n, e));
}
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 12,
center: new google.maps.LatLng(40.689055, -89.584747),
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var rectangle = new google.maps.Rectangle({
strokeColor: '#000000',
strokeOpacity: 1,
strokeWeight: 1,
fillColor: '#008000',
fillOpacity: .2,
editable: false,
draggable: false,
map: map,
bounds: calcBounds(new google.maps.LatLngBounds(
new google.maps.LatLng(40.626805, -89.539396),
new google.maps.LatLng(40.626055, -89.538507)).getCenter(), new google.maps.Size(875, 875))
});
var rectangle = new google.maps.Rectangle({
strokeColor: '#000000',
strokeOpacity: 1,
strokeWeight: 1,
fillColor: '#008000',
fillOpacity: .2,
editable: false,
draggable: false,
map: map,
bounds: calcBounds(new google.maps.LatLngBounds(
new google.maps.LatLng(40.750555, -89.633655),
new google.maps.LatLng(40.749805, -89.632766)).getCenter(), new google.maps.Size(875, 875))
});
var rectangle = new google.maps.Rectangle({
strokeColor: '#000000',
strokeOpacity: 1,
strokeWeight: 1,
fillColor: '#008000',
fillOpacity: .2,
editable: false,
draggable: false,
map: map,
bounds: calcBounds(new google.maps.LatLngBounds(
new google.maps.LatLng(40.626805, -89.519833),
new google.maps.LatLng(40.626055, -89.518943)).getCenter(), new google.maps.Size(875, 875))
});
google.maps.event.addListener(map, 'click', function(event) {
alert("Latitude: " + event.latLng.lat() + " " + ", longitude: " + event.latLng.lng());
});
}
google.maps.event.addDomListener(window, 'load', initialize);
Any suggestions are appreciated, thanks! 任何建议表示赞赏,谢谢!
You need to add a click event to each of your rectangles. 您需要为每个矩形添加一个click事件。 For example:
例如:
google.maps.event.addListener(rectangle, 'click', function( event ){
alert( "Latitude: "+event.latLng.lat()+" "+", longitude: "+event.latLng.lng() );
});
You can attach a listener to the rectangle
as you do for the map
. 您可以像在
map
那样将侦听器附加到rectangle
map
。
google.maps.event.addListener(rectangle, 'click', function(event) {
alert('latitude: ' + event.latLng.lat() + ' , longitude: ' + event.latLng.lng());
});
Example here http://jsfiddle.net/0c9rnyf8/5/ 此处的示例http://jsfiddle.net/0c9rnyf8/5/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.