1 /**
  2  * @author <a href="mailto:micmath@gmail.com">Michael Mathews</a>
  3  */
  4 
  5 // This is an event that fires when PhoneGap is fully loaded.
  6 // then we can begin to run GPS to get the position
  7 document.addEventListener("deviceready", onDeviceReady, false);
  8 window.addEventListener("orientationchange", orientationChange, true);
  9 
 10 /** google map object*/
 11 var map;
 12 /** information window for google map*/
 13 var infoWindow;
 14 
 15 /** location object */
 16 var JasonObj = {
 17 	"Locations" : [ {
 18 		"title" : "Store 1",
 19 		"longitude" : 29.9517070,
 20 		"latitude" : 31.2260669,
 21 	}, {
 22 		"title" : "Store 2",
 23 		"longitude" : 29.9598065,
 24 		"latitude" : 31.2243669,
 25 	}, {
 26 		"title" : "Store 3",
 27 		"longitude" : 29.9586080,
 28 		"latitude" : 31.2290669,
 29 	}, {
 30 		"title" : "Store 4",
 31 		"longitude" : 29.9508075,
 32 		"latitude" : 31.2296670,
 33 	} ]
 34 }
 35 
 36 function orientationChange(e) {
 37 	var orientation = window.orientation;
 38 	switch (orientation) {
 39 	}
 40 }
 41 
 42 /**
 43  * onDeviceRaedy method is called when the event "device ready" is fired" then
 44  * we can call navigator to get the current position if the device succeed to
 45  * get the location then call onSuceess() method else call onError() method
 46  * @function
 47   * @return {void}
 48  */
 49 function onDeviceReady() {
 50 	// nessecary to enable this option
 51 	// when working with android 2.x
 52 	var options = {
 53 		enableHighAccuracy : true
 54 	};
 55 	// activate the GPS
 56 	navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
 57 }
 58 
 59 /**
 60  * this method take parameter position that contain coords that a lot of
 61  * information about the current location such as latitude, longitude ,altitude
 62  * ,accuracy ,speed then create Gmap object and give the longitude and latitude
 63  * then show it in div with id = map1 then create marker with the current
 64  * position coordinates
 65  * @function
 66  * @param position
 67   * @return {void}
 68  */
 69 function onSuccess(position) {
 70 	// create Gmap object with longtiude and latitude
 71 	var latlng = new google.maps.LatLng(position.coords.latitude,
 72 			position.coords.longitude);
 73 	// setting its options
 74 	var myOptions = {
 75 		zoom : 15, // level of zoom
 76 		center : latlng,
 77 		mapTypeId : google.maps.MapTypeId.ROADMAP
 78 	// road map
 79 	};
 80 	// set the map to div with id map1
 81 	map = new google.maps.Map(document.getElementById("map1"), myOptions);
 82 	// making map marker to some the location point
 83 	var marker = new google.maps.Marker({
 84 		position : latlng,
 85 		title : "My Google Map",
 86 		clickable : true
 87 	});
 88 	infoWindow = new google.maps.InfoWindow();
 89 	google.maps.event.addListenerOnce(map, 'tilesloaded', addMarkers);
 90 
 91 	marker.setMap(map);
 92 	google.maps.event.addListener(marker, 'click', function() {
 93 		infoWindow.setContent("you are here");
 94 		infoWindow.open(map, marker);
 95 	});
 96 	// alert(position.coords.latitude + " " + position.coords.longitude);
 97 	// 31.2290669 29.9548075
 98 	getShortDistance(position.coords.latitude, position.coords.longitude);
 99 }
100 
101 /**
102  * used to add markers to map at specific points determineded by Jason Object
103  * @function
104   * @return {void}
105  */
106 function addMarkers() {
107 
108 	/**
109 	 * create the marker (i) at the specific (position)
110 	 * @function 
111 	 * @param {Object}
112 	 *            map
113 	 * @param {Object}
114 	 *            position
115 	 * @param {Number}
116 	 *            i
117 	  * @return {void}
118 	 */
119 	function createMarker(map, position, i) {
120 		var marker = new google.maps.Marker(
121 				{
122 					position : position,
123 					map : map,
124 					icon : "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld="
125 							+ (i + 1) + "|6495ED|000000"
126 				});
127 
128 		google.maps.event.addListener(marker, 'click', function() {
129 			var myHtml = JasonObj.Locations[i].title + '<br>'
130 					+ '<a href="http://www.google.com">Google</a>';
131 			infoWindow.setContent(myHtml);
132 			infoWindow.open(map, marker);
133 		});
134 	}
135 
136 	for ( var i = 0; i < JasonObj.Locations.length; i++) {
137 		var latLng = new google.maps.LatLng(JasonObj.Locations[i].latitude,
138 				JasonObj.Locations[i].longitude);
139 		createMarker(map, latLng, i);
140 	}
141 }
142 
143 /**
144  * called in case of error
145  * @function
146  * @param error
147  * @return {void}
148  */
149 function onError(error) {
150 	navigator.notification.alert('code: ' + error.code + '\n' + 'message: '
151 			+ error.message + '\n');
152 }
153 
154 /**
155  * get the shortest path from the current location to some points detereminded
156  * by the points in Jason object
157  * @function
158  * 
159  * @param {Object}
160  *            lat
161  * @param {Object}
162  *            lon
163  * @return {void}
164  */
165 function getShortDistance(lat, lon) {
166 	var min = 999999999;
167 	var index = 0;
168 	for ( var i = 0; i < JasonObj.Locations.length; i++) {
169 		var length = calculateDistance(lat, lon,
170 				JasonObj.Locations[i].latitude, JasonObj.Locations[i].longitude);
171 		if (length < min) {
172 			min = length;
173 			index = i;
174 		}
175 	}
176 	document.getElementById("nearestLoc").innerHTML = "Store " + (index + 1)
177 			+ " is the nearest to you<br>( " + (min * 1000).toFixed(2) + " M)";
178 }
179 
180 /**
181  * calculate distance method used to get the distance between two points using
182  * longitude and latitude the first point (lat1,lon1) the second point
183  * (lat2,lon2)
184  * @function
185  * 
186  * @param {Object}
187  *            lat1 latitude of the first point
188  * @param {Object}
189  *            lon1 longitude of the first point
190  * @param {Object}
191  *            lat2 latitude of the second point
192  * @param {Object}
193  *            lon2 longitude of the second point
194  * @return {Object} distance
195  */
196 function calculateDistance(lat1, lon1, lat2, lon2) {
197 	var R = 6371;
198 	// Radius of the earth in km
199 	var dLat = ((lat2 - lat1) * Math.PI) / 180;
200 	// Javascript functions in radians
201 	var dLon = ((lon2 - lon1) * Math.PI) / 180;
202 	var a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
203 			+ Math.cos((lat1 * Math.PI) / 180)
204 			* Math.cos((lat1 * Math.PI) / 180) * Math.sin(dLon / 2)
205 			* Math.sin(dLon / 2);
206 	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
207 	var d = R * c;
208 	// Distance in km
209 	return d;
210 
211 }
212