thorazine/location
Composer 安装命令:
composer require thorazine/location
包简介
A location reverser for Laravel with Google API
README 文档
README
Geo data to Geolocation
Get a complete standardized location php array or object from coordinates, address, postal code or IP. Through the Location Facade you can request the Google and IpInfo API to return the address of a visitor on your website. This script works out of the box, no need for any keys or registrations.
What you should keep in mind
This script uses the Google geodata and maps API to request information. Especially with the IP API there is margin for error. The Google API is quite accurate. However, please don't use this data as fact but rather as indication.
How to make it work
Run:
composer require thorazine/location
That's it
If you are on Laravel < 5.5
You can't use this version. Please go to the pre55 branch
Other optional stuff
Get the configuration:
php artisan vendor:publish --tag=location
If you have a Google key add a line to your .env file:
GOOGLE_KEY=[key]
This script used to work out of the box without a key, but it doesn't anymore. Thanks Google. You can request one here Do make sure it has sufficiant rights.
These (quick examples):
$location = Location::locale('nl')->coordinatesToAddress(['latitude' => 52.385288, 'longitude' => 4.885361])->get(); $location = Location::locale('nl')->addressToCoordinates(['country' => 'Nederland', 'street' => 'Nieuwe Teertuinen', 'street_number' => 25])->get(); $location = Location::locale('nl')->postalcodeToCoordinates(['postal_code' => '1013 LV', 'street_number' => '25'])->coordinatesToAddress()->get(); $location = Location::locale('nl')->postalcodeToCoordinates(['postal_code' => '1013 LV', 'street_number' => '25'], true)->get(); $location = Location::locale('nl')->ipToCoordinates('46.44.160.221')->coordinatesToAddress()->get(); // if IP resolves properly, which it mostly doesn't $location = Location::locale('nl')->ipToCoordinates('46.44.160.221', true)->get(); // if IP resolves properly, which it mostly doesn't
Will all result in:
$location['latitude'] = 52.385288, $location['longitude'] = 4.885361; $location['iso'] = 'NL'; $location['country'] = 'Nederland'; $location['region'] = 'Noord-Holland'; $location['city'] = 'Amsterdam'; $location['street'] = 'Nieuwe Teertuinen'; $location['street_number'] = '25'; $location['postal_code'] = '1013 LV';
To return it as object set the get() function to true: get(true)
Limit results by country
To limit the search results to only be included when from a set of predefined countries, use the countries() function.
It accepts iso notation country names as defined by "ISO 3166-1 alpha-2".
Extended example:
try { $location = Location::coordinatesToAddress(['latitude' => 52.385288, 'longitude' => 4.885361])->get(true); if($error = Location::error()) { dd($error); } } catch(Exception $e) { dd($e->getMessage()); }
The result is the default template and starts out as empty and gets filled throughout the call. So if no data is available the result for that entry will be "". After every call the script resets to it's initial template.
Chainable functions and their variables
| Functions | Values | Validation | Type |
|---|---|---|---|
| countries() | iso's | required | array |
| coordinatesToAddress() | latitude | required | float |
| longitude | required | float | |
| addressToCoordinates() | country | recommended | string |
| region | string | ||
| city | required | string | |
| street | recommended | string | |
| street_number | recommended | string | |
| postalcodeToCoordinates() | postal_code | required | string |
| street_number | recommended | string | |
| get() | true/false | boolean | boolean |
Other functions
| Functions | Values | Result |
|---|---|---|
| error() | none | Returns any error if there is one |
| response() | none | Returns the raw response from the Google API |
Debug
With the try catch you can already see what you need. But besides this there is also a cached result of the raw response from the google API. Please note that this is not the case with the ip request.
$location = Location::coordinatesToAddress(['latitude' => 52.385288, 'longitude' => 4.885361])->get(); Location::response(); // results in raw api response
thorazine/location 适用场景与选型建议
thorazine/location 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 457 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 thorazine/location 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 thorazine/location 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 457
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-21