ttungbmt/yii2-leaflet-geocoder

Leaflet Interactive Maps Extension Library for Yii2.

1.0.0 2019-06-14 02:46 UTC

This package is auto-updated.

Last update: 2024-04-14 13:58:43 UTC


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

Yii 2 LeafletJs Plugin that adds support for address lookup to Leaflet with dropdown list capabilities and loading icon feedback. This Plugin works in conjunction with LeafLet library for Yii 2 framework.

Important The libraries used on this plugin have been modified from the original plugin source. So, if you wish to modify the plugin, remember that you cannot update it from its original source.

Installation

The preferred way to install this extension is through composer.

Either run

composer require ttungbmt/yii2-leaflet-geocoder "@dev"

or add

"ttungbmt/yii2-leaflet-geocoder" : "@dev"

to the require section of your application's composer.json file.

Usage

There are four services that you can use, even though we have implemented only three:

  • Nominatim
  • Bing
  • MapQuest
  • Google
  • HCMGIS
  • Mapbox
  • Here

Anybody will to help integrate more services, is very welcome :)

use dosamigos\leaflet\layers\TileLayer;
use ttungbmt\leaflet\Leaflet;
use dosamigos\leaflet\types\LatLng;
use ttungbmt\leaflet\geocoder\services\ServiceHCMGIS;
use ttungbmt\leaflet\geocoder\GeoCoder;

// lets use nominating service
$hcmgis = new ServiceHCMGIS();

// create geocoder plugin and attach the service
$geoCoderPlugin = new GeoCoder([
    'service' => $hcmgis,
    'clientOptions' => [
        // we could leave it to allocate a marker automatically
        // but I want to have some fun
        'defaultMarkGeocode' => false
    ],
    'clientEvents'  => [
        'markgeocode' => new JsExpression('function(e){ console.log(e) }')
    ]
]);

// add a marker to center
$marker = new Marker([
    'name' => 'geoMarker',
    'latLng' => $center,
    'clientOptions' => ['draggable' => true], // draggable marker
    'clientEvents' => [
        'dragend' => 'function(e){
            console.log(e.target._latlng.lat, e.target._latlng.lng);
        }'
    ]
]);

// configure the tile layer
$tileLayer = new TileLayer([
    'urlTemplate' => 'http://{s}.google.com/vt/lyrs={map}&x={x}&y={y}&z={z}',
    'clientOptions' => [
        'map' => 'm',
        'attribution' => ''© Google Maps',
        'subdomains' => ['mt0', 'mt1', 'mt2', 'mt3'],
    ]
]);

// initialize our leafLet component
$leaflet = new Leaflet([
    'name' => 'geoMap',
    'tileLayer' => $tileLayer,
    'center' => $center,
    'zoom' => 10,
]);

// add the marker
$leaflet->addLayer($marker);

// install the plugin
$leaflet->installPlugin($geoCoderPlugin);

// run the widget (you can also use dosamigos\leaflet\widgets\Map::widget([...]))
echo $leaflet->widget();

Testing

$ ./vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

The BSD License (BSD). Please see License File for more information.

2amigOS!
Web development has never been so fun!
www.2amigos.us