// NEXT STEP: Create IF error statements to return "Out of Area"
<meta charset="utf-8">
<body onload="geoFindMe()">
<body onload="geocodeLatLng()">
<!-- Demo is where sheets db is called. -->
<center>
<div id="demo"></div>
</center>
<div id="this"></div>
<div id="floating-panel">
<input id="latlng" type="text" value="Coordinates">
<input id="submit" type="button" value="Reverse Geocode">
<div id="test"></div>
</div>
<div id="title"></div>
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>
CSS
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
.phone {
font-family: 'Roboto', 'sans-serif';
line-height: 20px;
font-weight: bold;
font-size: large;
text-align:center;
margin: 10px;
border: 0px solid black;
outline: 4px solid red;
outline-offset: 5px;
}
#demo {
font-family: 'Roboto', 'sans-serif';
line-height: 30px;
padding-left: 10px;
}
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#floating-panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto', 'sans-serif';
line-height: 30px;
padding-left: 10px;
}
#floating-panel {
position: absolute;
top: 5px;
left: 50%;
margin-left: -180px;
width: 350px;
z-index: 5;
background-color: #fff;
padding: 5px;
opacity: 0.5;
border: 1px solid #999;
}
#latlng {
width: 225px;
}
JavaScript
// This function gets your current locations latitude and longitude
function geoFindMe() {
var output = document.getElementById("test");
document.getElementById("floating-panel").style.display = 'none';
if (!navigator.geolocation) {
output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
return;
}
function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var lattlngg = latitude + ', ' + longitude;
window.lattlngg1 = lattlngg;
// Set latlng input field to current coordinates
document.getElementById("latlng").value = latitude + ', ' + longitude;
output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';
}
function error() {
output.innerHTML = "Unable to retrieve your location";
}
output.innerHTML = "<p>Locating…</p>";
navigator.geolocation.getCurrentPosition(success, error);
}
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: {
lat: 12.137497,
lng: -86.281859
}
});
var geocoder = new google.maps.Geocoder;
var infowindow = new google.maps.InfoWindow;
// document.getElementById("latlng").value = latitude;
document.getElementById('submit').addEventListener('click', function() {
geocodeLatLng(geocoder, map, infowindow);
});
setTimeout(function() {
geocodeLatLng(geocoder, map, infowindow);
}, 5000);
}
// This converts coordinates from input field into address
function geocodeLatLng(geocoder, map, infowindow, countryl) {
var input = document.getElementById('latlng').value;
// var input = lattlngg1;
var latlngStr = input.split(',', 2);
var latlng = {
lat: parseFloat(latlngStr[0]),
lng: parseFloat(latlngStr[1])
};
geocoder.geocode({
'location': latlng
}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
map.setZoom(14);
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.