creativefactoryrv/google-maps-geocoder
最新稳定版本:2.1.3
Composer 安装命令:
composer require creativefactoryrv/google-maps-geocoder
包简介
Simple and lightweight geocoder that uses the Google Maps Platform API. It can optionally use a PSR cache.
README 文档
README
A super simple geocoding class that use Google Maps Platform to do the magic.
You only need to obtain a Google Geocoding API key (https://developers.google.com/maps).
Optionally, the class can leverage a PRS-6 cache implementation in order to query the Maps Platform only when necessary.
Install
composer require creativefactoryrv/google-maps-geocoder
How to use
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); $latLng = $location->getCoordinates(); echo 'Lat: ' . $latLng['lat'] . ' / Lng: ' . $latLng['lng']; echo 'ZIP code: ' . $location->getPostalCode();
Get a JSON
Return the Google Maps Platform API response as it is (JSON).
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); $myJSONString = $location->getRaw();
Get an array
Return the Google Maps Platform API response as an associative array.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); $myArray = $location->toArray();
Get latitude and longitude
Returns an array with the desired values.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); $myArray = $location->getCoordinates();
Country
Returns the country value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getCountry();
Locality (city)
Returns the city or locality value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getLocality();
Postal code
Returns the postal code or zipcode value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getPostalCode();
Route (street name)
Returns the street name (called "route" by Google).
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getRoute();
Street number
Returns the street number value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getStreetNumber();
Administrative levels 1
Returns the administrative levels 1 value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getAdministrativeAreaLevel1();
Administrative levels 2
Returns the administrative levels 2 value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getAdministrativeAreaLevel2();
Administrative levels 3
Returns the administrative levels 3 value.
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getAdministrativeAreaLevel3();
Formatted address
Returns a string representing the address (called "formatted_address" by Google).
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); echo $location->getFormattedAddress();
Format as you like
Returns a formatted address according to $format.
- %A1 Administrative Area Level 1
- %a1 Administrative Area Level 1 (short version)
- %A2 Administrative Area Level 2
- %a2 Administrative Area Level 2 (short version)
- %A3 Administrative Area Level 3
- %a3 Administrative Area Level 3 (short version)
- %C Country
- %L Locality
- %P Postal code
- %R Route
- %N Street number
$geocoder = new Geocoder('XXXXXXXXX-My-Google-Geocoding-API-XXXXXXXXX', 'en'); $location = $geocoder->query('Duomo, Milano, Italy'); $format = '%R %N, %C'; echo $location->format($format);
License
MIT
统计信息
- 总下载量: 30
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-09-28