当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

google-places-api

授权协议 MIT License
开发语言 PHP
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 缑永年
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Google Places API.

This is a PHP wrapper for Google Places API Web Service. And is Laravel Framework friendly.

About Package

With just 2 lines of code you can request to any google places api feature. No need to manually perform any curl requests.

The following place requests are available:

  • Place Search This service gives a list of places based on a user's location or search string.
  • Place Details This service gives more detailed information about a specific Place, including user reviews.
  • Place Autocomplete This service is Used to automatically fill in the name and/or address of a place as you type.
  • Query Autocomplete This service is Used to provide a query prediction service for text-based geographic searches, by returning suggested queries as you type.
  • Place Photo This gives you access to the millions of photos stored in the Google's Places database
  • Custom Headers Set Custom Headers.
  • Additional Methods Additional Methods Available.

Installation

Install it with composer

composer require skagarwal/google-places-api

Usage

Laravel user can see the Laravel Usage section

Step 1 - Import the class using namespace

use SKAgarwal\GoogleApi\PlacesApi;

Step 2 - Initiate the object

$googlePlaces = new PlacesApi('API KEY');

Note: You can also set the API KEY after initiating the class using setKey('KEY') method. You can chain this with method with any other methods.

Step 3 - Start Using the Api.

Example:

$response = $googlePlaces->placeAutocomplete('some Place');

As mentioned earlier just 2 lines of code to make any request.

Full example:

use SKAgarwal\GoogleApi\PlacesApi;


function () {
  $googlePlaces = new PlacesApi('API_KEY') # line 1
  $response = $googlePlaces->placeAutocomplete('some input'); # line 2
}

Use with Laravel

For Laravel 5.5

Auto Discovery added.

For Laravel 5.4 and below

Step 1

Set up the service provider and facade in the config\app.php

'providers' => [
....
....
SKAgarwal\GoogleApi\ServiceProvider::class,
];

'aliases' => [
....
....
'GooglePlaces' => SKAgarwal\GoogleApi\Facade::class,
];

Step 2

publish the config file with following artisan command

php artisan vendor:publish --provider="SKAgarwal\GoogleApi\ServiceProvider"

This will create google.php file in the config directory.

Set the API KEY in this config file.

Set 3

Start using the package using Facade.

$response = GooglePlaces::placeAutocomplete('some city');

Response

The response returned is a Laravel's Collection so that you can perform any of the available collection methods on it.

If you are not familiar with Laravel's Collection you can either reference the docs here or you can use response as simple array.

Available Methods

Place Search

nearbySearch($location, $radius = null, $params = [])

  • location — The latitude/longitude around which to retrieve place information. This must be specified as latitude, longitude.
  • 'radius' — Defines the distance (in meters) within which to return place results. The maximum allowed radius is 50 000 meters. Note that radius must not be included if rankby=distance (described under Optional parameters below) is specified.
  • If rankby=distance (described under Optional parameters below) is specified, then one or more of keyword, name, or types is required.
  • params - Optional Parameters You can refer all the available optional parameters on the Google's Official Webpage

textSearch($query, $params = [])

  • query — The text string on which to search, for example: "restaurant". The Google Places service will return candidate matches based on this string and order the results based on their perceived relevance.
  • params - Optional Parameters You can refer all the available optional parameters on the Google's Official Webpage

findPlace($input, $inputType, $params = [])

  • input — The text input specifying which place to search for (for example, a name, address, or phone number).
  • inputType — The type of input. This can be one of either textquery or phonenumber. Phone numbers must be in international format (prefixed by a plus sign ("+"), followed by the country code, then the phone number itself).
  • params - Optional Parameters You can refer all the available optional parameters on the Google's Official Webpage

Place Details

placeDetails($placeId, $params = [])

  • placeId — A textual identifier that uniquely identifies a place, returned from a Place Search.
  • params - Optional Parameters You can refer all the available optional parameters on the Google's Official Webpage

Place Autocomplete

placeAutocomplete($input, $params = [])

  • input — The text string on which to search. The Place Autocomplete service will return candidate matches based on this string and order results based on their perceived relevance.
  • params - Optional Parameters You can refer all the available optional parameters on the Google's Official Webpage

Query Autocomplete

queryAutocomplete($input, $params = [])

  • input — The text string on which to search. The Places service will return candidate matches based on this string and order results based on their perceived relevance.
  • params - Optional Parameters You can refer all the available optional parameters on the Google's Official Webpage

Place Photo

photo($photoReference, $params = [])


Custom Headers

withHeaders(array $headers)

Call This method before any other methods to set the headers. You can chain this method.

new PlacesApi($key = null, $verifySSL = true, array $headers = [])

To have custom headers set for every call, you can pass 3rd parameter as the headers to class constructor.

Note: For Laravel Users, you can set this in config file with key headers


Additional Methods

getStatus()

This will return the status of the response send by google api. Use it after making any request.

getKey()

This will return the API KEY been used with the requests.

setKey($key)

This will set the API KEY.

verifySSL($verifySSL = true)

You can pass false to disable Verification of SSL Certification.

Note: For Laravel Users, you can set this in config file with key verify_ssl

Or You can Pass the path to the certificate.

Exceptions

Google Places API may throw various exceptions based on the given $params or response and is located in the SKAgarwal\GoogleApi\Exceptions namespace.

  • A GooglePlacesApiException is thrown when no API KEY is provided or $params is invalid.Note: This is the parent class for the preceding exceptions.
  • A InvalidRequestException is thrown when the response status is INVALID_REQUEST
  • A OverQueryLimitException is thrown when the response status is OVER_QUERY_LIMIT
  • A RequestDeniedException is thrown when the response status is REQUEST_DENIED
  • A UnknownErrorException is thrown when the response status is UNKNOWN_ERROR
  • A NotImplementedException is thrown when the response cannot be determined.

If any of these exception has been thrown, you can use the getErrorMessage() method to get the error_message field from the response if any is provided.Note: error_message field is not guaranteed to be always present, and its content is subject to change.

Contribution

Feel free to report issues or make Pull Requests.If you find this document can be improved in any way, please feel free to open an issue for it.

License

The Google Places Api is open-sourced software licensed under the MIT license

 相关资料
  • 我一直试图在Google Places API中找到特定的位置,但没有成功。当我在谷歌地图中键入“la Cucina trattoria”时,我会得到三个结果。但是,当我运行此查询时: https://maps.googleapis.com/maps/api/place/autocomplete/json?key=api_key&input=la%20cucina%20trattoria 我只得到

  • 问题内容: Angular的新手,它尝试使用Google Maps / Places API设置一个非常简单的应用程序。 我可以通过以下方式成功使用地理位置服务: 问题: 当我尝试对他们的Places API进行类似的GET请求时-我收到CORS错误: 问题: 我是否可以从Angular发出这些类型的请求,还是需要设置后端来进行该调用? 我尝试使用JSONP作为方法,该方法通过了CORS问题,但随

  • 我正在试用google places api。 我可以搜索酒店或我想要的任何东西,并可以将这些位置保存在我的数据库中,包含google places API提供的任何信息。我的计划是,任何用户都可以保存其实际位置。在下一步中,用户应该看到,其他用户都在附近。 我怎么能这样做?有什么方法可以用谷歌地图的经纬度计算吗? 希望你能理解我的好英语: D

  • Places 是在 Lumia SensorCore SDK 上使用 Place Monitor API 接口的示例。这个应用可以在地图上显示所有已知的地方,还能将不确定的地点画个圈标记出来。

  • 这里我有一个问题,我无法访问android中的google places api服务来查找附近的地方。 我读过很多教程,但没有一个清楚地提到如何制作浏览器键或Android键? 这些我都试过了。 1)制作一个浏览器密钥并传入此查询https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151

  • 问题内容: 我正在使用Android Google Places API自动完成街道和地址。问题是它覆盖了整个国家的所有街道。当然,我添加了边界来限制搜索的位置,但是它无法正常工作- 它仅给出优先级,因此换句话说,最佳结果在列表中会更高,仅此而已 所以代码: 简而言之,问题是:当我输入“ Lenina Street”之类的搜索内容时,我看到了很多无用的结果。试想一下,几乎 每个 地方 都 存在类似