kossmoss / yii2-google-maps-api
方便地从Yii2应用程序中访问Google API功能
1.0.1
2016-03-21 14:26 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: >=2.0.6
This package is not auto-updated.
Last update: 2024-09-13 15:19:21 UTC
README
此扩展的主要目的是从Yii2应用程序中通过Google Maps API查找地址坐标。它还提供资产,以便在视图中使用Google API JavaScript库。
安装
将依赖项添加到项目的composer.json文件中
{
"require": {
"kossmoss/yii2-google-maps-api": "^0.2"
}
}
用法
此扩展允许在视图中使用Google Maps API JavaScript库。只有当您正确配置了资产参数时,GoogleMapsAsset才会将库附加到视图。
'components' => [ ... 'assetManager' => [ 'bundles' => [ 'kossmoss\GoogleMaps\GoogleMapsAsset' => [ 'apiKey' => 'YOUR_API_KEY_HERE', // get at https://developers.google.com/maps/documentation/javascript/get-api-key 'language' => 'ru', // use language code supported by Google Maps API (default: en) ], ], ], ... ],
然后,在视图中注册资产,您就可以使用Google Maps API JS库了。
<?php /* @var $this yii\web\View */ \kossmoss\GoogleMaps\GoogleMapsAsset::register($this); ?> <div id="map" style="width: 400px; height: 300px;"></div> <?php $this->registerJs(' var map; initMap(); function initMap() { map = new google.maps.Map(document.getElementById("map"), { center: {lat: -34.397, lng: 150.644}, zoom: 8 }); } ');
此扩展还包括来自https://github.com/streetlogics/php-google-map-api的php-google-map-api库。自从它被构建以来已经过去了很多年,暂时有很多东西需要重构,但就目前而言,也许您会发现它对Google MAP API的同步调用很有用。
use kossmoss\GoogleMaps\GoogleMaps; ... $coords = GoogleMaps::coordsByAddress("Lisboa, Portugal");
如果您想使用原始扩展的其他功能,您需要使用GoogleMapAPI类而不是GoogleMaps类。您可以在这里找到关于GoogleMapsAPI类的部分文档。