lkrhl / geocoder

该包已 废弃 且不再维护。未建议替代包。

使用 Google 地图 API 的地理编码和反向地理编码的简单 Laravel 包

dev-master 2019-10-25 09:50 UTC

This package is auto-updated.

Last update: 2023-11-22 12:51:25 UTC


README

关于

使用 Google 地图 API 的地理编码和反向地理编码的简单 Laravel 包

安装

composer require lkrhl/geocoder

发布配置文件

php artisan vendor:publish --provider="Lkrhl\Geocoder\GeocoderServiceProvider" --tag="config"

示例

使用 Geocoder 门面

// Convert address to coordinates
try {
    Geocoder::geocode("Champ de Mars, 2 Allée Adrienne Lecouvreur, Paris", "France", "75007");
} catch (GeocodingServiceConnectionError | GeocodingServiceError $ex) {
    // handle exceptions
}
/*
[
    "address" => "Champ de Mars, 2 Allée Adrienne Lecouvreur, 75007 Paris, France",
    "accuracy" => 0,
    "lat" => 48.8556475,
    "lng" => 2.2986304
]
*/

// Convert coordinates to address
try {
    Geocoder::reverseGeocode("41.8899435", "12.4943371");
} catch (GeocodingServiceConnectionError | GeocodingServiceError $ex) {
    // handle exceptions
}
/*
[
    "address" => "Piazza del Colosseo, 1, 00184 Roma RM, Italy",
    "accuracy" => 0,
    "lat" => 41.8899435,
    "lng" => 12.4943371
]
*/

$client = new \GuzzleHttp\Client();

$geocoder = new Geocoder($client);

// $geocoder->setApiKey($apiKey);
$geocoder->setApiKey(config('geocoder.api_key'));

// Set response language (optional)
$geocoder->setLanguage($language);

try {
    $geocoder->geocode($streetAddress, $country, $postalCode);
    $geocoder->reverseGeocode($latitude, $longitude);
} catch (GeocodingServiceConnectionError | GeocodingServiceError $ex) {
    // handle exceptions
}

其他

accuracy 字段对应 location_type 参数

0 => ROOFTOP (most accurate)
1 => RANGE_INTERPOLATED
2 => GEOMETRIC_CENTER
3 => APPROXIMATE