2amigos / yii2-leaflet-extension

Yii 2 Extension library to display interactive maps with Leaflet .
http://yiiwheels.com
Other
30 stars 26 forks source link

Setting control layers #46

Open arjenmeijer opened 5 years ago

arjenmeijer commented 5 years ago

I am trying to add tilelayers combined with the Markercluster plugin. This is working and provided as documentation: ... use dosamigos\leaflet\layers\TileLayer; use dosamigos\leaflet\controls\Layers; ... $center = new LatLng(['lat' => (float)$latitude, 'lng' => (float)$longitude]);

$openstreet = new TileLayer([ 'urlTemplate' => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', 'clientOptions' => [ '© OpenStreetMap contributors' ] ]);

$arogis = new TileLayer([ 'urlTemplate' => 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', 'clientOptions' => [ '© OpenStreetMap contributors' ] ]);

$baseLayers = new Layers(); $baseLayers->setBaseLayers(['Card' => $openstreet, 'Photo' => $arogis]);

$leaflet = new LeafLet([ 'tileLayer' => $openstreet, 'center' => $center, 'zoom'=> 13, ]);

$leaflet ->setControls([$baseLayers]);

Adding Markercluster works also.

$cluster = new MarkerCluster([ 'url' => Url::to(['you rur lreturning json']), ]); and changing $leaflet to: $leaflet ->setControls([$baseLayers]); $leaflet ->installPlugin($cluster);

Do to: a. Setting grouplayers b. Combining groupslayer control and MarkerCluster.

tonydspaniard commented 5 years ago

Not sure I understand your challenge.

arjenmeijer commented 5 years ago

I do not understand how I can set grouplayers. How can I add records to a group layer? I have:

$layerGroups = [];

foreach($uniqegroup as $line) {
    $layerGroups[$line['name']] = new LayerGroup(['name' => $line['id'] ]);
}

This will set the layersgroups and give them a name. But it doesn't link the items to it. So I missing the concept to add the individual item to a group. How should I do that?

tonydspaniard commented 5 years ago

A layerGroup configures a set of layers (see the layers folder) objects and adds it to the layerControl. Please, see the examples: https://leafletjs.com/examples/layers-control/.

Please, check the tests here: https://github.com/2amigos/yii2-leaflet-extension/blob/master/tests/functional/LayersControlTest.php#L85 , that would give you a good starting point, the only thing missing is adding the layer to the map.