PHP JSON Google maps

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whitep8
    New Member
    • Oct 2009
    • 65

    PHP JSON Google maps

    Hi All,

    Im attempting to get a return from google maps to display the lat and long of a postcode but its not outputting them, but it is if i convert it to a raw URL

    any help would be appreciated. Ive come to a dead end
    Code:
    <?php
    function get_lat_long($postcode) {
            #change to your local domain i.e. .com
    echo	$domain = "co.uk";
    echo "domain<br><br>";
    echo	$maps_key = "ABQIAAAAVHrPYH_Sk6LVC-esA5cvrhSBDl4JnyGZq3II_C_YS5ykoCjTcBRI61RIZRv6SGWAhpvhTZdb-6YsXQ";
    echo "maps key<br><br>";
    echo	$url = "http://maps.google." . $domain . "/maps/geo?q=" . urlencode($postcode) . "&output=json&key=". $maps_key;
    echo "url<br><br>";
    echo	$curl = new curl();
    echo "curl<br><br>";
    echo	$json = $curl->get_page(array("url"=>$url));
    echo "json<br><br>";
    echo	$store_data = json_decode(str_replace("&quot;","\"",htmlentities($json))); //Take care of accents
    echo "store data<br><br>";
    echo	$lng = $store_data->Placemark[0]->Point->coordinates[0];
    echo "$lng<br><br>";
    echo	$lat = $store_data->Placemark[0]->Point->coordinates[1];
    
    	//Return
    	if($lng && $lat) {
    		return array('lat'=>$lat,
    		'lng'=>$lng
    		);
    
    	} else {
    
    		return false;
    
    	}
    
    }
    #this extracts the lat and long from the bottom - to call it use  this command:
    echo "lat<br><br>";
    echo $coords = get_lat_long('NE1 1AA');
    echo "postcode<br><br>";
    #retults are returned like so
    
    echo $coords['lat']; //latitude
    echo $coords['lng']; // longtitude
    ?>
    this is the raw URL response

    Code:
    {
      "name": "NE1 1AA",
      "Status": {
        "code": 200,
        "request": "geocode"
      },
      "Placemark": [ {
        "id": "p1",
        "address": "Newcastle Upon Tyne NE1 1, UK",
        "AddressDetails": {
       "Accuracy" : 5,
       "Country" : {
          "AdministrativeArea" : {
             "AdministrativeAreaName" : "Newcastle Upon Tyne",
             "SubAdministrativeArea" : {
                "Locality" : {
                   "LocalityName" : "Newcastle Upon Tyne",
                   "PostalCode" : {
                      "PostalCodeNumber" : "NE1 1"
                   }
                },
                "SubAdministrativeAreaName" : "Newcastle Upon Tyne"
             }
          },
          "CountryName" : "UK",
          "CountryNameCode" : "GB"
       }
    },
        "ExtendedData": {
          "LatLonBox": {
            "north": 54.9734515,
            "south": 54.9626769,
            "east": -1.6062257,
            "west": -1.6221915
          }
        },
        "Point": {
          "coordinates": [ -1.6135470, 54.9694353, 0 ]
        }
      } ]
    }
    any help would be appreciated
Working...