My Smartapp, under development atm, can determine at any given moment the current Lat/Lon position of my mobile. It also has the Lat/Lon coordinates of my home. My question is, is it possible within groovy , to calculate the distance between the current mobile position and my home? I already have this ability within JS but not sure how I could use (or call) such script within my smartapp.
In case you are wondering. Here is the JS function used to calc the distance
var R2D = 180 / Math.PI;
var D2R = Math.PI / 180;
/**************************************************************************************************/
function groundDistance(toLat,toLon,fromLat,fromLon) {
toLatRad = toLat * D2R
toLonRad = toLon * D2R
fromLatRad = fromLat * D2R
fromLonRad = fromLon * D2R
x = (Math.sin(fromLatRad) * Math.sin(toLatRad)) + (Math.cos(fromLatRad) * Math.cos(toLatRad) * Math.cos(fromLonRad - toLonRad))
if(x >= 1)
return 0
else
return 60 * ((Math.atan(-x / Math.sqrt(-x * x + 1)) + 2 * Math.atan(1)) * R2D)
}
geko
(Geko)
September 6, 2015, 3:12pm
2
Have you tried Google Translate to convert JS to Groovy? (Sorry, could not resist)
3 Likes
sidjohn1
(sidjohn1)
September 6, 2015, 4:40pm
3
Groovy is based off Java. Here are some docs that may help you with the math differences.
http://www.groovy-lang.org/differences.html
http://www.groovy-lang.org/structure.html
viguera
September 6, 2015, 5:19pm
4
Chances are everything you’re trying to do has already been done, or someone has something close that can be easily adapted.
You can probably reuse most of this code:
I needed to check a ninth grader's math homework related to calculating three things about a line between two points. The assignment was to calculate the distance between two points, the slope of a line connecting the two points, and the midpoint on...
Thanks everyone. Decided that since I had an Apache web server with php running I would go that route. Simply:
<?php
$toLat= $_GET[‘toLat’] ;
$toLon= $_GET[‘toLon’] ;
$fromLat= $_GET[‘fromLat’] ;
$fromLon= $_GET[‘fromLon’] ;
$unit= $_GET[‘unit’] ;
function getDist($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == “K”) { // Return Kilometers
return ($miles * 1.609344);
}
else if ($unit == “M”) { // Return Meters
return ($miles * 1609.344);
}
else if ($unit == “F”) { // Return Feet
return ($miles * 5280);
}
else { // Return Miles
return $miles;
}
}
header(‘Content=Type: application/json’); // only way I could get the output trimmed of spaces
echo getDist($toLat, $toLon, $fromLat, $fromLon, $unit);
Then my Smartapp contains:
httpGet("http://www.something.com:portid/getDistance.php?toLat=44&toLon=-77&fromLat=45&fromLon=-78&unit=m") {response ->
def dist = response.data
log.debug "Distance between in Meters: $dist"
}
chuckles
(Chuckles)
September 7, 2015, 1:17pm
7
Haversine function in Groovy:
2 Likes