src/Services/GMaps.php
public function generateLink(string $keyword) : string
    {
        $response = file_get_contents('https://maps.googleapis.com/maps/api/place/textsearch/json?query=' . urlencode($keyword) . '&key=' . $this->apiKey);
        $data     = json_decode($response);

        if (empty($data->results) || $data->status == 'ZERO_RESULTS') {
            throw new \Exception('GoogleMaps exception: ADDRESS NOT FOUND. [' . $keyword . ']', 1);
        }

        $lat = $data->results[0]->geometry->location->lat;
        $lng = $data->results[0]->geometry->location->lng;

        return 'http://maps.google.com/maps?q=' . $lat . ',' . $lng . '&z=17';
    }
public function generateLink(string $keyword) : string
    {
        $response = file_get_contents('https://maps.googleapis.com/maps/api/place/textsearch/json?query=' . urlencode($keyword) . '&key=' . $this->apiKey);
        $data     = json_decode($response);

        if (empty($data->results) || $data->status == 'ZERO_RESULTS') {
            throw new \Exception('GoogleMaps exception: ADDRESS NOT FOUND. [' . $keyword . ']', 1);
        }

        $lat = $data->results[0]->geometry->location->lat;
        $lng = $data->results[0]->geometry->location->lng;

        return 'http://maps.google.com/maps?q=' . $lat . ',' . $lng . '&z=17';
    }
public function generateLink ( string $ keyword ) : string { $ response = file_get_contents ( 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=' . urlencode ( $ keyword ) . '&key=' . $ this -> apiKey ) ; $ data = json_decode ( $ response ) ; if ( empty ( $ data -> results ) || $ data -> status == 'ZERO_RESULTS' ) { throw new \ Exception ( 'GoogleMaps exception: ADDRESS NOT FOUND. [' . $ keyword . ']' , 1 ) ; } $ lat = $ data -> results [ 0 ] -> geometry -> location -> lat ; $ lng = $ data -> results [ 0 ] -> geometry -> location -> lng ; return 'http://maps.google.com/maps?q=' . $ lat . ',' . $ lng . '&z=17' ; }
Search the most relevant place based on a keyword and return a link to its location

@param string $keyword An address or place. Ex: '5th Avenue, New York' or 'Eiffel Tower'

@return string A GoogleMaps link

@throws \Exception When the given address was not found
Search the most relevant place based on a keyword and return a link to its location