-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Finding a point within the LngLat bounds #4278
Comments
Mapbox is a map rendering library, and lack a lot of common geospatial methods. If you want to do geospatial calculations and don't want to re-invent the wheel, I'd recommend turf.js. |
Totally with @kristfal on this one. If you don't care about unwrapped coordinates, you can hack it with |
@Vypah thanks for your feature request. We do recommend using turf.js for spatial analysis queries like the one you've requested. For this use case, I think turf#inside will work for you. |
Thanks for the responses. I still think these kind of handy methods should be a part of mapboxgl itself like the way leaflet provides all these methods. For now I wrote the method myself but having this inside mapboxgl will be really amazing 👍 |
you can extend this method to map instance.
[this] is the map instance. |
I should notice that the You can avoid it with the function below: const inBounds = function (bounds, lnglat) {
let lng;
const multLng = (lnglat[0] - bounds['_ne']['lng']) * (lnglat[0] - bounds['_sw']['lng']);
if (bounds['_ne']['lng'] > bounds['_sw']['lng']) {
lng = multLng < 0;
} else {
lng = multLng > 0;
}
const lat = (lnglat[1] - bounds['_ne']['lat']) * (lnglat[1] - bounds['_sw']['lat']) < 0;
return lng && lat;
}; |
I want something like the following:
but apparently according to
LngLatBounds
documentation https://www.mapbox.com/mapbox-gl-js/api/#LngLatBoundsLngLatBounds.contains
is not a method there. So is there anything else that I can use to determine if aLngLat
is a part of theLngLatBounds
or do I need to implement it myself ?The text was updated successfully, but these errors were encountered: