It’s very easy to get the address data from a website visitor. At first you need the geo location javascript to get the users lat and lng data. After that you can get the address by reverse geocoding using the google maps api.
1. Including JS
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> <script src="js/geo-min.js"></script>
2. Geo location
if(geo_position_js.init()){
geo_position_js.getCurrentPosition(success_callback,error_callback);
}
else{
do something else;
}
function success_callback(p) {
see 3.
}
function error_callback() {
alert('error');
};
3. Reverse geocoding
function success_callback(p) {
var lat = p.coords.latitude;
var lng = p.coords.longitude;
// reverse geocoding - get address by lat lng
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
document.getElementById('address').innerHTML = '<span>Your address: ' + results[0].formatted_address + '</span>';
} else {
document.getElementById('address').innerHTML = '<span>No address found.</span>';
}
} else {
document.getElementById('address').innerHTML = '<span>Geocoder failed due to: ' + status + '</span>';
}
});
}
Here you’ll find a working example: geo-location.html





