DISTANCE function

If you have questions or if you want to share your opinion about Aware IM post your message on this forum
Gabbitas
Posts: 334
Joined: Sun Jan 03, 2010 3:36 am

DISTANCE function

Post by Gabbitas »

Hi All,

I'm starting to experiment with the new distance function and trying to understand how it can be used. I have successfully managed to get the current location stored against the logged in user and I can create tracking records stored in a BO. I can also calculate and display the distance between the logged in user and a client address.

What I would really like to do is have a list of clients display that are close to the currently logged in user. Below is the rule inside a query that I've tried to use to accomplish this:

FIND Client WHERE (DISTANCE(Client.Address, LoggedInSystemUser.CurrentLocation)<1000)


When I run the query I get the following error:

"Internal error. Invalid parameters to DISTANCE function"


The Client.Address attribute is an address separated by commas eg streetname, town, county, postcode. I have tried removing the commas to see if that makes a difference but no luck. The SystemUser.CurrentLocation is just a geolocation string as stored by awareIM. I guess I'm most likely approaching this in the wrong way and/or the function can't be used inside the rules or a query.

Does any one have any ideas on where I'm going wrong here and how I might be able to display a list of clients close to the user? Also, in an ideal world it would be great to order the list with the closest client showing at the top of the list?

Thanks
aware_support
Posts: 7523
Joined: Sun Apr 24, 2005 12:36 am
Contact:

Post by aware_support »

Both parameters to the DISTANCE functions must be strings. The error message indicates that one of the parameters is not a string.
Aware IM Support Team
Gabbitas
Posts: 334
Joined: Sun Jan 03, 2010 3:36 am

Post by Gabbitas »

Hi,

I use virtually the same rule and the same attributes to calculate and display the distance from the user to a single client address and this works fine so I'm confident that the parameters to the function are both strings.

After further studying the rule I'm tiring to use I just don't think it's written right. However, I can't figure out how to write this in a different way to get the result I need. If any one is able to make any suggestion on how I might achieve my goal I would be very grateful

Thanks
Rennur
Posts: 1191
Joined: Thu Mar 01, 2012 5:13 am
Location: Sydney, Australia

Post by Rennur »

Try using REPLACE_PATTERN but I'm not sure if it would with with the FIND action.

FIND Client WHERE (DISTANCE(REPLACE_PATTERN(Client.Address,',',' '), REPLACE_PATTERN(LoggedInSystemUser.CurrentLocation,',' '))<1000)

Or

Create a Not Persisted BO (Gmap) with the Gmap.ClientAddress and Gmap.CurrentLocation plain text attributes.

Then in your process you create the BO first and run the FIND action against this BO.

CREATE Gmap WITH Gmap.CurrentLocation=REPLACE_PATTERN(LoggedInSystemUser.CurrentLocation,',' '), Gmap.ClientLocation=REPLACE_PATTERN(Client.Address,',',' ')

FIND Client WHERE DISTANCE(Gmap.CurrentAddress, Gmap.ClientAddress)<1000
yuri.rj
Posts: 223
Joined: Tue Jan 11, 2011 8:05 pm

Post by yuri.rj »

How did you get the CurrentLocation????
yuri.rj
Posts: 223
Joined: Tue Jan 11, 2011 8:05 pm

Post by yuri.rj »

Sorry, my bad.... didn't know it already exist on AwareIM
bondicoffee
Posts: 130
Joined: Tue Dec 03, 2013 10:19 pm

Re: DISTANCE function

Post by bondicoffee »

Hi,

We are getting the below error on using the Distance function in Awareim which was working earlier perfectly in earlier versions of AwareIM.

We are currently using Build 2571 v8.2. Following is the rule causing the below error :
Orders.Distance=DISTANCE(Orders.ps_FromAddress.Address,Orders.InstallationAddress)/1000

Can someone please advise if this is a known bug or we need to enable some setting in AwareIm?

XML returned by Google is <?xml version="1.0" encoding="UTF-8"?><DirectionsResponse> <status>REQUEST_DENIED</status> <error_message>You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account</error_ ... nsResponse>
java.lang.Exception: Status returned is REQUEST_DENIED
at com.bas.shared.functions.GoogleLibrary.calculateDistance(GoogleLibrary.java:182)
at com.bas.shared.functions.GoogleLibrary.calculate(GoogleLibrary.java:112)
at com.bas.basserver.executionengine.rliproc.SystemFunctionResolver.calculate(Unknown Source)
at com.bas.shared.ruleparser.ASTSystemFunction.calculate(ASTSystemFunction.java:152)
at com.bas.shared.ruleparser.ASTDivNode.calculate(ASTDivNode.java:92)
at com.bas.shared.ruleparser.ASTModifyActionStatement.calculate(ASTModifyActionStatement.java:431)
at com.bas.shared.ruleengine.ModifyAction.calculate(ModifyAction.java:200)
at com.bas.basserver.executionengine.rliproc.G.C(Unknown Source)
at com.bas.basserver.executionengine.rliproc.G.T(Unknown Source)
at com.bas.basserver.executionengine.rliproc.G.A(Unknown Source)
at com.bas.basserver.executionengine.rliproc.G.execute(Unknown Source)
at com.bas.basserver.executionengine.ExecutionEngine.A(Unknown Source)
at com.bas.basserver.executionengine.ExecutionEngine.A(Unknown Source)
at com.bas.basserver.executionengine.ExecutionEngine.A(Unknown Source)
at com.bas.basserver.executionengine.ExecutionEngine.doRecalculateObject(Unknown Source)
at com.bas.basserver.executionengine.ExecutionEngine.recalculateObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.bas.basserver.bsmanager.E.B(Unknown Source)
at com.bas.basserver.bsmanager.E.A(Unknown Source)
at com.bas.basserver.bsmanager.E.A(Unknown Source)
at com.bas.basserver.iopipes.ComponentSink.processMessage(Unknown Source)
at org.openadaptor.adaptor.STPipelineController.processMessage(STPipelineController.java:68)
at com.bas.openadaptor.NonTransactionalController.processMessage(NonTransactionalController.java:136)
at org.openadaptor.adaptor.AbstractSimplePipe.processMessage(AbstractSimplePipe.java:222)
at org.openadaptor.adaptor.STPipelineController.processMessage(STPipelineController.java:68)
at com.bas.openadaptor.NonTransactionalController.processMessage(NonTransactionalController.java:136)
at org.openadaptor.adaptor.AbstractSimpleSource.processMessage(AbstractSimpleSource.java:403)
at org.openadaptor.adaptor.AbstractSimpleSource.sourceProcess(AbstractSimpleSource.java:1049)
at org.openadaptor.adaptor.AbstractSimpleSource.sourceProcess(AbstractSimpleSource.java:1012)
at com.bas.openadaptor.MTJMSListener$JMSMessageHandler.run(MTJMSListener.java:275)
at java.lang.Thread.run(Thread.java:745)


Thanks in advance!
aware_support
Posts: 7523
Joined: Sun Apr 24, 2005 12:36 am
Contact:

Re: DISTANCE function

Post by aware_support »

This should be fixed in build 2573. You now need a third parameter to the DISTANCE function - Google Maps key. For example,

DISTANCE (Object.Location1, Object.Location2, SystemSettings.GoogleMapsKey)
Aware IM Support Team
bondicoffee
Posts: 130
Joined: Tue Dec 03, 2013 10:19 pm

Re: DISTANCE function

Post by bondicoffee »

Hi,

Thank you for the patch update. However, this is still not working.

I added a key in system settings and tried the below rule in test version as well as published version. However, I am getting the same error.


Following is rule updated :
Orders.Distance=DISTANCE(Orders.ps_FromAddress.Address,Orders.InstallationAddress,SystemSettings.GoogleMapsKey)/1000

Error:
Exception thrown com.bas.connectionserver.server.BASServerException com.bas.shared.ruleparser.ParseException Status returned is REQUEST_DENIED
com.bas.connectionserver.server.BASServerException: com.bas.shared.ruleparser.ParseException Status returned is REQUEST_DENIED
at com.bas.connectionserver.server.ConnectionFactory.sendMessage(ConnectionFactory.java:275)
at com.bas.connectionserver.server.ConnectionFactory.sendMessage(ConnectionFactory.java:233)
at com.bas.webapp.common.WebAppUtils.sendMessageToExecutionEngine(Unknown Source)
at com.bas.webapp.common.WebAppUtils.sendMessageToExecutionEngine(Unknown Source)
at com.bas.webapp.thin.handlers.C.O.A(Unknown Source)
at com.bas.webapp.thin.handlers.XMLRequestHandler.A(Unknown Source)
at com.bas.webapp.thin.handlers.XMLRequestHandler.handleRequest(Unknown Source)
at com.bas.webapp.thin.servlets.WebInterfaceServlet.doPost(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)


Looking forward to hearing from you.
aware_support
Posts: 7523
Joined: Sun Apr 24, 2005 12:36 am
Contact:

Re: DISTANCE function

Post by aware_support »

Did you specify a valid key?

What is the exact response returned by Google? (this response should be in the output of the Aware IM Server - just like in your original report - something like this:

XML returned by Google is <?xml version ....
Aware IM Support Team
bondicoffee
Posts: 130
Joined: Tue Dec 03, 2013 10:19 pm

Re: DISTANCE function

Post by bondicoffee »

Apologies for sending incomplete error details.

Following is the error recieved :
XML returned by Google is <?xml version="1.0" encoding="UTF-8"?><DirectionsResponse> <status>REQUEST_DENIED</status> <error_message>Requests to this API must be over SSL. Load the API with "https://" instead of "http://".</error_message></DirectionsResponse>
java.lang.Exception: Status returned is REQUEST_DENIED
at com.bas.shared.functions.GoogleLibrary.calculateDistance(GoogleLibrary.java:180)


I believe to fix this, the following code has to be fixed at com.bas.shared.functions.GoogleLibrary.java file which is present inside shared.jar file,
"String urlStr = "http://maps.googleapis.com/maps/api/directions/xml?origin=" +
URLEncoder.encode(addressFrom, "UTF-8") +
"&destination=" + URLEncoder.encode(addressTo, "UTF-8") +
"&units=metric&sensor=false";
"

Suggested change:
String urlStr = "https://maps.googleapis.com/maps/api/directions/xml?origin=" +
URLEncoder.encode(addressFrom, "UTF-8") +
"&destination=" + URLEncoder.encode(addressTo, "UTF-8") +
"&units=metric&sensor=false";
aware_support
Posts: 7523
Joined: Sun Apr 24, 2005 12:36 am
Contact:

Re: DISTANCE function

Post by aware_support »

Please try this in build 2574
Aware IM Support Team
yahya
Posts: 77
Joined: Sat Jul 16, 2011 6:00 am
Location: South Africa
Contact:

Re: DISTANCE function

Post by yahya »

Am doing a simple map using the predefined element where I am using a query to find a place and selecting the field with is a the location the field is a text.

Get this error message in the Java Console of the browser.

Geocoding Service: This API project is not authorized to use this API. For more information on authentication and Google Maps JavaScript API services please see: https://developers.google.com/maps/docu ... et-api-key js:70:128
xd https://maps.googleapis.com/maps/api/js ... *******:70
d https://maps.googleapis.com/maps-api-v3 ... coder.js:2
c https://maps.googleapis.com/maps-api-v3 ... mmon.js:69
<anonymous> https://maps.googleapis.com/maps/api/js ... ch?4sParis, France&7sUS&9sen-US&callback=_xdc_._ssnuog&key=AIzaSyCa9xifIxgz********************&token=32174:1
AwareIM Developer edition. Version 8.5 (Build 2827) running on Windows Server 2012 R2 Standard
aware_support
Posts: 7523
Joined: Sun Apr 24, 2005 12:36 am
Contact:

Re: DISTANCE function

Post by aware_support »

Your Google Maps key is wrong - it must have access to Google Directions API enabled.
Aware IM Support Team
yahya
Posts: 77
Joined: Sat Jul 16, 2011 6:00 am
Location: South Africa
Contact:

Re: DISTANCE function

Post by yahya »

Did not have the Directions API enabled but now it is enabled, and now have got the following API's enabled, yet :
Cloud Messaging
Firebase Cloud Messaging API
Geolocation API
Maps JavaScript API
Maps SDK for Android
Maps SDK for iOS
Places API
Directions API

Geocoding Service: This API project is not authorized to use this API. For more information on authentication and Google Maps JavaScript API services please see: https://developers.google.com/maps/docu ... et-api-key js:70:128
xd https://maps.googleapis.com/maps/api/js ... *******:70
d https://maps.googleapis.com/maps-api-v3 ... coder.js:2
c https://maps.googleapis.com/maps-api-v3 ... mmon.js:69
<anonymous> https://maps.googleapis.com/maps/api/js ... ch?4sParis, France&7sUS&9sen-US&callback=_xdc_._ssnuog&key=AIzaSyCa9xifIxgz****************&token=21255:1
AwareIM Developer edition. Version 8.5 (Build 2827) running on Windows Server 2012 R2 Standard
Post Reply