dev-master
9999999-devYii2 map widget
MIT
The Requires
extension yii2 geo yii widget map
Yii2 map widget
This is a wrapper of the Leaflet JavaScript geomapping library for the Yii 2.0 PHP Framework. It's an Yii2 Widget that can be used to display geographical data stored in an 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., (*2)
A demonstration of yii2-locator is here., (*3)
The preferred way to install yii2-locator is through Composer.
Either add the following to the require section of your composer.json
file:, (*4)
"sjaakp/yii2-locator": "*"
, (*5)
Or run:, (*6)
composer require sjaakp/yii2-locator "*"
, (*7)
You can manually install yii2-locator by downloading the source in ZIP-format., (*8)
Yii2-locator handles data in GeoJSON format. Some databases
store these directly. Others, like MySQL and MariaDB, use their own format for spatial data.
My Yii2-spatial extension can be used to
transform MySQL format 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
., (*9)
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:, (*10)
class Tower extends sjaakp\spatial\ActiveRecord { public static function tableName() { return 'tower'; } // ... }
The table tower
has, among others, the following fields:, (*11)
location
: POINT
the location of the tower,mapcenter
: POINT
the center of the map,mapzoom
: int
the zoom level of the map.In a yii2 view, displaying a map of a tower is simple as this:, (*12)
<?php use sjaakp\locator\Locator; /** * @var app\models\Tower $model */ ?> ... <?php $map = Locator::begin([ 'height' => 480, // ... other options ... ]); $map->modelCenter($model, 'mapcenter'); // set the map's center $map->modelZoom($model, 'mapzoom'); // set the map's zoom level $map->modelFeature($model, 'location'); // place a marker at the tower's location Locator::end(); ?> ...
Displaying a map with all the towers in, say, the index view, can be accomplished with:, (*13)
<?php use sjaakp\locator\Locator; /** * @var yii\data\ActiveDataProvider $dataProvider */ ?> ... <?php $map = Locator::begin([ 'leafletOptions' => [ 'center' => [48.8, 2.3], // Paris 'zoom' => 5, // ... more options ... ], ]); $map->modelFeatures($dataProvider, 'location'); // provide the tower locations Locator::end(); ?> ...
In a create or update view, Locator can be used in a form:, (*14)
<?php use yii\widgets\ActiveForm; use sjaakp\locator\Locator; /** * @var app\models\Tower $model */ ?> ... <?php $form = ActiveForm::begin(); ?> ... <?php $map = Locator::begin([ // ... Locator options ... ]); $map->activeCenter($model, 'mapcenter'); // allow the map's center to be changed $map->activeZoom($model, 'mapzoom'); // allow the map's zoom level to be changed $map->activeMarker($model, 'location'); // allow the model's location to be changed $map->finder(); // add an interactive Search control to the map Locator::end(); ?> ... <?php ActiveForm::end(); ?> ...
$data: string|array
: tile provider name,
or name with options. See Tile names. Return: $this
.$lat
and $lng
are the latitude and longitude, float
. $lat
can also be an array [<lat>, <lng>]
.
Return: $this
.$attribute
in $model
. This should be a
GeoJSON Feature.
Return: $this
.$attribute
in $model
. Return: $this
.$z
: integer
. Return: $this
.$attribute
in $model
. Return: $this
.$attribute
in $model
. Return: $this
.$this
.$attribute
in $model
as a GeoJSON Feature to the map. Return: $this
.$dataProvider
, using attribute $attribute
. Return: $this
.marker($lat = null, $lng = null, $options = [ ]) - Add marker to the map. Return: $this
., (*15)
$lat == null
: marker appears at the first click point on the map.$lat
and $lng
are float
s: these are the latitude and longitude.$lat
is an array: [<latitude>, <longitude>]
.$options
Options for the marker:, (*16)
'type'
: the type of the marker. If not set: 'Marker'
.$attribute
in $model
. Return: $this
.$attribute
in $model
. Return: $this
.geocoder($options) - Set the geocoder of the map. Return: $this
., (*17)
$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, using $geocoder
,
to the map, with specified position.
Return: $this
., (*18)
Locator is an Yii2 Widget, so it inherits all of its methods., (*19)
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:, (*20)
<?php use sjaakp\locator\Locator; ... <?php Locator::begin([ // ... options ... ])->modelCenter($model, 'mapcenter') ->modelZoom($model, 'mapzoom') ->modelFeature($model, 'location') ->end(); ?> ...
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
.string|array
Name or configuration of the first tile layer.
Default: 'OpenStreetMap'
.array
Type and options for the default marker.
Default: [ 'type' => 'DotMarker' ]
. array
HTML options of the map container. Use this to explicitly set the ID.
Default: [ ]
(empty array).array
JavaScript options of the map.
Default: [ ]
(empty array).null|true|array
Options for MarkerClusterer.
If null
: no clustering. If true
: clustering with default options. Default: null
.null|true|array
Options for popups.
If null
: no popups. If true
: popups with default options. Default: null
.null|int
Display a Scale Control
on the map. Can be null
(no Scale Control), SCALE_METRIC
, SCALE_IMPERIAL
or SCALE_BOTH
.
Default: SCALE_METRIC
.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
.bool
Whether to use 'fly-animation'
when a Marker is placed after find. string
Namespace of the Tile*
classes, defining the tile layers.
Use this to add your own tile layer. Locator is an Yii2 Widget, so it inherits all of its properties., (*21)
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., (*22)
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'
., (*23)
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., (*24)
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):, (*25)
Name | Variants | Required option |
---|---|---|
OpenStreetMap | BlackAndWhite, HOT | |
OpenMapSurfer | Roads, Hybrid, AdminBounds, ContourLines, Hillshade | |
OpenTopoMap | ||
OpenSeaMap | ||
Wikimedia (affiliated sites) | ||
Carto | Light, Dark, Voyager | |
Stamen | Toner, TonerBackground, TonerLines, TonerLabels, TonerLite, Watercolor, Terrain, TerrainBackground, TerrainLabels | |
EsriWorld | ||
Here | lots (see TileHere.php) | [ 'apiKey' => '...' ] |
TomTom | Basic, Hybrid, Labels | [ 'key' => '...' ] |
Kadaster (Netherlands only) | standaard, grijs, pastel, water | |
Amsterdam | light, zw |
If $tile
is not set, Locator uses tiles from OpenStreetMap., (*26)
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., (*27)
Generally, there will be no options, apart from the API key some providers expect. Other options may be added., (*28)
Currently, Locator supports the following providers (there may be more in the future):, (*29)
Name | Required option |
---|---|
Nominatim, by OpenStreetMap | |
GeoNames | [ 'username' => '...' ] |
Here | [ 'apiKey' => '...' ] |
TomTom | [ 'key' => '...' ] |
Kadaster (Netherlands only) |
Notice that some providers may stipulate that you should use their service only on map tiles of the same provider., (*30)
If you don't explicitly set a geocoder, Leaflet-search uses Nominatim., (*31)
In the property $marker
, and in methods like marker()
, modelMarker()
etc. the
marker type can be set. This is an array
with [ 'type' => '<marker type>' ]
,
supplemented with marker options (dependent on the type). For instance:, (*32)
$map->marker = [ 'type' => 'Marker', 'opacity' => 0.5 ]
Apart from Leaflet's own Marker and CircleMarker, Locator sports two other markers:, (*33)
A simple extension of CircleMarker. It has fixed
radius and always has (at least) the class name 'dot-marker'
. The default marker of
Locator is a DotMarker., (*34)
A marker with a DivIcon. Use this to display FontAwesome markers like so:, (*35)
$map->marker = [ 'type' => 'SpriteMarker', 'html' => '<i class="far fa-2x fa-dog"></i>' ]
Yii2 map widget
MIT
extension yii2 geo yii widget map