jboilesen/googleplace

Google Places API 库

1.1 2018-01-06 05:39 UTC

This package is auto-updated.

Last update: 2024-09-16 08:52:05 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\Service\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

文本搜索

文本搜索服务是一种基于字符串(例如“纽约的披萨”)返回一组地点信息的Web服务。

  $textSearch = new \GooglePlace\Services\TextSearch([
  'query' => 'Restaurants in Mirpur'
  ]);
   $places = $textSearch->places(); //same as nearby

地点详情

地点详情请求返回关于指定地点的更全面的信息,例如其完整地址、电话号码、用户评分和评论。您需要传递place_id或从地点搜索中引用。

$place=new \GooglePlace\Service\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());