Geospatial Queries / Field

default discord avatar
wiesson
last year
1 1

Does PayloadCMS supports geospatial queries? (https://www.mongodb.com/docs/manual/geospatial-queries/). I'd like to create a custom field that gets lat/lng from an autocompleted address.

Goal: I want to find database entries according to a radius around a point or within a geofence and show them on my website. (For example, show related entries that are nearby)

  • discord user avatar
    DanRibbens
    Payload Team
    last year

    Great question! Look at the docs for the point field.

    https://payloadcms.com/docs/fields/point

    Based on what you said it should get you everything you need.

    3 replies
  • default discord avatar
    wiesson
    last year

    Not sure how I could miss that 😅

  • default discord avatar
    wiesson
    last year

    Btw, any hint (or even better an example), how I could create a custom field that loads data from an API endpoint (based on input) and renders a select field for the addresses?

    Something like this (example taken from graphcms)
    Screenshot 2022-07-03 at 07 38 07

    (I have a react component ready that loads google maps sdk, has an input field, etc...)

    // edit:

    Just found https://github.com/payloadcms/custom-field-guide and https://github.com/payloadcms/public-demo/tree/master/src/fields to look at 👀

  • discord user avatar
    DanRibbens
    Payload Team
    last year

    You got it. The ui field is useful if you don't have data directly on the field and want to show a custom element in the edit view and/or cell views.

    When you do have data to store on the field, you can use whatever field type is best reflected by the data and still write your own custom components as explained in that guide. The main difference is having to update values. UI fields are nice in that you don't have to set values and can just get other field data to display what you need.

Open the post
Continue the discussion in GitHub
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.