Forum Moderators: open

Message Too Old, No Replies

Current position detection and stored value

         

toplisek

1:31 pm on Feb 13, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I like to store user's current position as address inside variable and further manipulate. Map is not needed in this case.

How to do it in the simple way and store as address not numbers. Currently I have current position detection:


<!DOCTYPE html />
<html>
<head>
<title>User's current detection'</title>
<link href='http://fonts.googleapis.com/css?family=Terminal+Dosis:800,600' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/styles1.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();

function initialize() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(foundYou, notFound);
} else {
alert('Geolocation not supported or not enabled.');
}
}
function notFound(msg) {
alert('Could not find your location :(')
}
function foundYou(position) {
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
geocoder.geocode({'latLng': latlng}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var address = results[0].address_components[1].long_name+' '+results[0].address_components[0].long_name+', '+results[0].address_components[3].long_name
$('.autoLink span').html(address).parent().show().click(function()
{}
);
}
} else {
alert("Geocoder failed due to: " + status);
}
}
);
}
</script>
</head>
<body onLoad="initialize()">

<h1>Current position detection</h1>


<div style="width: 500px; float: left; text-align: left; margin-right: 40px">

<a href="#" class="autoLink">Use current location: <span>not found</span></a>

</div><!-- .wrapper -->

</body>
</html>

cosmic wheels

11:23 am on Apr 16, 2017 (gmt 0)

5+ Year Member



The variable "address" is already storing address data that you can manipulate. You can use
console.log(results)
to see which components are returned and build the exact address using the different elements that are stored in results.