shumex/geocode

Google Geocode API for Laravel

1.0 2017-12-22 21:12 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:01:42 UTC


README

这是一个简单的Laravel服务提供者,用于Google Geocoding API。

安装

可以通过Composer安装此包。

运行composer require命令。

composer require shumex/geocode

Laravel 5.5

Laravel 5.5包发现将自动安装服务提供者和别名。

配置

将以下行添加到.env文件中

GEOCODE_GOOGLE_APIKEY=<your_google_api_key>

您可以可选地设置响应语言。

GEOCODE_GOOGLE_LANGUAGE=en # pt-BR, es, de, it, fr, en-GB

支持的语言用于Google Maps Geocoding API。

用法

您可以从地址找到数据

try {
    $response = Geocode::make()->address('Saporischschja', 'de');
    if ($response) {
        echo $response->latitude();
        echo "<br>";
        echo $response->longitude();
        echo "<br>";
        echo $response->formattedAddress();
        echo "<br>";
    }
} catch (GeoException $exception) {
    echo $exception->getMessage();
}

// Output
// 35.139567
// 47.8388
// Saporischschja, Ukraine, 69061

或者从纬度/经度

try {
    $response = Geocode::make()->latLng(47.850437, 35.135653, 'en');
    if ($response) {
        echo $response->latitude();
        echo "<br>";
        echo $response->longitude();
        echo "<br>";
        echo $response->formattedAddress();
        echo "<br>";
    }
} catch (GeoException $exception) {
    echo $exception->getMessage();
}

// Output
// 47.850437
// 35.135653
// Volhohrads'ka St, 27, Zaporizhzhia, Zaporiz'ka oblast, Ukraine, 69000

如果您需要除了格式化地址、纬度和经度之外的其他数据,可以使用raw()方法

try {
    $response = Geocode::make()->latLng(40.7637931,-73.9722014);
    if ($response) {
        echo $response->raw()->address_components[8]['types'][0];
        echo $response->raw()->address_components[8]['long_name'];
    }
} catch (GeoException $exception) {
    echo $exception->getMessage();
}

// Output
// postal_code
// 10153