-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_geocoding_google.js
50 lines (43 loc) · 1.64 KB
/
reverse_geocoding_google.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
/*jshint esversion: 11 */
const google = require("@googlemaps/google-maps-services-js");
let latitude = 28.54; //sub in your latitude
let longitude = -81.39;
getReverseGeocodingData(latitude, longitude);
function getReverseGeocodingData(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
// This is making the Geocode request
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'latLng': latlng }, (results, status) => {
if (status !== google.maps.GeocoderStatus.OK) {
alert(status);
}
// This is checking to see if the Geoeode Status is OK before proceeding
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
var address = (results[0].formatted_address);
}
});
}
/*
geocoder.geocode({ 'location': { lat: latitude, lng: longitude } }, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
results.forEach(function (element) {
element.address_components.forEach(function (element2) {
element2.types.forEach(function (element3) {
switch (element3) {
case 'postal_code':
postal_code = element2.long_name;
break;
case 'administrative_area_level_1':
state = element2.long_name;
break;
case 'locality':
city = element2.long_name;
break;
}
})
});
});
}
});
*/