imphinite / gaode-maps
这是一个用于高德Web服务API的PHP包
Requires
- illuminate/support: 5.*
This package is not auto-updated.
Last update: 2024-09-29 04:20:23 UTC
README
开发中
为Laravel应用程序提供方便的设置和向高德Web服务API发起请求的方式。
有关服务文档、API密钥和使用限制,请访问高德Web服务API和高德Web服务API使用限制和限制。
**请注意,此包处于开发阶段。大多数功能尚未实现。请随时为此项目合作!
**特别感谢Alexpechkarev。Web服务引擎借鉴自Alexpechkarev/google-maps。
功能
功能待办事项列表
依赖
安装
在控制台执行以下命令
composer require imphinite/gaode-maps
或者编辑composer.json文件,添加以下行并运行 composer update
"require": { ...., "imphinite/gaode-maps", },
配置
在'config/app.php'中注册包服务提供者和外观
'providers' => [ ... 'GaodeMaps\ServiceProvider\GaodeMapsServiceProvider', ] 'aliases' => [ ... 'GaodeMaps' => 'GaodeMaps\Facade\GaodeMapsFacade', ]
使用 php artisan vendor:publish --tag=gaodemaps --force
发布配置文件,或者简单地复制包配置文件并将其粘贴到 config/gaodemaps.php
打开配置文件 config/gaodemaps.php
并添加您的服务密钥
/* |---------------------------------- | Service Keys |------------------------------------ */ 'key' => 'YOUR GAODE API KEY HERE',
如果您希望为任何服务使用不同的密钥,您可以通过在所选Web服务的service
数组中指定它来覆盖主API密钥。
用法
以下是一个向地点搜索API发起请求的示例
$service = GaodeMaps::load('nearbysearch') ->setParam([ 'location' => '120.392164,36.056936', // Longitude first in Chinese convention 'keywords' => '餐厅', 'radius' => 5000, 'page' => 1, 'extensions' => 'all', 'output' => 'json' ]); $response = $service->get(); ...
或者可以使用setParamByKey()
方法设置参数。对于深层嵌套数组,请按以下示例使用“点”表示法。
$endpoint = GaodeMaps::load('nearbysearch') ->setParamByKey('location', '120.392164,36.056936') ->setParamByKey('keywords', '餐厅') //return $this ...
另一个示例,显示当请求多个地点详细信息时的批量请求服务
$batch_urls = array(); array_push($batch_urls, (object) array( 'url' => GaodeMaps::load('placedetails') ->setParam(['id' => $place->id) ->getBatchUrl() ) ); $service = GaodeMaps::load('batchrequest') ->setParam([ 'ops' => $batch_urls ]); $response = $batch_service->get(); ...
可用方法
load( $serviceName )
- 通过名称加载Web服务
接受字符串作为参数,Web服务名称与配置文件中指定的一致。
返回对其自身的引用。
GaodeMaps::load('nearbysearch') ...
setParamByKey( $key, $value )
- 使用键值对设置请求参数
接受两个参数
key
- 主体参数名称value
- 主体参数值
深层嵌套数组可以使用“点”表示法来分配值。
返回对其自身的引用。
$service = GaodeMaps::load('nearbysearch') ->setParamByKey('location', '120.392164,36.056936') ->setParamByKey('keywords', '餐厅') //return $this ...
setParam( $parameters )
- 一次性设置所有请求参数
接受参数数组
返回对其自身的引用。
$service = GaodeMaps::load('nearbysearch') ->setParam([ 'location' => '120.392164,36.056936', // Longitude first in Chinese convension 'keywords' => '餐厅', 'radius' => 5000, 'page' => 1, 'extensions' => 'all', 'output' => 'json' ]); // return $this ...
返回此服务的批量请求URL。
$url = GaodeMaps::load('nearbysearch') ->setParam([ 'location' => '120.392164,36.056936', // Longitude first in Chinese convension 'keywords' => '餐厅', 'radius' => 5000, 'page' => 1, 'extensions' => 'all', 'output' => 'json' ])->getBatchUrl(); ...
get()
- 执行Web服务请求(无论请求类型是POST还是GET)
返回由 setEndpoint()
方法指定的格式的网络服务响应,如果省略则默认为 JSON
。使用 json_decode()
将 JSON 字符串转换为 PHP 变量。有关解析返回输出的更多详细信息,请参阅 处理响应。
$response = GaodeMaps::load('nearbysearch') ->setParam([ 'location' => '120.392164,36.056936', // Longitude first in Chinese convension 'keywords' => '餐厅', 'radius' => 5000, 'page' => 1, 'extensions' => 'all', 'output' => 'json' ])->get(); var_dump(json_decode($response)); // output ... /* { "status": "1", "count": "274", "info": "OK", "infocode": "10000", "suggestion": { "keywords": [], "cities": [] }, "pois": [ "0": { "id": "B0FFFF4RX1", "tag": "牛道红花牛三品,菌类拼盘,新快猪上五花,牛舌厚切,石锅拌饭,红花三拼,红花牛芝士盖饭,烤蘑菇,红花牛肉,猪雪花肩胛肉,牛舌薄切,生拌牛肉,迷你现压朝鲜冷面,炒乌冬面,牛肩胛肉,酱香牛腿芯,海鲜饼,牛肋脊,泡菜饼,烤牛肉,牛仔骨,海鲜乌冬面,红花牛特色三样,烤五花肉,极品一口牛排", "name": "新快牛道红花牛馆(百丽广场店)", "type": "餐饮服务;中餐厅;特色/地方风味餐厅", ... */
MIT 许可证
Gaode 网络服务 API 的 Laravel 5 集合在 MIT 许可证 下发布。