Calculate Distance between two lat/long points with java

1

I'd like to calculate the distance between two locations based on their latitude and longitude (which I can retrieve via the google maps widget). I also have the calculation (and possible java code) to calculate this, however as I am new to Java (and quite new to mendix aswell) I'm not sure how to actually start using this in my application. I found the following formula: function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) { var R = 6371; // Radius of the earth in km var dLat = deg2rad(lat2-lat1); // deg2rad below var dLon = deg2rad(lon2-lon1); var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon/2) * Math.sin(dLon/2) ; var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = R * c; // Distance in km return d; } function deg2rad(deg) { return deg * (Math.PI/180) } How would I got about using this so I can actually start using it to calculate the distances.

asked

Niels van de Sande

2 answers

6

I've converted the code you posted to Java and created a Java action in Mendix (5.8.1) for you here. You can go into your project and right-click the module you want to place it in, then select "import document package" and select this file. After that simply use the Java action it in a microflow with the right input. If the Java code is correct you should get the right result in kilometers.

answered

Jaap Pulleman

3

Create in Mendix a new java action. You should provide two parameters to this java action and receive a result (float).
Now open eclips and refresh (or import if you have not already done so) the project.
You will now see this java action but there will be no code inside but the standard warning from mendix. Copy paste the above code and adjust it so it uses the parameter that where given from mendix and return the calculated value.
When there are no errors in Eclipse you can test your java action.
Hopes this helps a bit,