Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Get current location on load of a page using google map API

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 746
    Comment on it

    A code snippet for getting current location on browser using google map API

    Here we are calling getLocation() on load of the page

       getLocation();
      function getLocation()
      {
           if (navigator.geolocation)
            {
                 navigator.geolocation.getCurrentPosition(getLocationDetails);
            }
       }
    
      var country,state,city,pinCode;
      function createCORSRequest(method, url) 
           {
              var xhr = new XMLHttpRequest();
    
              if ("withCredentials" in xhr) 
                 {
                     // XHR for Chrome/Firefox/Opera/Safari.
                     xhr.open(method, url, true);
    
                 } else if (typeof XDomainRequest != "undefined") {
                    // XDomainRequest for IE.
                     xhr = new XDomainRequest();
                     xhr.open(method, url);
    
               } else {
                            xhr = null;
                         }
      return xhr;
      }
    
    

    Function getLocationDetails will fetch the latitude & longitude of the current position based on which we will be able to find the city, state, pincode & country.

    function getLocationDetails(getLocationDetails) 
      { 
          hide();
          latitude1=position.coords.latitude;
          longitude1=position.coords.longitude;
    
           var url="http://maps.googleapis.com/maps/api/geocode/json?latlng="+
           latitude1+","+longitude1+"&sensor=true";
            var xhr = createCORSRequest('POST', url);
               if (!xhr) {
                 alert('CORS not supported');
               }
    
               xhr.onload = function() 
                {
                  var data =JSON.parse(xhr.responseText);
    
                    if(data.results.length>0)
                     {
    
                        var locationDetails=data.results[0].formatted_address;
                        var  value=locationDetails.split(",");
    
                         count=value.length;
    
                          country=value[count-1];
                          state=value[count-2];
                          city=value[count-3];
                          pin=state.split(" ");
                           pinCode=pin[pin.length-1];
                           state=state.replace(pinCode,' ');         
                           document.getElementById("val").innerHTML=country+
                           " | "+state+" | "+city+"  | "+pinCode;
                       }
                 else
                   {
                    document.getElementById("messageBox").style.visibility="visible";
                    document.getElementById("message").innerHTML=
                    "No location available for provided details.";
                   }
    
    
               };
    
                   xhr.onerror = function() 
                   {
                   alert('Woops, there was an error making the request.');
                    };
                  xhr.send();
    
        }
    
    function hide()
    {
    document.getElementById("messageBox").style.visibility="hidden";
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: