dericcain/geo-things

接收各种输入并进行地理解码/编码等。

v1.0 2016-12-28 11:59 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:25:22 UTC


README

#Geo Thing Build Status

描述

是否需要快速从一组坐标获取地址?或者从地址获取坐标,甚至是两个地址之间的距离?这是一个非常简单的包,它使用Google的API执行这些操作。默认情况下,您无需提供API密钥,但您的API请求数量将受到限制。如果您不进行大量调用,这应该足够了。

安装

使用composer按如下方式安装此包

composer require dericcain/geo-things

使用方法

使用此包相当简单。您需要在PHP文件顶部导入该包。一旦完成,您就可以使用以下不同的方法。

从地址获取坐标

// You will need to declare the namespace
use GeoThing/GeoThing;

$address = '123 Main Street';
$zip = '32119';

$results = GeoThing::getCoordinates($address, $zip);

$results->lat // 33.5075002
$results->lng // -86.8105789
$results->error // The error code from Google if there is one. This attribute will not be here if there is not error.

如果没有结果或发生错误,返回的对象将有一个error属性,说明错误原因。此外,latlng属性将设置为null

从坐标获取地址

// You will need to declare the namespace
use GeoThing/GeoThing;

$response = GeoThing::getAddress($lat, $lng);

$response->error // This will only be set if there is an error
$response->street_number // The number only
$response->street_name // The name of the street
$response->city // The full city name
$response->state // The full state name, not the abbreviation
$response->zip // The zip code
$response->formatted_address // The full formated address "277 Bedford Avenue, Brooklyn, NY 11211, USA"

获取起点和终点之间的距离

// You will need to declare the namespace
use GeoThing/GeoThing;

$response = GeoThing::getDistance($origin, $destination);

$response->error // This will only be set if there is an error
$response->distance // This will be a string like "1.2 mi" (I'll change this soon)
$response->duration // This will also be a string as of right now

辅助函数

使用Composer安装此包后,您将有权访问一些全局辅助函数。如果您想在视图中调用不同的函数或难以声明use语句的地方,这将很有用。以下是一些辅助函数

getAddress($lat, $lng, $apiKey); // $apiKey is optional
getCoordinates($address, $zip, $apiKey); // $apiKey is optional
getDistance($origin, $destination, $apiKey); // $apiKey is optional

贡献

请随时帮助这个小项目。如果您发现错误或想添加某些内容,请告诉我。如果您发起拉取请求,请确保测试您的代码,并且所有测试都通过。这是合并工作所必需的。

联系

给我发消息!

TODO

  • 获取两组坐标之间的距离
  • 添加KM作为距离选项