How to use geo::latLng function in Sanity GROQ query without syntax errors?
The error you're seeing is due to an API version compatibility issue. The geo::latLng() function and other geo functions were introduced in API version v2021-03-25 when Sanity added function namespaces to GROQ. If you're using an older API version, the parser doesn't recognize the geo:: namespace syntax, which causes the "expected '}' following object body" error.
Here's how to fix it:
1. Update your client API version
If you're querying from a client (like @sanity/client), make sure you're using a recent API version:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
apiVersion: '2024-01-01', // Use 2021-03-25 or later
useCdn: false,
})2. Update Vision plugin API version (if testing in Studio)
If you're testing this query in the Vision plugin in Sanity Studio v3, you need to configure it with a compatible API version in your sanity.config.ts:
import {defineConfig} from 'sanity'
import {visionTool} from '@sanity/vision'
export default defineConfig({
// ... other config
plugins: [
visionTool({
defaultApiVersion: '2024-01-01', // Add this - must be 2021-03-25 or later
}),
],
})3. Your query should work once updated
Your query syntax is correct, but note that geo::latLng() creates a geopoint object. You'll typically want to use it with geo::distance() to calculate distances:
*[_type == 'space'] {
_id,
location,
distance: geo::distance(location, geo::latLng(59.911491, 10.757933))
}Or to filter by distance:
*[_type == 'space' && geo::distance(location, geo::latLng(59.911491, 10.757933)) < 5000] {
_id,
location
}No installation needed – geo functions are built into the GROQ API, you just need the right API version. According to the GROQ Functions Reference, all geo functions (including geo::latLng(), geo::distance(), geo::contains(), and geo::intersects()) require API version v2021-03-25 or later.
The key takeaway: always specify an explicit, recent API version in your client configuration and Vision tool settings rather than relying on defaults. This ensures you have access to all modern GROQ features, including the geo namespace functions.
Show original thread33 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.