Hi all,
I am trying to use the geo_distance function with fields from a custom module. Usually you would use this with HubDB rows like this:
{{ row.location|geo_distance(request.query_dict[‘lat’], request.query_dict[‘lng’], “mi”) }}
For my situation I want to be able to define a location using fields from a custom module, like this:
{% set location = {
lat: item.location_lat,
lon: item.location_lon
} %}
{{ location|geo_distance(request.query_dict[‘lat’], request.query_dict[‘lng’], “mi”) }}
I have tried setting the location lat and lon values to strings and floats without success.
Is there a way to create an object containing lat and lon values that can be used with the geo_distance function?
@miljkovicmisa
Would you be able to help out here?
Hello @JustinDowty , hi @dennisedson ,
I’m afraid you’ve hit a limitation here, if you have tried to create a location in hubdb you’re probably familiar with the map interface that pops up and prompts you to select a location, by doing that, hubdb automatically creates a special type of object called…you guessed it, “Location”, it looks something like the following:
(Location: {lat=35.51315, lon=24.004864, name=null, objectAsEncodedString=35.51315,24.004864, objectMap={lat=35.51315, long=24.004864}, type=location})
So this kind of object is not possible to create in hubspot without the support of hubdb which does it automagically. Without this object you won’t be able to leverage the geo_distance filter or function because it requires it’s value to be a location object which is also probably the error you see in the hubspot editor if you worked there.
However, I’m going to get a lot technical here, bear with me, if you do need to do this by yourself you can replicate what hubspot does with geo_distance, it is called the Haversine formula, its basically what hubspot docs describe as “the ellipsoidal 2D distance between two points on Earth”.
You could theoretically replicate that using javascript, there is this answer that managed to do that.
I’m not sure what your exact case is and what you need to accomplish in your particular situation, so if you go down this road it depends, you should think it through before jumping over an existing solution that is hubdb.
Don’t hesitate to write here if you need more help with this.
If my answer was helpful, please mark it as a solution.
Hey @miljkovicmisa,
Thanks for the detailed response. This is what I was afraid of, I was hoping there would be a way to “trick” the system so to speak. For my use-case javascript is not ideal, I am making a custom module for a theme and since I will be sorting and paginating with HubL ideally, javascript would be a mess. Also HubDB tables aren’t a great option either since at this time a theme can’t ship with a HubDB table, which would be necessary for the user to easily have a HubDB table to use with a module requiring specific HubDB structure.
For what it is worth, a few features that I expected to be there would solve for my use case:
- Location field type in custom modules (that works with geo_distance)
- The ability to use geo_distance with hubl object
- The ability to include HubDB tables along with themes
Thanks again for the response.