Get Google Places ID from coordinates - php

I am looking for a way to find the PlaceID from a Google web request. I have several hundred addresses (i.e. coordinates) and I want to find a way to all enter them in a phpscript.
I am struggling with the API request for the place API. I have the following request:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=50.096332,8.673130&radius=1&key=APIKEY
This returns the entire city of Frankfurt, but that's not what I am after. If I enter the address Oskar-Sommer-Strasse 7, Frankfurt am Main, Deutschland into the PlaceID finder on the Google Maps documentation site, I get the following result for the ID ChIJ-b9GgAUMvUcRYuYKJtGGlw4.
I am now looking for a way to get this done. I've collected the coordinates using Google Maps API as well, so they should match (or at least that's what I thought).
Maybe I need to use some other request type?

Using the geocode api and sending a request to
https://maps.googleapis.com/maps/api/geocode/json?address=Oskar-Sommer-Strasse+7+%20Frankfurt%20am%20Main%20&component=Deutschland
returns (I'm assuming you want the place id field)
{
"results" : [
{
"address_components" : [
{
"long_name" : "7",
"short_name" : "7",
"types" : [ "street_number" ]
},
{
"long_name" : "Oskar-Sommer-Straße",
"short_name" : "Oskar-Sommer-Straße",
"types" : [ "route" ]
},
{
"long_name" : "Frankfurt am Main Süd",
"short_name" : "Frankfurt am Main Süd",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Frankfurt am Main",
"short_name" : "Frankfurt am Main",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Darmstadt",
"short_name" : "DA",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Hessen",
"short_name" : "HE",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Germany",
"short_name" : "DE",
"types" : [ "country", "political" ]
},
{
"long_name" : "60596",
"short_name" : "60596",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Oskar-Sommer-Straße 7, 60596 Frankfurt am Main, Germany",
"geometry" : {
"location" : {
"lat" : 50.09665,
"lng" : 8.67299
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 50.0979989802915,
"lng" : 8.674338980291502
},
"southwest" : {
"lat" : 50.0953010197085,
"lng" : 8.671641019708497
}
}
},
"place_id" : "ChIJ-b9GgAUMvUcRYuYKJtGGlw4",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}

Related

How to access json file element

So I am retrieving a json file from an API, and I want to grab the data from object 'place_id'.
I have been looking online and I have seen how to grab elements from json files, however I can't understand what I am doing wrong, as my code gives me an error:
Warning: Undefined array key "place_id" in C:\xampp\htdocs\cw\tester.php on line 15
This is my php code where I try to grab the data from 'place_id':
$place_json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=belfast+zoo&key=' . $place_id_key);
$json = json_decode($place_json, true);
echo $json['results']['place_id'];
this is my json file:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Antrim Road",
"short_name" : "Antrim Rd",
"types" : [ "route" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Newtownabbey",
"short_name" : "Newtownabbey",
"types" : [ "postal_town" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Northern Ireland",
"short_name" : "Northern Ireland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "BT36 7PN",
"short_name" : "BT36 7PN",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Antrim Rd, Belfast, Newtownabbey BT36 7PN, UK",
"geometry" : {
"location" : {
"lat" : 54.6577821,
"lng" : -5.943665699999999
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 54.65913108029149,
"lng" : -5.942316719708497
},
"southwest" : {
"lat" : 54.6564331197085,
"lng" : -5.945014680291502
}
}
},
"place_id" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
"plus_code" : {
"compound_code" : "M354+4G Belfast, UK",
"global_code" : "9C6PM354+4G"
},
"types" : [ "establishment", "point_of_interest", "tourist_attraction", "zoo" ]
}
],
"status" : "OK"
}
Apologies as I know it is obvious, but I can't seem to understand why! thanks!
This should do the trick? Notice the [0] that was added. Since results is actually an array.
<?php
$place_json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=belfast+zoo&key=' . $place_id_key);
$json = json_decode($place_json, true);
echo $json['results'][0]['place_id'];

Laravel reverse geocode the latitude and longitude

Im using Laravel and this dependancy https://github.com/alexpechkarev/google-geocoder to reverse geocode my latitude and longitude what I did first is to put a static lat and lng.
In my controller I have this:
public function location(){
$lat=40.714224;
$lng=-73.961452;
$param = array("latlng"=>"$lat,$lng");
$response = \Geocoder::geocode('json', $param);
return $response;
}
This returns.
{
"results" : [
{
"address_components" : [
{
"long_name" : "277",
"short_name" : "277",
"types" : [ "street_number" ]
},
{
"long_name" : "Bedford Avenue",
"short_name" : "Bedford Ave",
"types" : [ "route" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Kings",
"short_name" : "Kings",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "277 Bedford Avenue, Brooklyn, NY 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.714232,
"lng" : -73.9612889
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.7155809802915,
"lng" : -73.9599399197085
},
"southwest" : {
"lat" : 40.7128830197085,
"lng" : -73.96263788029151
}
}
},
"place_id" : "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
"types" : [ "street_address" ]
},
],
"status" : "OK"
}
Now what I want to do is to get the "formatted_address" of the results. Can you help me with this.

Google Maps API - Retrieving city and state using PHP

I'm currently using Google Maps API to get a longitude and latitude form the user's street, street number and zip code.
I would also like to grab their city and state from the Maps API results. The XML document formats it strangely though, and I'm not sure how I'm supposed to reference it (it's different from the way I'm grabbing the long and lat).
This is how I am loading the Maps API:
$map_address = str_replace($address."+".$zip);
$map_api_url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$map_address."&key=myKey";
$resp_json = file_get_contents($map_api_url);
$resp = json_decode($resp_json, true);
I then get the Long and Lat:
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
But how do I grab the city and state?
{
"results" : [
{
"address_components" : [
{
"long_name" : "3840",
"short_name" : "3840",
"types" : [ "street_number" ]
},
{
"long_name" : "Fake Street",
"short_name" : "Fake St",
"types" : [ "route" ]
},
{
"long_name" : "Wilson Heights",
"short_name" : "Wilson Heights",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "North York",
"short_name" : "North York",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Toronto",
"short_name" : "Toronto",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Toronto Division",
"short_name" : "Toronto Division",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Ontario",
"short_name" : "ON",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Canada",
"short_name" : "CA",
"types" : [ "country", "political" ]
},
{
"long_name" : "M6H",
"short_name" : "M6H",
"types" : [ "postal_code_prefix", "postal_code" ]
}
],
"formatted_address" : "3840 Fake Street, Toronto, ON M6H, Canada",
"geometry" : {
"location" : {
"lat" : 43.5399244,
"lng" : -78.43486559999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 43.5412733802915,
"lng" : -78.43351661970848
},
"southwest" : {
"lat" : 43.7385754197085,
"lng" : -79.43621458029151
}
}
},
"place_id" : "ChIJPVDxjGgyK4gRt3O7zOSmIF4",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
Just answered my own question by running the code through a loop. Please let me know if you have a better way!
foreach($resp['results'][0]['address_components'] as $address_componenets) {
if($address_componenets["types"][0]=="country") {
$country = $address_componenets["long_name"];
}
if($address_componenets["types"][0]=="administrative_area_level_1") {
$state = $address_componenets["long_name"];
}
if($address_componenets["types"][0]=="locality") {
$city = $address_componenets["long_name"];
}
}

How to Fetch google APi array

I want to fetch Postcode [ "long_name" : "S35 1TZ",] from this google api array
Iam usin this API
http://maps.googleapis.com/maps/api/geocode/json?address=(53.4624118,%20-1.4922259999999596)
Using
msg.results[0].address_components[6].long_name;
Not working
{
"results" : [
{
"address_components" : [
{
"long_name" : "7A",
"short_name" : "7A",
"types" : [ "street_number" ]
},
{
"long_name" : "Hollow Gate",
"short_name" : "Hollow Gate",
"types" : [ "route" ]
},
{
"long_name" : "Chapeltown",
"short_name" : "Chapeltown",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Sheffield",
"short_name" : "Sheffield",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Sheffield",
"short_name" : "Sheffield",
"types" : [ "postal_town" ]
},
{
"long_name" : "South Yorkshire",
"short_name" : "South York",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "S35 1TZ",
"short_name" : "S35 1TZ",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "7A Hollow Gate, Sheffield, South Yorkshire S35 1TZ, UK",
"geometry" : {
"location" : {
"lat" : 53.4624118,
"lng" : -1.492226
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 53.4637607802915,
"lng" : -1.490877019708498
},
"southwest" : {
"lat" : 53.4610628197085,
"lng" : -1.493574980291502
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
There are two ways. Based on your code, I am not sure if you got a string of that data or already an object. If you only got data back, it is a string and you first have to convert the JSON into PHP array or object.
1 - You could convert the JSON to an array:
$msg = json_decode($msg,true);
/* And call it by */
echo($msg['results'][0]['address_components'][7]['long_name']);
OR
2 - Using the object notation:
$msg = json_decode($msg);
/* And call it by */
echo($msg->results[0]->address_components[7]->long_name);
If you got the data back using a Google API PHP libary, it's probably not a string but an object so you can dismiss the json_decode() and use option 2. If you want to prefer using option 1, you can cast the object into an array by using $msg = (array)$msg;
In general using print_r($msg) or var_dump($msg) can be handy to debug the thing, just add your path each time you get closer to your goal. So in this case var_dump($msg) then var_dump($msg->result), then var_dump($msg->result[0]), and so on.

Parsing Google Maps API for Latitude and Longitude

I am trying to get the lat/lng of an address with the Google maps API. I am having trouble parsing the JSON result just to store the lat and lng values as PHP variables. Anyone able to help me parse this?
This is what I have so far:
<?php
$jsonObject = file_get_contents(
"http://maps.googleapis.com/maps/api/geocode/json?
address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true");
$object = json_decode($jsonObject);
$lat = $object->results[0]->geometry[0]->location[0]->lat;
$lng = $object->results[0]->geometry[0]->location[0]->lng;
echo "$lat, $lng";
?>
This is the JSON feed (the lat/lng values are about two-thirds the way down the code):
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara",
"short_name" : "Santa Clara",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4216410,
"lng" : -122.08550160
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.42298998029150,
"lng" : -122.0841526197085
},
"southwest" : {
"lat" : 37.42029201970850,
"lng" : -122.0868505802915
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
geometry and location aren't arrays, try
$lat = $object->results[0]->geometry->location->lat;
$lng = $object->results[0]->geometry->location->lng;

Categories