Contentful logo

Contentful Community

Get address or city/state from location

I’ve added a content type that has a location field, and I’d like to show the location I entered in a friendly format (city/state and/or address). All I get when querying the API is lat/lon. How would I do this without having to query the Google Maps API or something like that?

There are several ways I can think of to do this:

First, instead of using the Location field, you could use a Text field (or several) for editors to input the city/state and/or address as short text.

Or, you could add some sort of reverse geocode script to your webpage/app to turn the coordinates into actual addresses.

1 Like

OK, those are the options I was trying to avoid. In the case of the first option, I’d rather not use a text field, because I like the CMS UI of showing a map where I can search for and visualize the location. With the second option, I was trying to avoid the overhead of undoing work that didn’t need to be done in the first place (geocoding). Is it possible at some point that Contentful could add the option to either 1) store a location field in a non-geocoded format, or 2) store both geolocation and address/city/state/country info? It could present in an object similar to the way that relationships show up.

I’ll add that one major downside of storing the geolocation info is that even on your own interface, it appears you’re doing a reverse geolocation to show the info after it’s been saved and then you come back to look at it, and it doesn’t always get it right. I just encountered a situation just like this where the address was different when I came back.

I’ll definitely bring this feedback to our Product team and I’ll troubleshoot the issue with reverse geolocation not returning consistent addresses with the same coordinates.

I thought of another workaround for your use case but it might be too clunky for you:

  • Set up a Content type with a Location field (since you would like to preserve the map visualizer).
  • You can call this field something like ‘Location Visualizer’ and the help text can say something like ‘Use this map to pick a location and type the address into the field below.’
  • Immediately following the Location field, add a short-text field called ‘Street Address.’

Now, because we don’t want the actual coordinates from the Location field to come back in the API response (we just want to use the map as a visualizer), you can click on the ellipsis next to the Location field > Disable in response. This will disable the Delivery API response for just this field.

What the content type looks like with this setup:

What the editor experience looks like when you create content using this content type:

Yeah, I did something like that to make this work for me. A bit clunky to have to enter it in 2 places, but it works for now. Thanks for your help!

1 Like

Is this still the current state of affairs?

I need this for my project, and it seems that I can only return the lat/lng from this type of field.

Seems kinda silly to be so restrictive on this, when I’m sure the very presence of the Google API / Map will allow for much more data to be returned.

Pretty sure there’d be a lot of use-cases where pulling back the City/ Country of where the pin is dropped would be very useful.

That’s exactly what I need to do now - And it feels like I need to take the lat/lng (From a google api) and pass that to a reverse geocode api (also google) to be able to get the full location details for a pin-point…

Please let me know if anyone has any ideas or input :slight_smile: