Geocoding locality

Hi, Does anyone know how the get the adres (only locality) of coordinates? I already have the coord, and the api key but I can´t seem to grap only the variable where type is locality.

Right now this is what i´ve got, but from that address_components array I need the long_name where the type is [‘locality’]

<?php

namespace myPHPnotes;

class Geocoding
{
    protected $api_key;
    protected $debug;
    protected $callurl = "https://maps.googleapis.com/maps/api/geocode/json";
    function __construct($api_key, $debug = 0)
    {
        $this->api_key = $api_key;
        $this->debug = $debug;    
    }

    public function request($url, $parameters)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url . "?" . http_build_query($parameters));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $result = curl_exec($ch);
        return $result;
    }

    public function getAddress($latitude, $longitude)
    {
        $data = [
            'latlng' => "$latitude,$longitude",
            'key' => $this->api_key
        ];

        $addressData = $this->request($this->callurl, $data);
	 
		return (json_decode($addressData)->results[0]->address_components);
    }
	
    public function getCoordinates($address)
    {
        $data = [
            'address' => $address,
            'key' => $this->api_key
        ];
		
        $addressData = $this->request($this->callurl, $data);
        $location = json_decode($addressData)->results[0]->geometry->location;
        return [ 'latitude' => $location->lat, 'longitude' => $location->lng];
    }
}
Sponsor our Newsletter | Privacy Policy | Terms of Service