php know if city exist gmap api 1

php know if city exist gmap api

   public static function  latlong($location) {

        if ($location!='') {
            try {
                $json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($location));
                $parsedjson = json_decode($json, true);

                if (key_exists(0,$parsedjson['results'])) {
                    $lat_long_array = $parsedjson['results'][0]['geometry']['location'];
                    return $lat_long_array;
                } else {
                    return false;
                }
            } catch (Exception $e) {
                //echo 'Caught exception: ',  $e->getMessage(), "\n";
                return false;
            }
        }


    }

Here is what the above code is Doing:
1. We’re using the file_get_contents() function to get the contents of the Google Maps API URL.
2. We’re using the json_decode() function to decode the JSON data.
3. We’re using the key_exists() function to check if the key exists in the array.
4. We’re returning the latitude and longitude values.

Similar Posts