How do i calculate a distance in a smartform?


Badge +3

I would like to use the new location services in 4.6.10 and use the lat long from the users current location to calculate a distance to a location which will be retrieved by a smartobject and surfaced in a list view.  I would like to add a calcualted column to this list showing the distance in meters from the users device to each item in the list.

 

Any help or guidance would be appreciated.


8 replies

Userlevel 1
Badge +8

Hi NathanPD

 

You may need to look at creating a custom service broker, or even an inline function, that will do this calculation for you. You could create a .net assembly (see link below for code example) that performs a calculation based on the 4 inputs (user latitude, user longitude, item latitude, item longitude) and return a distance in metres. This can then be exposed as a SmartObject method or as an Inline Function. 

 

An inline function would allow you to add an unbound column to your list and set its value as an expression using the function and the required input parameters from the smartobject.

 

http://www.geodatasource.com/developers/vb-dot-net

 

Custom Service Brokers

 

Custom Inline Functions

 

 

Badge +3
I think we cant use inline funtions in smartforms. Its availible only in Workflows.
Userlevel 1
Badge +8

Hi Nagorskiy, yes I think you are correct. In that case I think the custom service broker is the way to go.

Badge +3

Thank you Andrew and Nagorskiy for your answers its a pity you cant have custom inline functions in smartforms - hopefully something for the future!  In terms of the service broker, how would i use it to calculate a column in a list generated from a different smart object?  Im probably missing somthing obvious as my technical knowledge is limited.

Userlevel 5
Badge +16

Hi Nathan,

 

 

 

That requires alot of effort because there are no math functions is smartform (sin, cos...) to calculate the distance between two locations

 

This formula by Bob Chamberlain :

 

dlon = lon2 - lon1
dlat = lat2 - lat1
a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
c = 2 * atan2( sqrt(a), sqrt(1-a) )
d = R * c (where R is the radius of the Earth)

 

 

I guess if you search online on how to  calculate sin and cos without calculator you'll find away to make a complex expression(s) to get you the distance between two locations

I am assuming that you have in your list lat and long for each item

then you add new column and set the expression on it.

 

I prefere that you create multi expressions, for example:

 

one to find dlon and another one to find dlat, etc...

Userlevel 1
Badge +8

Hi Nathan

 

With a SmartObject method you are able to call more than one Service Object methods. So for example in your Create method you can call the default Create method against your business object (e.g. database table) and also call a "Calculate Distance" method against your a Service Object created from your custom service broker.

 

If you edit a SmartObject (in K2 Designer, K2 Studio, VS) and edit a method you will see there is a "Service Object Method Bindings" tab where you can add and edit these Service Object methods.

 

What you might want to do if you can is add a "Distance" field to your source table (e.g. SQL table) that your SmartObject is using, and the modify the Create method to call the additional service object method that calculates the distance. If you call the calculate distance method first and then the create (just change the order they are listed in) then you can save the Distance back to the source table when you Create the record (or update)

 

If you need some further assistance with this let me know.

Badge +3

Thats great!  Thanks for your help ill give that a go.

 

Nathan

Badge +10

Hi Nathan,



Can you log a ticket on this?  I think this would be a great feature request.


 


Regards,


 


Mike

Reply