how to get longitude and latitude using javascript
  • How do you convert this php script to javascript?
    I need javascript function that will convert an address to latlong (lat,long)

    * Given an address, return the longitude and latitude using The Google Geocoding API V3

    function Get_LatLng_From_Google_Maps($address) {

    $url = "$address&sensor=false";

    // Make the HTTP request
    $data = @file_get_contents($url);
    // Parse the json response
    $jsondata = json_decode($data,true);

    // If the json data is invalid, return empty array
    if (!check_status($jsondata)) return array();

    $LatLng = array(
    'lat' => $jsondata["results"][0]["geometry"]["location"]["lat"],
    'lng' => $jsondata["results"][0]["geometry"]["location"]["lng"],

    return $LatLng;

    * Check if the json data from Google Geo is valid

    function check_status($jsondata) {
    if ($jsondata["status"] == "OK") return true;
    return false;

    * Print an array

    function d($a) {
    echo "<pre>";
    echo "</pre>";

  • 4 Comments sorted by
  • hi try this 

    <!DOCTYPE html>
       <script type="text/javascript">
         function initGeolocation()
            if( navigator.geolocation )
               // Call getCurrentPosition with success and 

    failure callbacks

    success, fail );
               alert("Sorry, your browser does not support 

    geolocation services.");

         function success(position)

             document.getElementById('long').value = 

             document.getElementById('lat').value = 


         function fail()
            // Could not obtain location


     <body onLoad="initGeolocation();">
       <FORM NAME="rd" METHOD="POST" ACTION="index.html">
         <INPUT TYPE="text" NAME="long" ID="long" VALUE="">
         <INPUT TYPE="text" NAME="lat" ID="lat" VALUE="">

  • This is good but my goal is to convert an address to Longitude and Latitude.

  • please give any address sample example what kind of address ?
  • 713 Centennial Pkwy Raleigh NC 27606.
     Address  <input type='text' id='address' name='address'/>
     Result  <input type='text' id='result' name='result'/>
                <input type='button' value='Convert To Lat Long'/>
    When i click on Convert To Lat Long button it will convert the address data into lat long like (78.155,-80.455)
    I want to convert this address for example into latitude and longitude with google map api(javascript)

    Thank You

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook


Popular Posts of the Week

    Optimum Creative