opencage/geocode

一个通过 OpenCage 地理编码 API 进行地理编码的 PHP 库

v3.3.0 2024-03-11 18:19 UTC

README

一个用于使用PHP库来调用OpenCage 地理编码 API

构建状态 / 代码质量

Build Status PHP version Mastodon Follow

概述

此库尝试使用CURL扩展来访问 OpenCage 地理编码 API。如果 CURL 支持不可用,库将回退到使用fopen 包装器

要使用此库,您必须将 CURL 扩展编译到您的 PHP 版本中。或者,通过在您的 php.ini 中的 allow_url_fopen 指令配置 fopen 包装器的使用。

身份验证

您需要一个 API 密钥,您可以在此处注册。

教程

您可以在 OpenCage 网站上找到使用此模块的全面教程

安装

通过 Composer

推荐的 - 也是最容易的方式 - 安装是通过 Composer。在您的项目的 composer.json 文件中添加库要求。

$ composer require opencage/geocode

导入地理编码类。

require "vendor/autoload.php";

老式方法

查看 demo/geocode.php 文件

地理编码

$geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');
$result = $geocoder->geocode('82 Clerkenwell Road, London');
print_r($result);

反向地理编码

$geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');
$result = $geocoder->geocode('43.831,4.360'); # latitude,longitude (y,x)
print $result['results'][0]['formatted'];
// 3 Rue de Rivarol, 30020 Nîmes, France

设置可选参数

完整的列表请参见:https://opencagedata.com/api#optional-params

$result = $geocoder->geocode('6 Rue Massillon, 30020 Nîmes, France', [
    'language' => 'fr',
    'countrycode' => 'fr'
]);
if ($result && $result['total_results'] > 0) {
  $first = $result['results'][0];
  print $first['geometry']['lng'] . ';' . $first['geometry']['lat'] . ';' . $first['formatted'] . "\n";
  // 4.360081;43.8316276;6 Rue Massillon, 30020 Nîmes, Frankreich
}

设置代理 URL

$geocoder->setProxy('http://proxy.example.com:1234');
$result = $geocoder->geocode("Brandenburger Tor, Berlin");
print_r($result['results'][0]['formatted']);
// Brandenburger Tor, Unter den Linden, 10117 Berlin, Germany
print_r($result['results'][0]['geometry']);
// Array
// (
//    [lat] => 52.5166047
//    [lng] => 13.3809897
// )

示例结果

Array
(
    [total_results] => 2
    [status] => Array
        (
            [message] => OK
            [code] => 200
        )
    [results] => Array
        (
            [0] => Array
                (
                    [annotations] => Array
                        (
                            [DMS] => Array
                                (
                                    [lat] => 51° 31' 21.60894'' N
                                    [lng] => 0° 6' 8.95198'' E
                                )

                            [MGRS] => 30UYC0100511930
                            [Maidenhead] => IO91wm75qk
                            [Mercator] => Array
                                (
                                    [x] => -11408.763
                                    [y] => 6680801.955
                                )

                            [OSGB] => Array
                                (
                                    [easting] => 531628.199
                                    [gridref] => TQ 316 821
                                    [northing] => 182177.015
                                )

                            [OSM] => Array
                                (
                                    [url] => http://www.openstreetmap.org/?mlat=51.52267&mlon=-0.10249#map=17/51.52267/-0.10249
                                )

                            [callingcode] => 44
                            [geohash] => gcpvjemm7csmhczg9cvt
                            [sun] => Array
                                (
                                    [rise] => Array
                                        (
                                            [apparent] => 1452931140
                                            [astronomical] => 1452923940
                                            [civil] => 1452928800
                                            [nautical] => 1452926280
                                        )

                                    [set] => Array
                                        (
                                            [apparent] => 1452961320
                                            [astronomical] => 1452968520
                                            [civil] => 1452963660
                                            [nautical] => 1452966120
                                        )

                                )

                            [timezone] => Array
                                (
                                    [name] => Europe/London
                                    [now_in_dst] => 0
                                    [offset_sec] => 0
                                    [offset_string] => 0
                                    [short_name] => GMT
                                )

                            [what3words] => Array
                                (
                                    [words] => gallons.trim.tips
                                )

                        )

                    [bounds] => Array
                        (
                            [northeast] => Array
                                (
                                    [lat] => 51.5227563
                                    [lng] => -0.1023801
                                )

                            [southwest] => Array
                                (
                                    [lat] => 51.5226042
                                    [lng] => -0.1025907
                                )

                        )

                    [components] => Array
                        (
                            [city] => London
                            [country] => United Kingdom
                            [country_code] => gb
                            [house_number] => 82
                            [postcode] => EC1M 5RF
                            [road] => Clerkenwell Road
                            [state] => England
                            [state_district] => Greater London
                            [suburb] => Clerkenwell
                        )

                    [confidence] => 10
                    [formatted] => 82 Clerkenwell Road, London EC1M 5RF, United Kingdom
                    [geometry] => Array
                        (
                            [lat] => 51.52266915
                            [lng] => -0.10248666188363
                        )

                )

            [1] => Array
                (
                    [annotations] => Array
                        (
                            [DMS] => Array
                                (
                                    [lat] => 51° 30' 30.70800'' N
                                    [lng] => 0° 7' 32.66400'' E
                                )

                            [MGRS] => 30UXC9945410295
                            [Maidenhead] => IO91wm42vb
                            [Mercator] => Array
                                (
                                    [x] => -13997.313
                                    [y] => 6678279.278
                                )

                            [OSGB] => Array
                                (
                                    [easting] => 530055.544
                                    [gridref] => TQ 300 805
                                    [northing] => 180563.298
                                )

                            [OSM] => Array
                                (
                                    [url] => http://www.openstreetmap.org/?mlat=51.50853&mlon=-0.12574#map=17/51.50853/-0.12574
                                )

                            [geohash] => gcpvj0u6yjcmwxz8bn43
                            [sun] => Array
                                (
                                    [rise] => Array
                                        (
                                            [apparent] => 1452931140
                                            [astronomical] => 1452923940
                                            [civil] => 1452928800
                                            [nautical] => 1452926340
                                        )

                                    [set] => Array
                                        (
                                            [apparent] => 1452961320
                                            [astronomical] => 1452968520
                                            [civil] => 1452963660
                                            [nautical] => 1452966120
                                        )

                                )

                            [timezone] => Array
                                (
                                    [name] => Europe/London
                                    [now_in_dst] => 0
                                    [offset_sec] => 0
                                    [offset_string] => 0
                                    [short_name] => GMT
                                )

                            [what3words] => Array
                                (
                                    [words] => thing.then.link
                                )

                        )

                    [bounds] => Array
                        (
                            [northeast] => Array
                                (
                                    [lat] => 51.7202301025
                                    [lng] => 0.336111992598
                                )

                            [southwest] => Array
                                (
                                    [lat] => 51.2786598206
                                    [lng] => -0.523222982883
                                )

                        )

                    [components] => Array
                        (
                            [country] => United Kingdom
                            [county] => Greater London
                            [state] => England
                            [town] => London
                        )

                    [confidence] => 1
                    [formatted] => London, Greater London, United Kingdom
                    [geometry] => Array
                        (
                            [lat] => 51.50853
                            [lng] => -0.12574
                        )

                )

        )
)

版权

版权所有 (c) OpenCage GmbH。有关详细信息,请参阅 LICENSE。

OpenCage GmbH 是谁?

我们运行一个全球性的地理编码 API和基于开放数据的地理搜索服务。了解我们更多信息

我们还运营Geomob,这是一个针对基于位置的服务创建者的定期聚会系列,我们尽力突出地理创新。如果您喜欢地理事物,您可能会喜欢Geomob播客

-- 结束 --