flaviovs/yii2-geocomplete

为Yii2设计的jQuery Geocomplete小部件

2.1.1 2019-01-17 19:42 UTC

This package is not auto-updated.

Last update: 2024-09-15 05:54:56 UTC


README

使用Geocomplete jQuery插件实现Yii2的geocomplete小部件。

用法

首先需要配置Google Maps API密钥,这可以通过在应用的配置中设置小部件的资产包中的mapsApiKey属性来完成。

'components' => [
    'assetManager' => [
        'bundles' => [
            fv\yii\geocomplete\Asset::class => [
                'mapsApiKey' => 'YOUR-API-KEY',
			]
		]
	]
];

您可以在资产包配置中使用sessionToken来指定要发送给地图API的会话令牌(详情见此处)。默认情况下,扩展将生成会话令牌 - 将此参数设置为false以防止生成。

注意 除非您提供有效的API密钥,否则小部件将无法正常工作。有关详细信息,请参阅https://developers.google.com/maps/documentation/javascript/get-api-key

然后您可以将geocomplete小部件添加到您的视图中的模型字段。

<?= $form->field($model, 'address')
         ->widget(\fv\yii\geocomplete\Widget::class) ?>

Yii2 Geocomplete配置jQuery插件在地址查询完成后查找data-geocomplete属性。这使得从地址中获取其他值变得容易。例如,要获取国家代码和邮政编码分别在不同的字段中,请使用以下代码

<?= $form->field($model, 'address')
     ->widget(\fv\yii\geocomplete\Widget::class) ?>

<?= $form->field($model,
                 'postal_code', [
                     'inputOptions' => [
                         'data-geocomplete' => 'postal_code',
                     ],
                 ]) ?>

<?= $form->field($model,
                 'country_code', [
    	             'inputOptions' => [
                         'data-geocomplete' => 'country_short',
                     ],
                 ]) ?>

小部件选项

Yii2 Geocomplete小部件接受以下选项

  • options - 标准标签属性

  • container - Yii2 Geocomplete应查找data-geocomplete-tagged元素的jQuery选择器。默认是父表单(从JavaScript获取)。

  • geocompleteOptions - 包含Geocomplete插件选项的对象。有关完整列表,请参阅https://ubilabs.github.io/geocomplete/

问题

访问:https://github.com/flaviovs/yii2-geocomplete