SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot asrasrasr's Avatar
    Join Date
    Apr 2009
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How get accurate Longitude and Latitude

    Hello friends,

    I am trying following function to get Longitude and Latitude.

    Code PHP:
    <?php 
    function getLatLong($code){
     $mapsApiKey = 'ABQIAAAA7wBDi6CpypoVz_BrL4M4ORTNjq-eX0lgecZr0EpBIKf0u-oJURQ5hwTKIS8zcZIf6aRMVFJX25FigA';
     $query = "http://maps.google.co.uk/maps/geo?q=".urlencode($code)."&output=json&key=".$mapsApiKey;
     $data = file_get_contents($query);
     //$data=var_dump($data);
     // if data returned
     if($data){
      // convert into readable format
     
      $data = json_decode($data);
      $long = $data->Placemark[0]->Point->coordinates[0];
      $lat = $data->Placemark[0]->Point->coordinates[1];
      return array('Latitude'=>$lat,'Longitude'=>$long);
     }else{
      return false;
     }
    }
    $attays =getLatLong('KT2 5EE'); ?>

    This return me
    Longitude = -0.3019994
    Latitude = 51.422696

    Where right value of Longitude and Latitude
    Latitude: 51.414205
    Longitude: -0.300479

    how i get accurate Latitude and Longitude?

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    The data returned is only as good as a) the input sent and b) the data store. If the data store (in this case Google's Maps API) doesn't have the exact data that you want, then you just won't get it. For things like post codes, they span an area; in this case at least the provided coordinates appear to fall within the boundary of that post code even if they're not precisely what you're looking for.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •