PHPackages                             kriss/yii2-geo - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. kriss/yii2-geo

ActiveYii2-extension

kriss/yii2-geo
==============

Yii2 GEO

v1.0(5y ago)1110MITPHP

Since Mar 30Pushed 5y ago1 watchersCompare

[ Source](https://github.com/krissss/yii2-geo)[ Packagist](https://packagist.org/packages/kriss/yii2-geo)[ RSS](/packages/kriss-yii2-geo/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Yii2 Geo
========

[](#yii2-geo)

Yii2 Geo

Installation
------------

[](#installation)

```
composer require kriss/yii2-geo

```

Usage
-----

[](#usage)

1. 设置 geoClient，目前支持 `RedisGeoClient`

```
'components' => [
    // others
    'geoClient' => [
        'class' => 'kriss\geo\client\RedisGeoClient',
        //'redis' => 'redis', // 使用已有的 redis 组件
        'redis' => [
            'class' => 'yii\redis\Connection',
            'hostname' => '127.0.0.1',
            'database' => 0,
        ],
        //'prefix' => 'geo_', // key 前缀
    ]
    // others
],

```

2. 设置 ActiveRecord

假设有模型如下：

```
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]; // 替换成自己的经纬度字段
    }
}
```

3. 使用

自动 geo 信息更新

```
$model = new User();
$model->lnt = 116.306726;
$model->lat = 40.067816;
$model->save(); // 触发新增 geo 信息

$model->lnt = 117.306726;
$model->save(); // 触发更新 geo 信息

$model->name = 'aaa';
$model->save(); // 由于修改的字段为配置为触发，因此不会触发更新 geo 信息

$model->is_deleted = 1;
$model->save(); // 触发删除 geo 信息

$model->delete(); // 触发删除 geo 信息
```

获取 geo 相关信息

```
// 获取 GeoClient
$client = User::getGeoClient();
// 获取经纬度
$positions = $client->getPosition(1); // 有值时：[1 => ["116.30672782659531", "41.067817101970839"]]，无值时：[1 => []]
$positions = $client->getPosition(1, 2, 3); // 同时获取多个值
// 计算距离
$distance = $client->getDistance(33, 34); // "111226.0989"，单位默认为米
$distance = $client->getDistance(33, 34, 'km'); // "111.2261"，单位千米
// 获取范围内的
$members = $client->getMembersInRadius('116.427411','39.985579', 10, 'km'); // 10千米内的坐标：['34', '35']
$members = $client->getMembersInRadius('116.427411','39.985579', 10, 'km', ['sort' => 'ASC']); // 10千米内的坐标，并按照距离远近排序：['35', '34']
$members = $client->getMembersInRadius('116.427411','39.985579', 10, 'km', ['with' => 'DIST']); // 10千米内的坐标，并附带距离值：[['34' => '13.7595'], ['34' => '13.7591']]
$members = $client->getMembersInRadius('116.427411','39.985579', 10, 'km', ['count' => 1]); // 10千米内的坐标，最多返回1个：['34']
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1869d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b4053cfc9956b8c15b9bc4c9c7c2bad625d16d1b6d9191019b2a25cc0972d955?d=identicon)[kriss](/maintainers/kriss)

---

Top Contributors

[![krissss](https://avatars.githubusercontent.com/u/10680903?v=4)](https://github.com/krissss "krissss (1 commits)")

---

Tags

yii2extensiongeoredis geo

### Embed Badge

![Health badge](/badges/kriss-yii2-geo/health.svg)

```
[![Health](https://phpackages.com/badges/kriss-yii2-geo/health.svg)](https://phpackages.com/packages/kriss-yii2-geo)
```

###  Alternatives

[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13825.6k47](/packages/skeeks-cms)[sjaakp/yii2-spatial

Yii2 ActiveRecord supporting MySQL spatial data

1873.8k1](/packages/sjaakp-yii2-spatial)[kartik-v/yii2-ipinfo

An IP address information display widget for Yii 2.0 with country flag and geo position info.

2832.6k1](/packages/kartik-v-yii2-ipinfo)[dmstr/yii2-cookie-consent

Yii2 Cookie Consent Widget

1452.6k](/packages/dmstr-yii2-cookie-consent)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
