"use strict";
$(document).ready(function() {
// get location button functionality
$("#get-location-btn").click(function(event) {
event.preventDefault();
$("#location-lat-long").val("Finding your coordinates...");
$("#location-lat-long").val("Pinging your device...");
// check if browser supports the geolocation api
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success); // if geolocation supported, call function
} else {
$("#location-lat-long").val('Your browser doesn\'t support the geolocation api.');
}
});
// function to get lat/long
function success(position) {
var latitude = position.coords.latitude; // set latitude variable
var longitude = position.coords.longitude; // set longitude variable
var accuracy = position.coords.accuracy; // set accuracy variable
var latLongResponse = latitude + ', ' + longitude; // build string containing lat/long
var accuracyResponse = accuracy + ' feet'; // add feet to accuracy value in string
$("#location-lat-long").val(latLongResponse); // write lat/long string to input field
$("#location-accuracy").val(accuracyResponse); // write accuracy string to input field
getAddress(latitude, longitude); // geocode the lat/long into an address
}
// function to process address data
function processAddress(address) {
$("#location-address").val(address); // write address to field
}
// function to geocode a lat/long
function getAddress(myLatitude, myLongitude) {
var geocoder = new google.maps.Geocoder(); // create a geocoder object
var location = new google.maps.LatLng(myLatitude, myLongitude); // turn coordinates into an object
geocoder.geocode({
'latLng': location
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) { // if geocode success
processAddress(results[0].formatted_address); // if address found, pass to processing function
}...
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.