Last active
October 22, 2019 13:52
-
-
Save patrickmaciel/5e2bb0f8ddcef3f68a58497013276455 to your computer and use it in GitHub Desktop.
Google Maps URL to check if place or address exists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// example: https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=YOUR_PLACE_OR_ADDRESS_HERE,YOUR_CITY_HERE%2C+TO%2C+Brasil&inputtype=textquery&fields=photos,formatted_address,name,rating,opening_hours,geometry&key=YOUR_KEY_HERE | |
function cleanString($string) { | |
$string = str_replace('&','e', $string); | |
$string = str_replace("'",'', $string); | |
return $string; | |
} | |
function geocode($place, $city){ | |
$return = array(); | |
$place = urlencode(cleanString($place)); | |
$city = urlencode($city); | |
$key = env('GOOGLE_API_KEY'); | |
$url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input={$place},${city}&inputtype=textquery&fields=photos,formatted_address,name,rating,opening_hours,geometry&key={$key}"; | |
$resp_json = file_get_contents($url); | |
$resp = json_decode($resp_json, true); | |
if($resp['status']!=='OK') return false; | |
return true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"candidates":[ | |
{ | |
"formatted_address":"Espaço Médico Empresarial - Conj. 1, Lt. 1,, Av. Joaquim Teotônio Segurado, 304 - Plano Diretor Sul, Palmas - TO, 77015-550, Brasil", | |
"geometry":{ | |
"location":{ | |
"lat":-10.2103655, | |
"lng":-48.3342191 | |
}, | |
"viewport":{ | |
"northeast":{ | |
"lat":-10.20916107010728, | |
"lng":-48.33286257010727 | |
}, | |
"southwest":{ | |
"lat":-10.21186072989272, | |
"lng":-48.33556222989272 | |
} | |
} | |
}, | |
"name":"Psiquê - Valorizando Pessoas", | |
"opening_hours":{ | |
"open_now":true | |
}, | |
"photos":[ | |
{ | |
"height":264, | |
"html_attributions":[ | |
"<a href=\"https://maps.google.com/maps/contrib/108219149349085107427/photos\">Psiquê - Valorizando Pessoas</a>" | |
], | |
"photo_reference":"CmRaAAAAm24dejrQzlzqm6kNpnYoE1bZIWtB0QMzXrJBlXPpJWYRbcwC1Amx7J5m0489dYFGyuzS0eW1j5AicHUv_vhlDG2KUSk-r5qNNVWtlMM_5QJGS7xIH_R8vKf5hxKQOJ0gEhA_cqMVV4RRYuF_6Nrmwyj0GhScFecPhwKJYakyl2GZuXRTzUeeDg", | |
"width":264 | |
} | |
], | |
"rating":4.2 | |
} | |
], | |
"status":"OK" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment