buuum / googleplaces
带有Google Places的简单PHP API类
Requires
- php: >=7.0.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-17 20:30:09 UTC
README
安装
系统要求
要使用Buuum\GooglePlaces,您需要PHP >= 7.0.0,但建议使用PHP的最新稳定版本。
Composer
Buuum\GooglePlaces可在Packagist上找到,并可以使用Composer安装。
composer require buuum/googleplaces
手动
只要它遵循PSR-0或PSR-4标准,您就可以使用自己的自动加载器。只需将src目录的内容放入您的vendor目录中。
文档
https://developers.google.com/places/web-service/autocomplete
初始化
$apikey = 'xxxxxx'; $places = new \Buuum\GooglePlaces($apikey);
设置器
setLang
语言代码,表示如果可能的话,结果应该返回的语言。
$places->setLang('es');
setType
要返回的地点结果类型。 https://developers.google.com/places/web-service/autocomplete#place_types
$places->setType('address');
setComponents
一组您想限制结果的地点。
$places->setComponents(['country' => 'es']);
setLocation
您希望检索地点信息的点。
$places->setLocation('12312,12312');
#### setRadius 返回地点结果的范围(米)
$places->setRadius(100);
获取城市
$places->setType('(cities)'); $results = $places->search('montg');
输出
{ "predictions" : [ { "description" : "Montgat, España", "id" : "597a021526a575ea9ea5012c1f67d7b905cb8dba", "matched_substrings" : [ { "length" : 5, "offset" : 0 } ], "place_id" : "ChIJH-WPJmi6pBIRurNpFRmVe5o", "reference" : "CjQoAAAAEDBWR-Jb9vvh9yIosK02pjOhnyaHaSo4eiAGheeYIS6ZNQSPH9OXY73ETn0Nen1uEhASpJinNCF9CL702trOFaGNGhSm-btgLPmDWRc8ZCcesK8uvD7-tw", "structured_formatting" : { "main_text" : "Montgat", "main_text_matched_substrings" : [ { "length" : 5, "offset" : 0 } ], "secondary_text" : "España" }, "terms" : [ { "offset" : 0, "value" : "Montgat" }, { "offset" : 9, "value" : "España" } ], "types" : [ "locality", "political", "geocode" ] }, { "description" : "Mongay, España", "id" : "4213b95fa271545896e764db4938cf59f641af23", "matched_substrings" : [ { "length" : 6, "offset" : 0 } ], "place_id" : "ChIJ6e1Yx0uMphIR9uvohdVGEV4", "reference" : "CjQnAAAAbJmD2YnEzpp8HsQrz-CgOWjAnlkeYvfpuyjIw3BhvFL237j8HXRxyL3FMu93cegLEhBQMxbk83QdmPFn42_Ji5CJGhS9CNuZnN1Pp6J3OwdFlLqhYgm0OA", "structured_formatting" : { "main_text" : "Mongay", "main_text_matched_substrings" : [ { "length" : 6, "offset" : 0 } ], "secondary_text" : "España" }, "terms" : [ { "offset" : 0, "value" : "Mongay" }, { "offset" : 8, "value" : "España" } ], "types" : [ "locality", "political", "geocode" ] }, { "description" : "Montgarri, España", "id" : "d9facf5996da05bf7931167114fda926cbfb3973", "matched_substrings" : [ { "length" : 5, "offset" : 0 } ], "place_id" : "ChIJTUDIi5S7qBIRlVuOHDhd13U", "reference" : "CjQqAAAAW5LphMEAuP9hQwJQt1AnZynRzL8j4VF3qsQ44u71hOHNLswiHWFvUWGvRlwy9BFdEhCfPs_BHBPT6y6tb4b7J-kvGhTuWpZaYPCXrPm064-DlpTkGNb5-g", "structured_formatting" : { "main_text" : "Montgarri", "main_text_matched_substrings" : [ { "length" : 5, "offset" : 0 } ], "secondary_text" : "España" }, "terms" : [ { "offset" : 0, "value" : "Montgarri" }, { "offset" : 11, "value" : "España" } ], "types" : [ "locality", "political", "geocode" ] }, { "description" : "Montgo, España", "id" : "c2f6f412dd40fdcfa8e2345eee06b20861bdead2", "matched_substrings" : [ { "length" : 5, "offset" : 0 } ], "place_id" : "ChIJ13UluxkFnhIRiFZWEE1MLMs", "reference" : "CjQnAAAA_RFOM3JVU5_PpsEVFHcy-RM5uFlX4e-ULh88VpFhU6nPeu9CjV34M0htNQv83mePEhDXURC_KzaScbArWrkUimfDGhT_rnC4H6pEUA57zE1-wB2u5pMusw", "structured_formatting" : { "main_text" : "Montgo", "main_text_matched_substrings" : [ { "length" : 5, "offset" : 0 } ], "secondary_text" : "España" }, "terms" : [ { "offset" : 0, "value" : "Montgo" }, { "offset" : 8, "value" : "España" } ], "types" : [ "locality", "political", "geocode" ] }, { "description" : "Montgo-Toscamar, España", "id" : "46ca12912abe864670aec353f6eccbff0036cd8a", "matched_substrings" : [ { "length" : 5, "offset" : 0 } ], "place_id" : "ChIJ9UAbmBcFnhIR402ie35iGrs", "reference" : "CjQwAAAAE6jtRqPErXEZq_8f1VO6aISaVt7OFzBfhDGI-bT8z1tgoVie0VwOxJKIhCTZoPkNEhA-KN6lKtNgHblF0Rf4agooGhRnXMhoomK3_t4fUuPS5xuyL0LN1A", "structured_formatting" : { "main_text" : "Montgo-Toscamar", "main_text_matched_substrings" : [ { "length" : 5, "offset" : 0 } ], "secondary_text" : "España" }, "terms" : [ { "offset" : 0, "value" : "Montgo-Toscamar" }, { "offset" : 17, "value" : "España" } ], "types" : [ "locality", "political", "geocode" ] } ], "status" : "OK" }
获取地址
$places->setType('address'); $results = $places->search('montgat sant antoni maria');
输出
{ "predictions" : [ { "description" : "Carrer Sant Antoni Maria Claret, Montgat, España", "id" : "5dd38d73703d66646ad66cef3f571d8ae1e989da", "matched_substrings" : [ { "length" : 17, "offset" : 7 }, { "length" : 7, "offset" : 33 } ], "place_id" : "EjFDYXJyZXIgU2FudCBBbnRvbmkgTWFyaWEgQ2xhcmV0LCBNb250Z2F0LCBFc3Bhw7Fh", "reference" : "CkQ1AAAATvJA5yBRgVQC7zSEn7pgR42qRWxeIMjXDEJhO8N3qYARB3ZLOSkMT2ZWVYE4yuraNa9r2-Xrf1gLSzZ12wBNJRIQhiXNP2U97PYezHZANlv2hRoUa-Cc4lg1HYKus_lgepImMY5Hy9g", "structured_formatting" : { "main_text" : "Carrer Sant Antoni Maria Claret", "main_text_matched_substrings" : [ { "length" : 17, "offset" : 7 } ], "secondary_text" : "Montgat, España", "secondary_text_matched_substrings" : [ { "length" : 7, "offset" : 0 } ] }, "terms" : [ { "offset" : 0, "value" : "Carrer Sant Antoni Maria Claret" }, { "offset" : 33, "value" : "Montgat" }, { "offset" : 42, "value" : "España" } ], "types" : [ "route", "geocode" ] } ], "status" : "OK" }
通过邮政编码获取
$places->setType('(regions)'); $results = $places->search('08390');
输出
{ "predictions" : [ { "description" : "08390 Tiana, España", "id" : "4cd7ea52f3c1b4737c51e84c45d9f93651262658", "matched_substrings" : [ { "length" : 5, "offset" : 0 } ], "place_id" : "ChIJJ8a0p0O6pBIRkFdHeuP6ABw", "reference" : "CjQsAAAAgIVFpb0lpRZxF73ZUwTQFGB-OVhhw-XuexvW8oXNK5wsQKykRV_rfMZgZng_tym9EhAmNokICj4iE2nzsFCWhbpRGhRUi0CdSaOLow1fxh23W3odlzVrPA", "structured_formatting" : { "main_text" : "08390", "main_text_matched_substrings" : [ { "length" : 5, "offset" : 0 } ], "secondary_text" : "Tiana, España" }, "terms" : [ { "offset" : 0, "value" : "08390" }, { "offset" : 6, "value" : "Tiana" }, { "offset" : 13, "value" : "España" } ], "types" : [ "postal_code", "geocode" ] } ], "status" : "OK" }
通过地点ID获取
$results = $places->getDetailsFromPlaceId('ChIJJ8a0p0O6pBIRkFdHeuP6ABw');
输出
{ "html_attributions" : [], "result" : { "address_components" : [ { "long_name" : "08390", "short_name" : "08390", "types" : [ "postal_code" ] }, { "long_name" : "Tiana", "short_name" : "Tiana", "types" : [ "locality", "political" ] }, { "long_name" : "Barcelona", "short_name" : "Barcelona", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Cataluña", "short_name" : "CT", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "España", "short_name" : "ES", "types" : [ "country", "political" ] } ], "adr_address" : "\\u003cspan class=\\"postal-code\\"\\u003e08390\\u003c/span\\u003e \\u003cspan class=\\"locality\\"\\u003eTiana\\u003c/span\\u003e, \\u003cspan class=\\"region\\"\\u003eBarcelona\\u003c/span\\u003e, \\u003cspan class=\\"country-name\\"\\u003eEspaña\\u003c/span\\u003e", "formatted_address" : "08390 Tiana, Barcelona, España", "geometry" : { "location" : { "lat" : 41.4712564, "lng" : 2.2802755 }, "viewport" : { "northeast" : { "lat" : 41.4871754, "lng" : 2.2967348 }, "southwest" : { "lat" : 41.4608014, "lng" : 2.2616708 } } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png", "id" : "4cd7ea52f3c1b4737c51e84c45d9f93651262658", "name" : "08390", "place_id" : "ChIJJ8a0p0O6pBIRkFdHeuP6ABw", "reference" : "CmRbAAAARuRmnc0cppqN5RZECEW5gfjVncPfFnrK1Ue787NHntbUXPYgFouCtQJbhfBrlkRyB4yCLUPdY-wYKTf6KbdkvorRHrbGRRS8wl_Crm7wDap1obUThKcH28sVNlipYQ39EhAwfKZEtKCF2n1Lt_anfKlRGhSTW9Djj7trm6ThKstkv3fmh46RNA", "scope" : "GOOGLE", "types" : [ "postal_code" ], "url" : "https://maps.google.com/?q=08390&ftid=0x12a4ba43a7b4c627:0x1c00fae37a475790", "utc_offset" : 60, "vicinity" : "Tiana" }, "status" : "OK" }
许可证
MIT许可证(MIT)
版权所有 (c) 2016
特此授予任何获得本软件及其相关文档副本(“软件”)的人免费使用权,不受任何限制地使用该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向获得软件的人提供软件,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的副本或主要部分中。
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论此类索赔、损害或其他责任是基于合同、侵权或其他原因,源自、因之产生或与此类软件或其使用或其它交易有关。