1
votes

I am new to Anylogic and I am trying to calculate the distance between two points with given latitude-longitude values. I want the distance in miles. I have found a method from the Anylogic website that is below

default double getDistance(double startLat,
                           double startLon,
                           double endLat,
                           double endLon)

Calculates distance by route between two specified points. Returns: the distance between two specified points, measured in meters.

(source: https://help.anylogic.com/index.jsp?topic=%2Fcom.anylogic.help%2Fhtml%2Fjavadoc%2Fcom%2Fanylogic%2Fengine%2Fgis%2FIGISRouteProvider.html)

However, when I run this for two set of points with lat-long

Point 1:
latitude:41.40174, longitude: -72.0201
Point 2:
latitude:45.332, longitude:-73.2215

this gives me a distance of 4.1098062654025815 meters in anylogic; which is wrong.Could you please help me in giving insights what I might be doing wrong? Thanks

1

1 Answers

1
votes

What is wrong

You are currently using the standard distance function from Utilities, which is using the inputs as cartesian coordinates and not as geographic latitutude and logitude:

public static final double getDistance(double x1,double y1,double x2,double y2)

Returns the distance between two given points (x1, y1) and (x2, y2)
Parameters:x1 - the x coordinate of the first pointy1 - the y coordinate of the first pointx2 - the x coordinate of the second pointy2 - the y coordinate of the second point
Returns:the distance between points

How to fix it

In order to calculate geographic distances with latitude and longitude, you will have to access the functions that come with the ShapeGISMap object:

double  getDistanceByRoute(double latFrom, double lonFrom, double latTo, double lonTo)
Calculates length of route from one point to another.
double  getDistance(double latFrom, double lonFrom, double latTo, double lonTo)
Returns distance, in meters, between 2 given points

You can access them by adding a GIS Map from the SpaceMarkup palette (the instance here named map), and referencing it:

map.getDistanceByRoute(41.40174,-72.0201,45.332,-73.2215);

Additional Hint

You can always check if you are really using the correct function in your context by using Java auto completion (Type the first part of the function name, then CTRL + Space Bar) and having a look in the JavaDoc that is then showing:

Auto Completion