简体   繁体   English

在Google地图中单击矩形时,如何获取经度和纬度?

[英]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);

Here is an example 这是一个例子

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.

相关问题 如何使用Google Maps API获取所选地点的“纬度和经度”? - How can I obtain the “latitude and longitude” of the selected place with Google Maps API? 如何获取经度和纬度的谷歌在Javascript地图 - how to get longitude and latitude of google maps in javascript 经度和纬度搜索Google地图 - Longitude and Latitude search for google maps 如何获得与Google Maps URL的纬度和经度值相同的Google Geocoder API几何值(经度和纬度) - How to get the Google Geocoder API geometry values (latitude and longitude) same as Google maps URL latitude and longitude values 从纬度和经度值构建时,谷歌地图不显示路线 - Google Maps not showing route when building from latitude and longitude values 当不硬编码纬度和经度时,谷歌地图上的图钉会消失 - Pin on google maps disappears when not hard coding the latitude and longitude 如何从函数获取纬度经度并将其发送到新的google.maps.LatLng(latitude,longitude)javascript - how to get latitude longitude from function and send it to new google.maps.LatLng( latitude ,longitude ) javascript Google Maps - 如何向纬度/经度添加数字以更改坐标 - Google Maps - How to add a number to a latitude/longitude to change the coordinate 如何在Google Maps Geolocation API中获取笔记本电脑(wifi)的纬度和经度? - How get latitude and longitude of laptop (wifi) in Google Maps Geolocation API? 如何验证您在Google地图上指定的经度和纬度上到达的位置 - How to Validate Your Position Arrived on Tasked Latitude and Longitude on Google Maps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM