I have one place with latitude and longitude. I would like to find nearest sea coast distance miles/km to my place using google Map API.
Is this any solutions regarding this in php?
More Descriptions:
Is there anyway to know the distance to the nearest sea coast point (that is the minimun distance from the sea)?
Thanks in advance.
Use this function to calculate distance between long and lat
function distance($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 ($miles * 1.609344); } else if ($unit == "N") { return ($miles * 0.8684); } else { return $miles; } } echo distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles<br>"; echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>"; echo distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles<br>";
Please try
$dist = "https://maps.googleapis.com/maps/api/directions/json?key=&origin=".$lat1.",".$lon1."&destination=".$lat2.",".$lon2."&mode=driving&sensor=true";
$result_string = file_get_contents($dist);
$result = json_decode($result_string, true);
There's a Distance to Coast REST API: http://www.kbgeo.com. You provide a target latitude/longitude pair or street address and it returns distance to the nearest coastal point and what that point is. Example JSON response:
{
"distanceInMiles": 702.4893212372758,
"targetPoint": {
"lat": 43.045583,
"lng": -89.3799951
},
"coastlinePoint": {
"lat": 38.662389,
"lng": -77.236111
}
}
There's a live demo page at http://demo.kbgeo.com.
Disclosure: I wrote this API and therefore have a vested interest. Another API that does the same thing is maprisk.com.