Click here to Skip to main content
15,890,399 members
Articles / Ajax
Tip/Trick

Get Location Detail from Latitude and Longitude

Rate me:
Please Sign up or sign in to vote.
4.35/5 (10 votes)
21 Nov 2013CPOL 61.5K   1.4K   8   9
By entering latitude and longitude get location details Country, state, city, pin code, etc.

Introduction

It is very important for Hybrid mobile apps to get the location of customers who are using apps. With Cordova/phone gap, it's very essay to get latitude and longitude details from which you will get details about the customer country, state, city, etc. 

Using the Code 

  1. Copy code to one Notepad file.
  2. The code contains HTML and script parts.
  3. Enter the latitude and longitude values in the provided text boxes and get the location details :)
JavaScript
//Read the entered latitude and longitude
// values and get Country,State,City, in code detail
//Implemented with CORS   

 <script>
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 {
    // CORS not supported.
    xhr = null;
  }
  return xhr;
}

function getLocationDetails()
{
hide();
latitude1=document.getElementById("latitude").value;
longitude1=document.getElementById("longitude").value;

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";
}
</script>
HTML
//HTML Code 
 <body onload="hide()">

<div >

</div><br><br>
<div>
Latitude:<input type="text" id="latitude"><br><br>
Longitude:<input type="text" id="longitude"><br>
<br>
<input type="button" value="Get Location" 
onclick="getLocationDetails()"></button>
<br><br>
<label id="val"></label>
</div>
<div id="messageBox" 
  style="position:fixed;top:30%;left:30%;
  width:200px;height:100px;border-radius:15px;text-align:center;
background-color:skyblue;
">
<div style="position:absolute;margin-top:0px;left:2px;
height:20px;width:98%;border-radius:5px;overflow:hidden;
background-color:pink; 
"><label id="title" style="
color:blue;">Skin Advisor</label></div>
<div style="position:absolute;margin:2px;top:25px;height:80px;width:100%;">
<label id="message" style="
color:blue;font-family:Helvetica;"></label></div>
</div>
</body> 

Points of Interest 

You can use the API in Hybrid mobile apps which use HTML, JavaScript, jQuery. In place of CORS, if you use $.ajax service call, that will also show proper results.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Neeyamo Enterprise Pvt Ltd, Pune
India India
Working on .Net technologies from last 4 years, WCF, C#, Silverlight etc. Recently I have developed a Mobile apps with Phone Gap technique.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Chirag31106-Oct-14 0:48
Chirag31106-Oct-14 0:48 
GeneralRe: My vote of 1 Pin
Avinash64746-Oct-14 1:14
Avinash64746-Oct-14 1:14 
QuestionError Pin
hskn200618-Apr-14 11:11
hskn200618-Apr-14 11:11 
AnswerRe: Error Pin
hskn200618-Apr-14 11:12
hskn200618-Apr-14 11:12 
GeneralMy vote of 1 Pin
Ivor O'Connor16-Nov-13 13:15
Ivor O'Connor16-Nov-13 13:15 
QuestionIncomplete example. Pin
Ivor O'Connor16-Nov-13 13:13
Ivor O'Connor16-Nov-13 13:13 
GeneralMy vote of 3 Pin
Bernhard Hiller15-Nov-13 4:29
Bernhard Hiller15-Nov-13 4:29 
GeneralRe: My vote of 3 Pin
Avinash647415-Nov-13 16:29
Avinash647415-Nov-13 16:29 
GeneralRe: My vote of 3 Pin
Ivor O'Connor20-Nov-13 2:38
Ivor O'Connor20-Nov-13 2:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.