How to use geo::latLng function in Sanity GROQ query without syntax errors?

33 replies
Last updated: May 5, 2021
Hello I’m having issue with querying geopoint. This is an example query:
*[_type == 'space'] {

_id,

dis: geo::latLng(59.911491, 10.757933)

}

But this simple query is giving me error:
expected '}' following object body

Is there anything that I do wrong? or anything I need to install to enable the geo function?
AI Update

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 thread
33 replies
Hi Welly! Are you by any chance using the
v1
version of the API? geo-support was added in
v2021-03-25
You would also need to add “dis” in quotes, so
*[_type == 'space'] {
  _id,
  "dis": geo::latLng(59.911491, 10.757933)
}
should work
How do I check v1 or v2?
I’ve tried the “dis” but it still doesnt work
How are you running your query?
I’m trying the query in the sanity vision
Aha, in Vision you should see API Version as a dropdown on top. But that was added recently
Which version of
@sanity/vision
are you on?
I’m using the 2.10.0 version
Do you have this drop down above the query editor?
I don’t
Right, then it would default to
v1
. Can you try and delete the node_modules folder and reinstall the packages with
sanity install
before running
sanity start
again?
Sure let me try first
Now I’m getting
@sanity/core not installed in current project
when I run
sanity start
because I deleted the whole node_modules
Did you run
sanity install
?
yes but I only got
✔ Saved lockfile
Nothing is installed in the node_modules dir
That is weird 🤔 Can you try
yarn install
manually?
Here is what I got:
[1/4] :mag:  Resolving packages...

success Already up-to-date.

:sparkles:  Done in 0.53s.
Do you have something in node_modules now?
Nothing in the node_modules dir now
Which version of node are you on?
I’m using v14.16.1
Hm, very strange! Can you try
yarn cache clean
deleting node_modules and then do a
sanity install
?
Let me try
It’s still the same, empty node_modules
hmm. Can you also remove the
yarn.lock
-file? 🤔 Starting to run out of options I can think of 😅 Are you by any chance inside a yarn workspace?
Removing the lock file works!
Is there any breaking changes between v1 and v2?
Hurray! And did you get the api version in Vision?
Yes, the geo function is already working well. Thanks a lot!
🙂

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.

Was this answer helpful?