PHPackages                             sjaakp/yii2-locator - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. sjaakp/yii2-locator

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

sjaakp/yii2-locator
===================

Leaflet-wrapper for Yii2.x

1.0.5(1y ago)44.6k—0%[2 issues](https://github.com/sjaakp/yii2-locator/issues)MITPHPPHP &gt;=7.0.0CI failing

Since Jan 16Pushed 1y ago2 watchersCompare

[ Source](https://github.com/sjaakp/yii2-locator)[ Packagist](https://packagist.org/packages/sjaakp/yii2-locator)[ RSS](/packages/sjaakp-yii2-locator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (1)Versions (7)Used By (0)

Yii2-locator
------------

[](#yii2-locator)

#### Leaflet-wrapper for Yii2 PHP framework

[](#leaflet-wrapper-for-yii2-php-framework)

[![Latest Stable Version](https://camo.githubusercontent.com/df1969ce7e851aee6d403640d37a046db0a96ccbd96cf4243ff4497d72e22825/68747470733a2f2f706f7365722e707567782e6f72672f736a61616b702f796969322d6c6f6361746f722f762f737461626c65)](https://packagist.org/packages/sjaakp/yii2-locator)[![Total Downloads](https://camo.githubusercontent.com/3dcf79387b6e144ecda42e0f9e0afb196806477c0042194091d0cad346454c11/68747470733a2f2f706f7365722e707567782e6f72672f736a61616b702f796969322d6c6f6361746f722f646f776e6c6f616473)](https://packagist.org/packages/sjaakp/yii2-locator)[![License](https://camo.githubusercontent.com/c110342a7392309cb6b1d3342be249bf2c4dd496bf68394964ee79fa618dc5fc/68747470733a2f2f706f7365722e707567782e6f72672f736a61616b702f796969322d6c6f6361746f722f6c6963656e7365)](https://packagist.org/packages/sjaakp/yii2-locator)

This is a wrapper of the [Leaflet](https://leafletjs.com/) JavaScript geomapping library for the [Yii 2.0](https://yiiframework.com/ "Yii") PHP Framework. It's an Yii2 [Widget](https://www.yiiframework.com/doc/api/2.0/yii-base-widget) that can be used to display geographical data stored in an [ActiveRecord](https://www.yiiframework.com/doc/api/2.0/yii-db-activerecord), as well as to update it. **Yii2-locator** optionally has a search facility. It can use several providers, for the map tiles as well as for the geocoding service.

A demonstration of **yii2-locator** is [here](https://sjaakpriester.nl/software/locator).

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

[](#installation)

The preferred way to install **yii2-locator** is through [Composer](https://getcomposer.org/). Either add the following to the require section of your `composer.json` file:

`"sjaakp/yii2-locator": "*"`

Or run:

`composer require sjaakp/yii2-locator "*"`

You can manually install **yii2-locator** by [downloading the source in ZIP-format](https://github.com/sjaakp/yii2-locator/archive/master.zip).

### GeoJSON

[](#geojson)

**Yii2-locator** handles data in [GeoJSON format](https://geojson.org/). Some databases store these directly. Others, like MySQL and MariaDB, use their own format for spatial data. My [Yii2-spatial](https://github.com/sjaakp/yii2-spatial) extension can be used to transform [MySQL format](https://dev.mysql.com/doc/refman/5.5/en/spatial-datatypes.html) to GeoJSON and vice versa. In that case, the model should be extended from `sjaakp\spatial\ActiveRecord`in stead of the usual `yii\db\ActiveRecord`.

Usage
-----

[](#usage)

A typical usage scenario is like this: suppose we have a database table with some geographical data, let's say the table `tower`. If we use MySQL or MariaDB, the model `Tower` is extended like this:

```
class Tower extends sjaakp\spatial\ActiveRecord    {
    public static function tableName()
    {
        return 'tower';
    }
    // ...
}

```

The table `tower` has, among others, the following fields:

- `location`: `POINT` the location of the tower,
- `mapcenter`: `POINT` the center of the map,
- `mapzoom`: `int` the zoom level of the map.

#### View

[](#view)

In a yii2 *view*, displaying a map of a tower is simple as this:

```

...

...

...

```

Methods
-------

[](#methods)

- **tileLayer(*$data*)** - Add a tile to the map. `$data: string|array`: tile provider name, or name with options. See [Tile names](#tile-names). Return: `$this`.
- **center(*$lat, $lng* = null)** - Set the center of the map. `$lat` and `$lng`are the latitude and longitude, `float`. `$lat` can also be an array `[, ]`. Return: `$this`.
- **modelCenter(*$model, $attribute*)** - Set the center of the map to the value of `$attribute` in `$model`. This should be a [GeoJSON Feature](https://geojson.org/geojson-spec.html#feature-objects). Return: `$this`.
- **activeCenter(*$model, $attribute*)** - Create an [ActiveField](https://www.yiiframework.com/doc/api/2.0/yii-widgets-activefield)for the center of the map, coupled to the value (a GeoJSON Feature) of `$attribute` in `$model`. Return: `$this`.
- **zoom(*$z*)** - Set the zoom level of the map. `$z`: `integer`. Return: `$this`.
- **modelZoom(*$model, $attribute*)** - Set the zoom level of the map to the value of `$attribute` in `$model`. Return: `$this`.
- **activeZoom(*$model, $attribute*)** - Create an ActiveField for the zoom level of the map, coupled to the value of `$attribute` in `$model`. Return: `$this`.
- **feature(*$feature*)** - Add a [GeoJSON Feature](https://geojson.org/geojson-spec.html#feature-objects)to the map. Return: `$this`.
- **modelFeature(*$model, $attribute*)** - Add the value of `$attribute` in `$model` as a GeoJSON Feature to the map. Return: `$this`.
- **modelFeatures(*$dataProvider, $attribute*)** - Add multiple GeoJSON features to the map, provided by [ActiveDataProvider](https://www.yiiframework.com/doc/api/2.0/yii-data-activedataprovider)`$dataProvider`, using attribute `$attribute`. Return: `$this`.
- **marker(*$lat* = null, *$lng* = null, *$options* = \[ \])** - Add marker to the map. Return: `$this`.

    - If `$lat == null`: marker appears at the first click point on the map.
    - If `$lat` and `$lng` are `float`s: these are the latitude and longitude.
    - If `$lat` is an array: `[, ]`.

    `$options` Options for the marker:

    - `'type'`: the [type](#marker-types) of the marker. If not set: `'Marker'`.
    - Other options are passed to the marker's constructor.
- **modelMarker(*$model, $attribute, $options* = \[ \])** - Set the location of the marker to the value (a [GeoJSON Feature](https://geojson.org/geojson-spec.html#feature-objects)) of `$attribute` in `$model`. Return: `$this`.
- **activeMarker(*$model, $attribute, $options* = \[ \])** - Create an ActiveField for the marker location, coupled to the value of `$attribute` in `$model`. Return: `$this`.
- **geocoder(*$options*)** - Set the geocoder of the map. Return: `$this`.

    - `$options` is `string`: the \[name\](#geocoder names) of the geocoder provider.
    - `$options` is `array`: first item is name, rest are geocoder options.
- **finder(*$geocoder* = null, *$position* = 'topright')** - Add a [Search Control](https://sjaakpriester.nl/software/leaflet-search), using `$geocoder`, to the map, with specified [position](https://leafletjs.com/reference.html#control-position). Return: `$this`.
- **getVar()** - Get the name of the JavaScript variable assigned to the Leaflet map. For advanced uses.

**Locator** is an [Yii2 Widget](https://www.yiiframework.com/doc/api/2.0/yii-base-widget), so it inherits all of its methods.

#### Chainable

[](#chainable)

Most of **Locator**'s methods return `this`, so they are *chainable*. This means that the absolute minimum code to display a map in a view would be something like:

```

...

```

Properties
----------

[](#properties)

- **$height** `int|string|false` Height of the Locator element. If `int` in pixels, if `string` any other valid CSS-value. If `false`, the height is not set. Notice that in that case the height must be set with some other means, otherwise the map will have a height of zero, and be invisible. Default: `400`.
- **$tile** `string|array` Name or configuration of the first [tile layer](#tile-names). Default: `'OpenStreetMap'`.
- **$marker** `array` Type and options for the default [marker](#marker-types). Default: `[ 'type' => 'DotMarker' ]`.
- **$options** `array` HTML options of the map container. Use this to explicitly set the ID. Default: `[ ]` (empty array).
- **$leafletOptions** `array` JavaScript [options](https://leafletjs.com/reference.html#map-option) of the map. Default: `[ ]` (empty array).
- **$cluster** `null|true|array` Options for [MarkerClusterer](https://github.com/Leaflet/Leaflet.markercluster#options). If `null`: no clustering. If `true`: clustering with default options. Default: `null`.
- **$popup** `null|true|array` [Options](https://leafletjs.com/reference.html#popup-option) for popups. If `null`: no popups. If `true`: popups with default options. Default: `null`.
- **$scale** `null|int` Display a [Scale Control](https://leafletjs.com/reference.html#control-scale)on the map. Can be `null` (no Scale Control), `SCALE_METRIC`, `SCALE_IMPERIAL` or `SCALE_BOTH`. Default: `SCALE_METRIC`.
- **$urlTemplate** `string` URL template used when marker is clicked. If not set, nothing happens. If `$popup` is set, a popup is shown with contents from the resulting URL. Otherwise a jump is performed to the URL. `'{xxx}'` is replaced by the Marker option with the name `'xxx'`. Typical use: `$urlTemplate = 'view/{id}'`. Default: `null`.
- **$fly** `bool` Whether to use ['fly-animation'](https://leafletjs.com/reference.html#map-flyto)when a Marker is placed after find.
- **$tileNamespace** `string` Namespace of the `Tile*` classes, defining the tile layers. Use this to add your own tile layer.

**Locator** is an [Yii2 Widget](https://www.yiiframework.com/doc/api/2.0/yii-base-widget), so it inherits all of its properties.

Tile Names
----------

[](#tile-names)

**Locator** retrieves its map tiles from a tile provider or map provider. Tiles are identified by the name of the provider, or by an `array` with the name as the first item and options in the rest of the array. This value is used in the `$tile` property, and in the `tileLayer()` method. A map can have more than one tile layers, which makes sense if they are partly transparent, like the tiles from *OpenSeaMap*.

Some providers offer tiles in a few *variants*. They are indicated with a suffix to the provider name, seperated by a dot. For example: `'OpenStreetMap'` and `'OpenStreetMap.BlackAndWhite'`.

Commercial tile providers expect some sort of API key. This should be added to the options. Often, an API key can be obtained free of charge for small or non-commercial applications.

Out of the box, **Locator** supports several tile providers. They each have a PHP class file in the `src/tiles` directory. Currently, the following tile providers are supported (there may be more in the future):

NameVariantsRequired option[OpenStreetMap](https://www.openstreetmap.org/about)BlackAndWhite, HOTOpenMapSurferRoads, Hybrid, AdminBounds, ContourLines, Hillshade[OpenTopoMap](https://opentopomap.org/about#verwendung)[OpenSeaMap](https://openseamap.org)[Wikimedia](https://commons.wikimedia.org/wiki/Commons:Map_resources) (affiliated sites)[Carto](https://github.com/CartoDB/basemap-styles)Light, Dark, Voyager[Stamen](http://maps.stamen.com)Toner, TonerBackground, TonerLines, TonerLabels, TonerLite, Watercolor, Terrain, TerrainBackground, TerrainLabelsEsriWorld[Here](https://developer.here.com)lots (see TileHere.php)`[ 'apiKey' => '...' ]`[TomTom](https://developer.tomtom.com/maps-api)Basic, Hybrid, Labels`[ 'key' => '...' ]`[Kadaster](https://pdok-ngr.readthedocs.io/services.html#tile-map-service-tms) (Netherlands only)standaard, grijs, pastel, water[Amsterdam](https://map.data.amsterdam.nl/)light, zwIf `$tile` is not set, **Locator** uses tiles from *OpenStreetMap*.

Geocoder Names
--------------

[](#geocoder-names)

**Locator**'s Search functionality uses information from a *geocoding service*. The service is set by the first parameter of the `finder()` method. This can be a `string`which is the name of the geocoding service, or an `array` with the name as first item, followed by options.

Generally, there will be no options, apart from the API key some providers expect. Other options may be added.

Currently, **Locator**supports the following providers (there may be more in the future):

NameRequired option[Nominatim](https://nominatim.org), by [OpenStreetMap](https://www.openstreetmap.org/about)[GeoNames](https://geonames.org)`[ 'username' => '...' ]`[Here](https://developer.here.com/documentation/authentication/dev_guide/index.html)`[ 'apiKey' => '...' ]`[TomTom](https://developer.tomtom.com/search-api/search-api-documentation/)`[ 'key' => '...' ]`[Kadaster](https://github.com/PDOK/locatieserver/wiki/API-Locatieserver) (Netherlands only)Notice that some providers may stipulate that you should use their service only on map tiles of the same provider.

If you don't explicitly set a geocoder, **Leaflet-search** uses *Nominatim*.

Marker Types
------------

[](#marker-types)

In the property `$marker`, and in methods like `marker()`, `modelMarker()` etc. the marker type can be set. This is an `array` with `[ 'type' => '' ]`, supplemented with marker options (dependent on the type). For instance:

```
$map->marker = [ 'type' => 'Marker', 'opacity' => 0.5 ]

```

Apart from **Leaflet**'s own [Marker](https://leafletjs.com/reference.html#marker) and [CircleMarker](https://leafletjs.com/reference.html#circlemarker), **Locator** sports two other markers:

#### DotMarker

[](#dotmarker)

A simple extension of [CircleMarker](https://leafletjs.com/reference.html#circlemarker). It has fixed radius and always has (at least) the class name `'dot-marker'`. The default marker of **Locator** is a DotMarker.

#### SpriteMarker

[](#spritemarker)

A marker with a [DivIcon](https://leafletjs.com/reference.html#divicon). Use this to display [FontAwesome](https://fontawesome.com) markers like so:

```
$map->marker = [ 'type' => 'SpriteMarker', 'html' => '' ]

```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance22

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity56

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

Every ~324 days

Recently: every ~404 days

Total

6

Last Release

692d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/156f636aceedc73021a2bfafb5433271c2c55d6cf4bce06b12902c85b4b491f1?d=identicon)[sjaakp](/maintainers/sjaakp)

---

Top Contributors

[![sjaakp](https://avatars.githubusercontent.com/u/5585878?v=4)](https://github.com/sjaakp "sjaakp (14 commits)")

---

Tags

mapyii2extensionwidgetyiigeoleaflet

### Embed Badge

![Health badge](/badges/sjaakp-yii2-locator/health.svg)

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

PHPackages © 2026

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