1. Go to this page and download the library: Download kriss/yii2-geo library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kriss / yii2-geo example snippets
use yii\db\ActiveRecord;
class User extends ActiveRecord
{
}
use yii\db\ActiveRecord;
use kriss\geo\activeRecord\ActiveRecordGeoInterface;
use kriss\geo\activeRecord\ActiveRecordGeoTrait;
class User extends ActiveRecord implements ActiveRecordGeoInterface
{
use ActiveRecordGeoTrait;
public function behaviors()
{
$behaviors = parent::behaviors();
// 自动更新经纬度数据
$behaviors['geo'] = [
'class' => GeoUpdateBehavior::class,
'updateTriggerAttributes' => ['lng', 'lat'], // 触发更新的字段
'deleteAttributes' => ['is_deleted' => true], // 删除的字段和值
];
return $behaviors;
}
/**
* @inheritDoc
*/
public function getGeoLngLat()
{
return [$this->lng, $this->lat]; // 替换成自己的经纬度字段
}
}