how to get lat long from google map when our own geojson layer is there?

狂风中的少年 提交于 2019-12-04 07:25:59

问题


google.maps.event.addListener(map, 'click', function( event ){
  alert( "Latitude: "+event.latLng.lat()+" "+", longitude:"+event.latLng.lng() ); 
});  

this code gives us lat long when we click on google map. but it does not work when there is our own geojson layer that is if we have our own geojson layer of polygon over google map then clicking inside that polygon the above code would not work


回答1:


Either add your click event listener to the Data layer or set the Data layer to not accept mouse events (clickable:false).

Set event listener on Data layer:

  google.maps.event.addListener(map, 'click', function(event) {
    alert("Latitude: " + event.latLng.lat() + " " + ", longitude: " + event.latLng.lng());
  });
  // Set mouseover event for each feature.
  map.data.addListener('click', function(event) {
    alert("Latitude: " + event.latLng.lat() + " " + ", longitude: " + event.latLng.lng());
  });

proof of concept fiddle

Set Data layer to clickable: false:

map.data.setStyle(function(feature) {
  var color = 'gray';
  var opacity = 0.45;

  return /** @type {google.maps.Data.StyleOptions} */ ({
    fillColor: color,
    strokeColor: "black",
    strokeWeight: 2,
    strokeOpacity: opacity,
    fillOpacity: opacity,
    clickable: false
  });
});

proof of concept fiddle



来源:https://stackoverflow.com/questions/41377642/how-to-get-lat-long-from-google-map-when-our-own-geojson-layer-is-there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!