-
Notifications
You must be signed in to change notification settings - Fork 32
/
Geocoding.js
136 lines (116 loc) · 4.49 KB
/
Geocoding.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
module.exports = function Geocoding(options) {
var map = options.map || document.getElementById("map") || L.map('map');
function getPlacenameFromCoordinates(lat, lng, precision, onResponse) {
$.ajax({
url:"https://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lng + "&key=AIzaSyDWgc7p4WWFsO3y0MTe50vF4l4NUPcPuwE",
success: function(result) {
if(result.results[0]) {
var country;
var fullAddress = result.results[0].formatted_address.split(",");
for (i in result.results) {
if(result.results[i].types.indexOf("country") != -1) {
//If the type of location is a country assign it to thr input box value
country = result.results[i].formatted_address;
}
}
if (!country) country = fullAddress[fullAddress.length - 1];
if(precision <= 0) onResponse(country);
else if(precision == 1) {
if (fullAddress.length>=2) onResponse(fullAddress[fullAddress.length - 2] + ", " + country);
else onResponse(country);
}
else if(precision >= 2) {
if (fullAddress.length >= 3) onResponse(fullAddress[fullAddress.length - 3] + ", " + fullAddress[fullAddress.length - 2] + ", " + country);
else if (fullAddress.length == 2) onResponse(fullAddress[fullAddress.length - 2] + ", " + country);
else onResponse(country);
}
else onResponse(result.results[0].formatted_address);
}
else onResponse("Location unavailable");
},
error: function(error) {
onResponse("Location unavailable");
}
});
}
function panMapByBrowserGeocode(checkbox) {
var x = document.getElementById("location");
if(checkbox.checked == true) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
function displayPosition(position) {
panMap(parseFloat(position.coords.latitude), parseFloat(position.coords.longitude));
}
}
}
function panMapToGeocodedLocation(selector) {
var input = document.getElementById(selector);
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.addListener('place_changed', function() {
setTimeout(function () {
var str = input.value;
geocodeStringAndPan(str);
}, 10);
});
$("#"+selector).keypress(function(e) {
setTimeout(function () {
if(e.which == 13) {
var str = input.value;
geocodeStringAndPan(str);
}
}, 10);
});
};
function geocodeWithBrowser(success) {
if(success) {
var label = document.createElement("label");
label.classList.add("spinner");
var i = document.createElement("i");
i.classList.add("fa");
i.classList.add("fa-spinner");
i.classList.add("fa-spin");
label.appendChild(i);
var element = document.getElementById(options.geocodeButtonId);
element.appendChild(label);
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function(position) {
options.goTo(position.coords.latitude, position.coords.longitude,options.zoom);
i.classList.remove("fa") ;
i.classList.remove("fa-spinner") ;
i.classList.remove("fa-spin") ;
}, function(error) {
console.log(error);
});
}
}
}
function geocodeStringAndPan(string, onComplete) {
if(typeof map.spin == 'function'){
map.spin(true) ;
}
var url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + string.split(" ").join("+") + "&key=AIzaSyDWgc7p4WWFsO3y0MTe50vF4l4NUPcPuwE" ;
var Blurred = $.ajax({
async: false,
url: url
});
onComplete = onComplete || function onComplete(geometry) {
$("#lat").val(geometry.lat);
$("#lng").val(geometry.lng);
map.setView([geometry.lat, geometry.lng], options.zoom);
if(typeof map.spin == 'function'){
map.spin(false) ;
}
}
onComplete(Blurred.responseJSON.results[0].geometry.location);
}
return {
geocodeStringAndPan: geocodeStringAndPan,
getPlacenameFromCoordinates: getPlacenameFromCoordinates,
panMapByBrowserGeocode: panMapByBrowserGeocode,
panMapToGeocodedLocation: panMapToGeocodedLocation,
geocodeWithBrowser: geocodeWithBrowser
}
}