Geo Location - Get location based on where you are

Hi, Today i am going to share script how to get location based on where you are located.  Here is the script,

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>GMap location</title>
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"> </script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
$(document).ready(function() {
 navigator.geolocation.getCurrentPosition(function (pos) {
    var geocoder = new google.maps.Geocoder();
    var lat = pos.coords.latitude;
    var lng = pos.coords.longitude;
    var latlng = new google.maps.LatLng(lat, lng);
     //reverse geocode the coordinates, returning location information.
    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
    var result = results[0];
    var state = '';
    var Address ='';
    var city;
    var state;
    var country;
    var zipcode;
    for (var i = 0, len = result.address_components.length; i < len; i++) {
        var ac = result.address_components[i];
        if (i == 0 || i == 1 || i == 2 || i == 3 || i == 4)
        Address += ac.long_name+", ";
        if (i == 4) {
        Address += ac.long_name+".";
        $('#addr').html(Address);
        }
        if (i == 5) {
        city = ac.long_name;
        $('#city').html(city);
        }
        if (i == 6) {
        state = ac.short_name;
        $('#state').html(state);
        }
        if (i == 7) {
        country = ac.short_name;
        $('#country').html(country);
        }
        if (i == 8)  {
        zipcode  = ac.long_name;
        $('#zipcode').html(zipcode);
        }
     }
 });
 });
});
</script>
<style>
table td {  border-bottom: 1px solid #ccc; }
</style>
</head>
<body>
<table cellpadding="10" cellspacing="10" border="0" >
  <tr>
    <td> Address - </td>
    <td><div id="addr"></div></td>
  </tr>
  <tr>
    <td>City - </td>
    <td><div id="city"></div></td>
  </tr>
  <tr>
    <td>State - </td>
    <td><div id="state"></div></td>
  </tr>
  <tr>
    <td>Country - </td>
    <td><div id="country"></div></td>
  </tr>
  <tr>
    <td>Zipcode - </td>
    <td><div id="zipcode"></div></td>
  </tr>
</table>
</body>
</html>




Comments

Popular posts from this blog

Ghostscript Installation and Configuration in Windows 7

Joomla - Get user id