ipowerful / googleplace
Google Places API 库
1.3
2021-08-18 12:58 UTC
Requires
- guzzlehttp/guzzle: ^6.3|^7.0
- illuminate/collections: ^8.0
Suggests
- tightenco/collect: Must be included if using outside laravel
This package is not auto-updated.
Last update: 2024-09-27 01:38:53 UTC
README
位置感知的Web应用程序大量使用google-places-api(地点搜索、地点详情、地理编码、距离矩阵、时区、海拔),它们之间没有关联,也难以找到一个简单且节省时间的PHP库来与这些API一起工作。这些库将需要较少的关于Google API的知识,并给你OOP的感觉。
设置
在您的composer.json文件中添加此行
"require":{ "digitaldream/googleplace":"1.*" }
您需要在页面的顶部设置您的Google API密钥。例如,如下所示
\GooglePlace\Request::$api_key = 'YOUR_GOOGLE_PLACES_API_KEY';
附近搜索
附近搜索允许您在指定区域内搜索地点。例如,您可以搜索您所在城市的所有餐馆。
$rankBy = new \GooglePlace\Services\Nearby([ 'location' => '23.823168,90.367728', 'rankby' => 'distance', 'type' => 'bank' ] ); $rankBy->places(); // it will return \Collection each contains a object of GooglePlace\Services\Place /* Google Return 60 places divide by 20 each request. To get next 20 result you have to call nextPage method. */ print_r($rankBy->nextPage()); // it will return \GooglePlace\Response
文本搜索
文本搜索服务是一个基于字符串(例如“纽约的披萨”)返回一组地点信息的网络服务。
$textSearch = new \GooglePlace\Services\TextSearch([ 'query' => 'Restaurants in Mirpur' ]); $places = $textSearch->places(); //same as nearby
地点详情
地点详情请求返回有关指定地点的更详细信息,例如其完整地址、电话号码、用户评分和评论。您需要传递place_id或地点搜索中的引用。
$place=new \GooglePlace\Services\Place([ 'placeid'=>'any place id' ]); $place->get(); echo $place->address(); echo $place->phone(); print_r($place->photos()); // returns Collection each contains a GooglePlace\Helpers\PlacePhoto object print_r($place->reviews()); // return Collection print_r($place->timezone(true)); // return Timezone API response print_r($place->distance($place2)); // return Distance Matrix API response print_r($place->elevation()); // return Elevation API response
地理编码
您可以通过地点名称或经纬度来获取地点。
$geocoding = new \GooglePlace\Services\Geocoding([ 'address' => 'House 13,Road 10,Section 11,Mirpur,Dhaka' ]); print_r($geocoding->places()); $reverseGeocoding= new \GooglePlace\Services\Geocoding([ 'address' => 'latlng' => '23.8163589,90.3709893' ]); print_r($reverseGeocoding->places()); //same as nearby
距离矩阵API
距离矩阵API是一个服务,它基于起点和终点之间的推荐路线,为来源和目的地的矩阵提供旅行距离和时间。
$distanceMatrix = new \GooglePlace\Services\DistanceMatrix([ 'origins' => ['Dhaka University, Dhaka'], 'destinations' => ['National University of Bangldesh, Gazipur']]); print_r($distanceMatrix->calculate());
时区API
时区API提供地球表面位置的时偏数据。API返回该时区的名称、UTC时差和夏令时偏移。
$timezone= new \GooglePlace\Services\Timezone([ 'location'=>'23.8163589,90.3709893', timestamp=time() ]) $response=$timezone->get(); echo $response->timeZoneId // return Asia/Dhaka
海拔API
获取指定地点的海拔(从海平面高度以米为单位)。
$elevation =new \GooglePlace\Services\Elevation([ 'locations'=>'23.8163589,90.3709893' ]); print_r($elevation->get());