Skip to content
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

Closed
NoMemoryError opened this issue Feb 15, 2017 · 6 comments
Closed

Finding a point within the LngLat bounds #4278

NoMemoryError opened this issue Feb 15, 2017 · 6 comments

Comments

@NoMemoryError
Copy link

I want something like the following:

const bounds = map.getBounds()
const itemPos = new MapboxGl.LngLat(coordinates.longitude, coordinates.latitude)
bounds.contains(itemPos)

but apparently according to LngLatBounds documentation https://www.mapbox.com/mapbox-gl-js/api/#LngLatBounds LngLatBounds.contains is not a method there. So is there anything else that I can use to determine if a LngLat is a part of the LngLatBounds or do I need to implement it myself ?

@kristfal
Copy link

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.

@Scarysize
Copy link

Totally with @kristfal on this one. If you don't care about unwrapped coordinates, you can hack it with LngLatBounds.extend(). Just call it and check if the bounds changed by extending them 😉

@mollymerp
Copy link
Contributor

@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.

@NoMemoryError
Copy link
Author

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 👍

@ParryQiu
Copy link

ParryQiu commented Feb 2, 2019

you can extend this method to map instance.

  public inBounds(lnglat: [number, number]) {
    const bounds = this.getBounds();
    const lng = (lnglat[0] - bounds['_ne']['lng']) * (lnglat[0] - bounds['_sw']['lng']) < 0;
    const lat = (lnglat[1] - bounds['_ne']['lat']) * (lnglat[1] - bounds['_sw']['lat']) < 0;
    return lng && lat;
  }

[this] is the map instance.

@StasDeep
Copy link

I should notice that the inBounds method above is incorrect. It will fail as soon as you have 180 degree line of longitude inside your bounds.

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;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants