SitePoint Sponsor

User Tag List

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

    Google map Longitude Latitude

    Hello Friend,
    I m trying to find Longitude and Latitude using php function from goiogle map.
    My PHP Function is:

    Code 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($query);
     // if data returned
     if($data){
      // convert into readable format
      $data = json_decode($data[0]);
      $long = $data->Placemark[0]->Point->coordinates[0];
      $lat = $data->Placemark[0]->Point->coordinates[1];
      return array('Latitude'=>$lat,'Longitude'=>$long);
     }else{
      return false;
     }
    }
    print_r(getLatLong('EC3N 1DD'));


    But this function print

    Array ( [Latitude] => [Longitude] => )

    This function does not print Longitude or Latitude

    Can anybody tell me where i m wrong?

    Thankx in advance.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What do you get when you use:
    PHP Code:
    var_dump($data); 
    after the json_decode command?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Zealot asrasrasr's Avatar
    Join Date
    Apr 2009
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when i used var_dum($data) before var_dump($data);

    ex:

    Code 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($query);
     $data=var_dump($data);
     // if data returned
     if($data){
      // convert into readable format
      $data = json_decode($data[0]);
      $long = $data->Placemark[0]->Point->coordinates[0];
      $lat = $data->Placemark[0]->Point->coordinates[1];
      return array('Latitude'=>$lat,'Longitude'=>$long);
     }else{
      return false;
     }
    }
    print_r(getLatLong('EC3N 1DD'))


    This return me


    array(41) { [0]=> string(2) "{ " [1]=> string(22) " "name": "EC3N 1DD", " [2]=> string(14) " "Status": { " [3]=> string(17) " "code": 200, " [4]=> string(25) " "request": "geocode" " [5]=> string(5) " }, " [6]=> string(19) " "Placemark": [ { " [7]=> string(16) " "id": "p1", " [8]=> string(45) " "address": "City of London, EC3N 2, UK", " [9]=> string(24) " "AddressDetails": { " [10]=> string(19) " "Accuracy" : 5, " [11]=> string(17) " "Country" : { " [12]=> string(31) " "AdministrativeArea" : { " [13]=> string(54) " "AdministrativeAreaName" : "Greater London", " [14]=> string(37) " "SubAdministrativeArea" : { " [15]=> string(27) " "Locality" : { " [16]=> string(50) " "LocalityName" : "City of London", " [17]=> string(32) " "PostalCode" : { " [18]=> string(48) " "PostalCodeNumber" : "EC3N 2" " [19]=> string(17) " } " [20]=> string(15) " }, " [21]=> string(59) " "SubAdministrativeAreaName" : "City of London" " [22]=> string(11) " } " [23]=> string(9) " }, " [24]=> string(40) " "CountryName" : "United Kingdom", " [25]=> string(31) " "CountryNameCode" : "GB" " [26]=> string(5) " } " [27]=> string(3) "}, " [28]=> string(22) " "ExtendedData": { " [29]=> string(21) " "LatLonBox": { " [30]=> string(29) " "north": 51.5150171, " [31]=> string(29) " "south": 51.5087218, " [32]=> string(28) " "east": -0.0739622, " [33]=> string(27) " "west": -0.0802574 " [34]=> string(8) " } " [35]=> string(7) " }, " [36]=> string(15) " "Point": { " [37]=> string(51) " "coordinates": [ -0.0766630, 51.5113690, 0 ] " [38]=> string(6) " } " [39]=> string(6) " } ] " [40]=> string(2) "} " }


    Now how i get Latitude and Longitude from this

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What is the output if you put the var_dump statement AFTER the json_decode statement, i.e.:
    PHP Code:
    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);
        if(
    null !== ($data json_decode($data)){
            
    var_dump($data);
            
    $long $data->Placemark[0]->Point->coordinates[0];
            
    $lat $data->Placemark[0]->Point->coordinates[1];
            return array(
    'Latitude'=>$lat,'Longitude'=>$long);
        }else{
            return 
    false;
        }

    The var_dump statement isn't for allocating to a variable, its for debugging - it tells you the entire contents of an object/array (easier to view if you viewsource)

    Notice I replaced the file function with file_get_contents. This puts it in an entire string, rather than an array.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •