hugobots/php-geocode

Google 地理编码API的封装,用于获取与特定地址相关的不同详细信息,如纬度、经度、国家、城市、区、邮编、镇和街道编号。

v2.0 2016-01-22 19:59 UTC

This package is not auto-updated.

Last update: 2024-09-18 22:05:51 UTC


README

不再维护,更好的替代方案是 geocoder-php

PHP 地理编码

Build Status Scrutinizer Code Quality

Google 地理编码API的封装,用于获取地址的相关详细信息,例如

  • 纬度/经度
  • 国家
  • 城市
  • 邮编
  • 街道编号

要求

PHP >= 5.4.0 和已启用 curl 的服务器。

安装

您可以使用以下方法安装库

Composer

推荐的安装方法是使用Composer,PHP的依赖管理器。只需将hugobots/php-geocode添加到您的项目composer.json文件中

{
    "require": {
        "hugobots/php-geocode": "*"
    }
}

然后运行composer install。有关更多详细信息,您可以在Packagist上找到该包。

手动方式

或者,您可以通过以下方式手动安装包

  • src/php-geocode.php复制到您的代码库中,例如到供应商目录。
  • Geocode类添加到您的自动加载器或直接require该文件。

入门

我将使用以下地址来解释库的使用,即

1600 Amphitheatre Parkway, Mountain View, CA

实例化Geocode类并按以下方式调用方法

// Introduce the class into your scope
use KamranAhmed\Geocode\Geocode;


// Optionally you can pass the API key for Geocoding
$geocode = new Geocode();

// Get the details for the passed address
$location = $geocode->get("1600 Amphitheatre Parkway, Mountain View, CA");

// Note: All the functions below accept a parameter as a default value that will be return if the reuqired value isn't found
$location->getAddress( 'default value' ); 
$location->getLatitude(); // returns the latitude of the address
$location->getLongitude(); // returns the longitude of the address
$location->getCountry(); // returns the country of the address
$location->getLocality(); // returns the locality/city of the address
$location->getDistrict(); // returns the district of the address
$location->getPostcode(); // returns the postal code of the address
$location->getTown(); // returns the town of the address
$location->getStreetNumber(); // returns the street number of the address
$location->getNeighborhood(); // returns the neighborhood of the address

反馈

我很乐意听取您的意见。请为任何您可能需要的功能请求或您注意到的错误打开一个问题。您也可以通过kamranahmed.se@gmail.com联系我,或者您也可以在twitter上找到我@kamranahmedse

注意

应注意的是,Google 地理编码API有以下限制,在使用此封装之前请牢记

  • 每24小时最多2500次请求。
  • 每秒最多5次请求。