Check if marker is in view (map) - mapbox

爱⌒轻易说出口 提交于 2019-12-22 01:15:45

问题


I want to check if marker is on mapview or out of the map. I am putting marker of the map and not able to check if it on currentView or not. I have tried below code. this.map.getBounds().contains(e.layer.getLatLng()); but it is returning me true. map is returning its old lat lng i.e bounds (I think so)


回答1:


map.getBounds().contains(myMarker.getLatLng())

  • map.getBounds(): Returns the LatLngBounds of the current map view.

  • latLngBounds.contains(): Returns true if the rectangle contains the given point.

  • myMarker.getLatLng(): Returns the current geographical position of the marker.

See also: area estimation in viewpoint of map using leaflet




回答2:


I use custom function:

function inBounds(point, bounds) {
  var lng = (point.lng - bounds._ne.lng) * (point.lng - bounds._sw.lng) < 0;
  var lat = (point.lat - bounds._ne.lat) * (point.lat - bounds._sw.lat) < 0;
  return lng && lat;
}


来源:https://stackoverflow.com/questions/37269764/check-if-marker-is-in-view-map-mapbox

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