//Convert Destination Address to lat lng values
var specificlocation = {
lat: -13.26589,
lng: 98.365297
}
function onDeviceReady() {
//onDeviceReaddy
navigator.geolocation.getCurrentPosition(displayAndWatch, locError);
}
// user Current Position
function displayAndWatch(position) {
setCurrentPosition(position);
watchCurrentPosition();
}
function setCurrentPosition(pos) {
var image = 'img/ic_CurrentLocationmap.png';
currentPositionMarker = new google.maps.Marker({
icon: image,
map: map,
position: new google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
),
title: "Current Location"
});
map.panTo(new google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
));
}
//Watch User/phone current location
function watchCurrentPosition() {
var positionTimer = navigator.geolocation.watchPosition(
function (position) {
setMarkerPosition(
currentPositionMarker,
position
);
});
}
function setMarkerPosition(marker, position) {
marker.setPosition(
new google.maps.LatLng(
position.coords.latitude,
position.coords.longitude)
);
var center = {
lat: position.coords.latitude,
lng: position.coords.longitude
}
map.setCenter(center);
//Check distance between specificlocation and user/phone current location
var distance = DistanceBetweenTwoPoints(center.lat, center.lng, specificlocation.lat,
specificlocation.lng, "K")
//with in 100 Meters
if (distance < 0.1) {
navigator.notification.confirm(
'You are Reached specificlocation Address', // message
onConfirmReached, //Callback
'AppName', // title
['No', 'Yes'] // buttonLabels
);
}
function locError(error) {
// the current position could not be located
}
function onConfirmReached(buttonIndex) {
if (buttonIndex == 2) {
//Alert result
}
}
//Find Distance between Two coordinations
function DistanceBetweenTwoPoints(lat1, lon1, lat2, lon2, unit) {
try {
unit = "K"
var radlat1 = Math.PI * lat1 / 180
var radlat2 = Math.PI * lat2 / 180
var radlon1 = Math.PI * lon1 / 180
var radlon2 = Math.PI * lon2 / 180
var theta = lon1 - lon2
var radtheta = Math.PI * theta / 180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(
radtheta);
dist = Math.acos(dist)
dist = dist * 180 / Math.PI
dist = dist * 60 * 1.1515
//Calculate Kilometer
if (unit == "K") {
dist = dist * 1.609344
}
//Calculate Miles
if (unit == "N") {
dist = dist * 0.8684
}
return dist;
} catch (err) {
console.log(err);
}
}