map.php 3.82 KB
<?php
/**
 * Created by PhpStorm.
 * User: allakuzmenko
 * Date: 23.10.17
 * Time: 21:02
 */

use yii\helpers\Html;

use frontend\modules\map\Map;
use frontend\themes\defaults\assets\AppAsset;

$urlMapResource = Map::getUrlListByType('resource');
$urlMapProjects = Map::getUrlListByType('project');

$bundle = AppAsset::register($this);

$js_icons = [];
$js_features = [];
function jsString($str = '')
{
    return preg_replace("/('|\"|\r?\n)/", '', $str);
}

foreach ($models as $model) {
    /* $js_icons[] = '
                 ' . $group['id'] . ':  { icon: \'' . addslashes($group->getImageLink()) . '\'}';*/
    $content = $this->render('parts/_popup', [
        'model' => $model,
        'types' => $types,
    ]);
    $position = str_replace('(', '', $model['coordinates']);
    $position = str_replace(')', '', $position);
    $position = explode(',', $position);
    $js_features[] = '{   
                    "id": "' . $model['id'] . '",    
                    "title": "' . addslashes($model['lang']['title']) . '",
                    "position": {"lat": ' . $position[0] . ', "lng": ' . $position[1] . '},
                    "type": "' . $model['type_id'] . '",
                    "direction": "' . $model['area_id'] . '",
                    "region": "' . $model['region_id'] . '",
                    "power": "' . $model['electric_power'] . '",
                    "investment": "' . $model['investment'] . '",
                    "icon": "' . $bundle->baseUrl . '/img/curent-wind.png",
                    "infoWindow": {"content": "' . addslashes(jsString($content)) . '"}
                }';
}

if (!empty($js_icons)) {
    $js_icons = implode(",", $js_icons);
} else {
    $js_icons = '';
}

if (!empty($js_features)) {
    $js_features = implode(",", $js_features);
} else {
    $js_features = '';
}
?>
<div class="inner-map-container">
    <div class="static-btn-container">
        <?= (Yii::$app->request->get('type') == 'project')
            ? Html::a(Yii::t('front', 'resource potential'), $urlMapResource, [
                'class' => 'static-btn static-btn-resours'
            ]) : Html::a(Yii::t('front', 'projects'), $urlMapProjects, [
                'class' => 'static-btn static-btn-project'
            ])
        ?>
    </div>
    <div class="maps-action-elements">
        <?= $this->render('parts/filters', ['types' => $types, 'regions' => $regions]); ?>
        <div class="search-block">
            <form class="search-form">
                <input class="search-input" name="" placeholder="пошук">
                <button type="submit" class="search-input-btn"></button>
            </form>
        </div>
        <div class="all-obj">
            <a href="<?= Map::getUrlList() ?>" class="link-all-obj"><span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>
        </div>
    </div>

    <div class="map" id="map">
    </div>

</div>
<?php
$script = <<< JS
    var markers = [$js_features];
 
    function initMap() {
            var myLatLng = {lat: 48.741280, lng: 30.215513};

            var map = new google.maps.Map(document.getElementById('map'), {
                zoom: 6,
                center: myLatLng,
                scrollwheel: false

            });
            for (var i = 0, l = markers.length; i < l; i++) {
                var marker = markers[i];
                marker.map = map;
                markers[i] = new google.maps.Marker(marker);
            }
        }
JS;

$this->registerJsFile($bundle->baseUrl . '/libs/infoBubble/infobubble.js');
$this->registerJsFile('//developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js');

$this->registerJsFile('//maps.googleapis.com/maps/api/js?key=AIzaSyCRjI5CDX5BPpcmvu6a6bSzDyMXThMaNeo&callback=initMap');

$this->registerJs($script, yii\web\View::POS_HEAD);