sanitygroq

Is it possible to do geospatial queries in sanity.io?


I found out that sanity supports Geopoint type, but I could not find any information if it's possible to do any filter operation on this type. Are geospatial queries possible?


Solution

  • Simpler syntax for geospatial queries is on the roadmap.

    If you for instance want to make a Lat/Lng bounded query, you can do that the following way, given the schema:

    export default {
       name: 'aDocumentType',
       type: 'document',
       fields: [
        {
           name: 'position',
           type: 'geopoint'
        }
       ]
    }
    

    Let's say we have used Mapbox, and have a LngLatBounds object. Now we can make a query, using the params that maps to this object:

    *[
      _type == "aDocumentType &&
      position.lng < $bounds._ne.lng &&
      position.lat < $bounds._ne.lat &&
      position.lng > $bounds._sw.lng &&
      position.lat > $bounds._sw.lat &&
    ]