SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Google Maps geocoding api

    Hi,

    I am trying to convert an address to coordinates using the following but it doesn't work (no errors displayed):

    PHP Code:
    <?php
    $data 
    = new SimpleXMLElement(file_get_contents('http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false'));

    $latitude $data->GeocodeResponse->result->geometry->location->lat;
    $longitude $data->GeocodeResponse->result->geometry->location->lng;

    echo 
    $latitude;
    echo 
    $longitude;
    ?>
    I guess I am doing something wrong but I can't see what.

    Thanks for any ideas.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    try:
    PHP Code:
    var_dump($data); 
    Maybe you have misspelt a key ...

    EDIT seems not, but keep adding accessors and see where it goes wrong ...

    $data->GeocodeResponse and so on ...

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    XML parsing strips off the first element of the return as the base container. $data is already pointing at the GeocodeResponse.

    $latitude = $data->result->geometry->location->lat;

    will get you your value.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    XML parsing strips off the first element of the return as the base container. $data is already pointing at the GeocodeResponse.

    $latitude = $data->result->geometry->location->lat;

    will get you your value.
    Thanks, I noticed that shortly after posting this question. I forgot to update here.


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
  •