Write a C program to receive values of latitude(L1, L2) and longitude(G1, G2), in degrees, of two places on the earth and output the distance (D) between them in nautical miles.
where lat1, lat2 are latitudes. lon1, lon2 are longitude.
Formula To Convert Degree To Radian
radian = degree * (PI/180.0); where PI is 3.141592
Logic To Find Distance In Nautical Miles
User input the values of latitude(lat1, lat2) and longitude(lon1, lon2 ), in degrees. We convert it to radians. Next we use the formula below: D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 – lon1) ); and out put the result on to the console.
Source Code: Calculate Distance in Nautical Miles: C Program
#include < stdio.h >
#include < math.h >
int main()
{
float lat1, lat2, lon1, lon2, D;
const float PI = 3.141592;
printf("Enter latitude(L1, L2)\n");
scanf("%f%f", &lat1, &lat2);
printf("Enter longitude(G1, G2)\n");
scanf("%f%f", &lon1, &lon2);
/* Convert Degrees To Radian */ lat1 = lat1 * ( PI / 180.0 );
lat2 = lat2 * ( PI / 180.0 );
lon1 = lon1 * ( PI / 180.0 );
lon2 = lon2 * ( PI / 180.0 );
D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1) );
printf("Distance in nautical miles is %f\n", D);
return 0;
}
Output: Enter latitude(L1, L2) 52.3 43.2 Enter longitude(G1, G2) 12.3 15.6 Distance in nautical miles is 647.682434
Here we’ve added an id to paragraph tag. And included Google Map API’s javascript file within the head tag. We must include it before myScript.js, as we’ll be using Google Map API’s inside myScript.js
Javascript File: Google Map API myScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function success(position)
{
var googleLatLng = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var mapOtn={
zoom:10,
center : googleLatLng,
mapTypeId: google.maps.MapTypeId.ROAD
}
var Pmap= document.getElementById("map");
var map= new google.maps.Map(Pmap, mapOtn);
}
function success(position)
{
var googleLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var mapOtn={
zoom:10,
center : googleLatLng,
mapTypeId: google.maps.MapTypeId.ROAD
}
var Pmap= document.getElementById("map");
var map= new google.maps.Map(Pmap, mapOtn);
}
Pass position.coords.latitude and position.coords.longitude values to LatLng constructor of Google Maps API, which converts it into googleLatLng object.
Set the options of Google Maps: zoom takes value from 0 to 21. We center the map to our users/visitors location. map type: ROADMAP, SATELLITE, HYBRID, TERRAIN.
$(document).ready(function(){
if( navigator.geolocation )
navigator.geolocation.getCurrentPosition(success, fail);
else
$("p").html("HTML5 Not Supported");
});
function success(position)
{
var googleLatLng = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var mapOtn= {
zoom:10,
center :googleLatLng,
mapTypeId:google.maps.MapTypeId.ROAD
}
var Pmap= document.getElementById("map");
var map= new google.maps.Map(Pmap, mapOtn);
}
function fail(error)
{
var errorType = {
0: "Unknown Error",
1: "Permission denied by the user",
2: "Position of the user not available",
3: "Request timed out"
};
var errMsg = errorType[error.code];
if(error.code == 0 || error.code == 2){
errMsg = errMsg+" - "+error.message;
}
$("p").html(errMsg);
}
$(document).ready(function(){
if( navigator.geolocation ) navigator.geolocation.getCurrentPosition(success, fail);
else $("p").html("HTML5 Not Supported");
});
function success(position)
{
var googleLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var mapOtn= {
zoom:10,
center :googleLatLng,
mapTypeId:google.maps.MapTypeId.ROAD
}
var Pmap= document.getElementById("map");
var map= new google.maps.Map(Pmap, mapOtn);
}
function fail(error)
{
var errorType = {
0: "Unknown Error",
1: "Permission denied by the user",
2: "Position of the user not available",
3: "Request timed out"
};
var errMsg = errorType[error.code];
if(error.code == 0 || error.code == 2){
errMsg = errMsg+" - "+error.message;
}
$("p").html(errMsg);
}
Note: If you’re accessing this application via your desktop computer, then it may be showing you the location of your ISP’s(Internet Service Provider’s) local office location. To get the accurate location, access our demo page via your smart phone.
Smartphone 1. Install Google Chrome browser. 2. Switch on your GPS and permit Google to access your location information, for testing purpose. 3. Visit our demo link(present above). 4. Move around and see your location information being updated in real-time. 5. We’ll teach you to make your application show real time data in coming videos, stay subscribed :-)
In this video tutorial we’ll show you how to write your first(simple) HTML5 geolocation API application. Basics of Geolocation API is also explained in the notes below, so don’t skip, read and practice it.
We’re also including a jQuery library file. Make sure to include it before our custom javascript file(myScript.js), as our javascript file depends on jQuery library file.
CSS File myStyle.css
1
2
3
4
p{
width: 300px;
height: 300px;
}
p{
width: 300px;
height: 300px;
}
This is a simple Cascading Stylesheet file, which is allocating 300px width and height to the paragraph tag present in index.html file. It’ll be helpful when we integrate Google Maps into our application( in the future videos ).
This is a minimalistic approach to using geolocation API. We’ll improve it in future videos.
Once the document is loaded and ready, we check if the browser supports Geolocation API. We do that by checking if navigator.geolocation object exists. If it doesn’t exist, we display appropriate message to the user(“HTML5 Not Supported”).
If it does support navigator.geolocation, we’ll make use of it’s methods and get the user location. navigator.geolocation has 3 methods: getCurrentPosition watchPosition clearWatch
In this video tutorial we’ll be looking at the first method, i.e., getCurrentPosition()
Note: 1. Geolocation API returns latitude and longitude values in decimal number system. 2. It determines users location by using various factors like: IP Address, GPS, Cell phone triangulation, WiFi etc. 3. We can’t know or specify which method it has to use to determine the users location. 4. If you’re getting 404 or googleapi not found, [object][object] etc error messages, then reload your webpage after you restart your system and/or your WiFi modem. Make sure your WiFi modem is ON: because it’s one of the way Geolocation API will fetch your location from.
Missing things: Error handling. Options, to configure maps output. etc Showing the location of the user on a MAP. Showing real time movement of the user on a MAP. All these will be covered in future videos. Stay Subscribed. Share this video with your friends.