1// Full tutorial: https://www.youtube.com/watch?v=JdJ2VBbYYTQ
2
3function reverseGeocode(latitude, longitude) {
4 let apikey = "your API key goes here";
5 fetch('https://api.opencagedata.com/geocode/v1/json'
6 + '?'
7 + 'key=' + apikey
8 + '&q=' + encodeURIComponent(latitude + ',' + longitude)
9 + '&pretty=1'
10 + '&no_annotations=1')
11 .then((response) => response.json())
12 .then((data) => alert(data.results[0].formatted));
13}
14
15function success(data) {
16 let latitude = data.coords.latitude;
17 let longitude = data.coords.longitude;
18 reverseGeocode(latitude, longitude);
19}
20
21if (navigator.geolocation) {
22 window.navigator.geolocation
23 .getCurrentPosition(success, console.error);
24}
1function getReverseGeocodingData(lat, lng) {
2 var latlng = new google.maps.LatLng(lat, lng);
3 // This is making the Geocode request
4 var geocoder = new google.maps.Geocoder();
5 geocoder.geocode({ 'latLng': latlng }, (results, status) =>{
6 if (status !== google.maps.GeocoderStatus.OK) {
7 alert(status);
8 }
9 // This is checking to see if the Geoeode Status is OK before proceeding
10 if (status == google.maps.GeocoderStatus.OK) {
11 console.log(results);
12 var address = (results[0].formatted_address);
13 }
14 });
15}