Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    104
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Extracting lat and long from google map json

    Hello Guys,

    I need some help here.. I am trying to extract the lat and long from a json query from google.

    Here is what I receive back from json
    PHP Code:
    {
      
    "name""795PollardBlvdSW,Atlanta,GA",
      
    "Status": {
        
    "code"200,
        
    "request""geocode"
      
    },
      
    "Placemark": [ {
        
    "id""p1",
        
    "address""Atlanta, GA, USA",
        
    "AddressDetails": {
       
    "Accuracy" 4,
       
    "Country" : {
          
    "AdministrativeArea" : {
             
    "AdministrativeAreaName" "GA",
             
    "SubAdministrativeArea" : {
                
    "Locality" : {
                   
    "LocalityName" "Atlanta"
                
    },
                
    "SubAdministrativeAreaName" "Fulton"
             
    }
          },
          
    "CountryName" "USA",
          
    "CountryNameCode" "US"
       
    }
    },
        
    "ExtendedData": {
          
    "LatLonBox": {
            
    "north"33.8231844,
            
    "south"33.6747422,
            
    "east": -84.2599230,
            
    "west": -84.5160418
          
    }
        },
        
    "Point": {
          
    "coordinates": [ -84.387982433.7489954]
        }
      } ]

    Here is the code I am using

    PHP Code:
    $geocode=file_get_contents('http://maps.google.com/maps/geo?output=json&q=795PollardBlvdSW,Atlanta,GA');

    $outputjson_decode($geocode);

    $lat $output->results[0]->geometry->location->lat;
    $long $output->results[0]->geometry->location->lng;

    echo 
    $lat;
    echo 
    $long
    Just not sure how to pull the lat and long out of a json into vars..

    Please advise..
    Dan

  • #2
    Regular Coder
    Join Date
    Jun 2010
    Posts
    302
    Thanks
    68
    Thanked 8 Times in 8 Posts
    I'm not familiar with the Google Maps API,

    but it looks to me as if in the general case, the lat and long would be extracted by:

    PHP Code:
    $geocode=file_get_contents('http://maps.google.com/maps/geo?output=json&q=795PollardBlvdSW,Atlanta,GA');

    $outputjson_decode($geocode);

    //Extract the placemark data into two arrays $long and $lat.
    $long $lat = array();

    foreach (
    $output->Placemark as $i => $placemark)
      list(
    $long[$i], $lat[$i]) = $placemark->Point->coordinates;

    var_dump($lat);
    var_dump($long); 
    The first number is long, then lat because 33 deg. North, 84 deg. West sounds about right for a city somewhere in the United States. (I have no idea where Atlanta actually is though, I've never been to the USA!)
    Last edited by XmisterIS; 01-13-2012 at 01:39 PM.

  • Users who have thanked XmisterIS for this post:

    dk4210 (01-13-2012)

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    104
    Thanks
    13
    Thanked 0 Times in 0 Posts
    I just got the fix and it is I had this wrong

    $lat = $output->Placemark[0]->Point->coordinates[1];
    $long = $output->Placemark[0]->Point->coordinates[0];


    It's working fine now! Thanks for your help!


  •  

    Posting Permissions

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