-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.js
121 lines (94 loc) · 3.71 KB
/
distance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
(function(distanceapp){
var latlong;
var apiKey = "AIzaSyAd9NQNHCBLHehNLvpdvvHMYB1rvgK0v_4";
var startLatLong = {lat: 33.988233, lng: -118.459086};
function Distance(){}
// format data from JSON, get distance and round to nearest integer
Distance.prototype.calcLocationCoords = function(value, callback){
endLatLong = value.results[0].geometry.location;
distRaw = Distance.prototype.formulateDistance(startLatLong.lat, startLatLong.lng, endLatLong.lat, endLatLong.lng);
distInt = Math.round(distRaw);
callback(distInt, endLatLong);
}
//shamelessly stolen from http://stackoverflow.com/questions/27928/
Distance.prototype.formulateDistance = function(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the earth in km
var dLat = Distance.prototype.deg2rad(lat2-lat1); // deg2rad below
var dLon = Distance.prototype.deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Distance.prototype.deg2rad(lat1)) * Math.cos(Distance.prototype.deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
Distance.prototype.deg2rad = function(deg) {
return deg * (Math.PI/180)
}
// main API call and handling of array sorting
Distance.prototype.measureIt = function(locations){
var stop = locations.length;
mainArray = [];
var coords;
$.each(locations, function( index, value ){
encodedLoc = encodeURIComponent(value);
var distanceAPI = "https://maps.googleapis.com/maps/api/geocode/json?address="+encodedLoc+"&key="+apiKey;
$.when(
$.getJSON(distanceAPI)
).then(function(a, b) {
if(b == 'success'){
Distance.prototype.calcLocationCoords(a, function(locationData, coordsObj){
//create array of objects (for sorting purposes)
locObj = {};
locObj['dist'] = locationData;
locObj['place'] = value;
mainArray.push(locObj);
//create string of lat/long coords
coords += coordsObj['lat'] + "," + coordsObj['lng'] + "|";
});
if(index == (stop - 1)){
sortedMainArray = mainArray.sort(function(a, b) {
return a.dist - b.dist;
})
Distance.prototype.printSorted(sortedMainArray);
Distance.prototype.printMap(coords);
}
}
});
});
}
// iterate through the sorted array and append to table
Distance.prototype.printSorted = function(values){
$.each(values, function(index, value){
var html;
html += '<tr>';
html += '<td>'+value['dist']+'</td>';
html += '<td>'+value['place']+'</td>';
html += '</tr>';
$('#content').append(html);
});
}
Distance.prototype.printMap = function(values){
$('#map').append('<img src=https://maps.googleapis.com/maps/api/staticmap?center=40.8513741,-73.8746806&size=640x640&format=png&maptype=roadmap&sensor=false&style=element:labels|visibility:off&markers=icon:http://goo.gl/k8cNSj|' + values + ' />');
}
window.Distance = Distance;
})(this);
$(document).ready(function(){
function init() {
// array of text location points to be processed by Google Maps API for latitude and longitude
var locations = new Array(
"Times Square, Manhattan, NY 10036",
"13000 S Dakota 244, Keystone, SD 57751",
"1600 Pennsylvania Ave NW, Washington, DC 20500",
"Golden Gate Bridge, San Francisco, CA 94129",
"Stonehenge, A344, Amesbury, Wiltshire SP4 7DE, United Kingdom",
"Great Wall of China",
"Hollywood Sign, Los Angeles, CA"
);
var dist = new Distance();
dist.measureIt(locations);
}
init();
});