umbrella-web/sf2-google-geocoding-api-bundle

为您的 Symfony2 项目集成 Google 地理编码 API

此包的规范存储库似乎已丢失,因此该包已被冻结。

安装: 299

依赖项: 0

建议者: 0

安全: 0

星标: 3

关注者: 45

分支: 2

类型:symfony-bundle

1.0.0 2014-04-01 13:01 UTC

This package is not auto-updated.

Last update: 2024-01-20 12:37:16 UTC


README

SensioLabsInsight

提供与 Google 地理编码 API v3 的交互(有关更多信息,请参阅 https://developers.google.com/maps/documentation/geocoding/)。

UmbrellawebGoogleGeocodingApiBundle 允许通过 Google 地理编码 API 服务将地址(如“1600 Amphitheatre Parkway, Mountain View, CA”)转换为地理坐标(如纬度 37.423021 和经度 -122.083739)。(有关地理编码的更多信息,请参阅 https://developers.google.com/maps/documentation/geocoding/#Geocoding)。之后,您可以使用地理坐标放置标记或定位地图。

使用限制

当前 UmbrellawebGoogleGeocodingApiBundle 版本 1.0.0 不使用 API 密钥。免费 API 的用户限制为每 24 小时 2,500 个请求。有关 API 密钥的更多信息,请参阅 https://developers.google.com/maps/documentation/geocoding/#api_key

地理编码响应

默认情况下,UmbrellawebGoogleGeocodingApiBundle 使用 json 响应格式。有关响应结构的更多信息,请参阅 https://developers.google.com/maps/documentation/geocoding/#JSON

UmbrellawebGoogleGeocodingApiBundle 提供获取完整结果数组和方便的格式中分离的结果部分(如纬度和经度)。

安装

umbrella-web/sf2-google-geocoding-api-bundle 包添加到您的 composer.json 文件中的 require 部分。

"require": {
    // ...
    "umbrella-web/sf2-google-geocoding-api-bundle": "dev-master"
}

更新包

$ composer update umbrella-web/sf2-google-geocoding-api-bundle

将 Umbrella-web Google 地理编码 API 包添加到 app/AppKernel.php

<?php
public function registerBundles()
{
    $bundles = array(
        // ...
        new Umbrellaweb\Bundle\GoogleGeocodingApiBundle\UmbrellawebGoogleGeocodingApiBundle(),
        // ...
    );
    ...
}

使用

该包提供了一个可通过 umbrellaweb.google_geo_api.manager 标识符访问的服务。

要从容器中检索服务,请在您的控制器中使用以下代码

$geocodeManager = $this->get('umbrellaweb.google_geo_api.manager');

基本使用

使用示例

$geoResponse = $this->get('umbrellaweb.google_geo_api.manager')->geocodeAddress(array('address' => '1600 Amphitheatre Parkway, Mountain View, CA', 'sensor' => 'false'));

// checking if there was some errors
if (!$geoResponse->isSuccess()) {
    $error = $geoResponse->getErrorMessage();

    echo $error;
}

// checking if response is received but location was not found by some reason
if ($geoResponse->isSuccess() && !$geoResponse->isOkResponse()) {
    $warning = 'Sorry, but your Location not found: ' . $geoResponse->getStatus();

    echo $warning;
}

// retrieve Latitude and Longitude 
if ($geoResponse->getLongitude() !== NULL && $geoResponse->getLatitude() !== NULL) {
    $lat = $geoResponse->getLatitude();
    $lng = $geoResponse->getLongitude();

    echo 'Latitude: ' . $lat;
    echo 'Longitude: ' . $lng;
}

// or for example you want retrieve contry code
if ($geoResponse->getCountryCode() !== NULL) {
    $countryCode = $geoResponse->getCountryCode();

    echo 'Country Code: ' . $countryCode;
}

Google 服务条款

请尊重 Google 为使用地理编码 API 所指定的 服务条款(TOS)。