quexer69 / yii2-google-api
一组针对 Google 地图 API 的调用,包括 staticmap、geocode、iframe,适用于 Google API v3
2.0.3
2016-12-22 19:32 UTC
Requires
- yiisoft/yii2: 2.0.*
README
一组针对 Google 地图 API 的调用,包括 staticmap、geocode、iframe 地图
...适用于 Google API v3, PHP >= 5.4.0, Yii 2.0.*
安装
通过 composer 安装此组件是首选方法。
运行以下命令之一:
php composer.phar require --prefer-dist quexer69/yii2-google-api "2.0.*"
或者将以下内容添加到您的 composer.json
文件的 require 部分:
"quexer69/yii2-google-api": "2.0.*"
配置
在您的 app 和/或 控制台配置文件中添加以下内容:
'components' => [ // Google Maps Image and Geocode API settings for \Yii::$app->googleApi component 'googleApi' => [ 'class' => 'quexer\googleapi\GoogleApiLibrary', // API Keys !!! 'staticmap_api_key' => '***************************************', 'geocode_api_key' => '***************************************', // Set basePath 'webroot' => '@webroot', // Image path and map iframe settings 'map_image_path' => '/images/google_map', 'map_type' => 'terrain', 'map_size' => '520x350', 'map_sensor' => false, 'map_zoom' => 9, 'map_scale' => 1, 'map_marker_color' => 'red', 'map_iframe_width' => '100%', // %, px, em 'map_iframe_height' => '500px', // %, px, em 'map_language' => 'de', // Debug 'quiet' => false ], ... ],
公共方法
\Yii::$app->googleApi->renderMapIframe($address, $latlng, $iFrameWidth, $iFrameHeight) \Yii::$app->googleApi->createImage($address, $latlng, $setMarker) \Yii::$app->googleApi->getGeoCodeObject($address, $latlng) \Yii::$app->googleApi->getDistance($start, $finish, $unit) \Yii::$app->googleApi->getBounds($address)
用法
组件安装后,只需在您的代码中按以下方式使用它:
// Use $address OR $latlng $address = '70180 Stuttgart, Germany'; $latlng = '48.7632145,9.174027';
创建 Google 地图图像
$relFilePath = \Yii::$app->googleApi->createImage($address, null, true);
获取 Google 地码对象
$relFilePath = \Yii::$app->googleApi->getGeoCodeObject(null, $latlng);
渲染 Google 地图 iframe
$iframeMarkup = \Yii::$app->googleApi->renderMapIframe(null, $latlng);
计算两个地理点之间的距离
$latlng_origin = ['48.7632145','9.174027']; $latlng_destination = ['48.4525334','9.468254']; $unit = 'miles'; // 'miles' or 'km' $floatDistance = \Yii::$app->googleApi->getDistance($latlng_origin, $latlng_destination, $unit);
另请参阅
...更广泛的变化