使用Leafletjs显示PostGIS数据

时间:2015-04-29 13:38:09

标签: javascript postgresql yii2 leaflet postgis

我正在使用PostGIS学习GIS,我想尝试一些有趣的东西,所以我从OSM下载了形状文件,在PostGres中导入了PostGIS扩展,现在我想用视觉表示PostGIS的数据。我有查询

的数据
SELECT ST_AsGeoJSON(geom) FROM "dar-es-salaam_tanzania.land_coast;

我有一堆GeoJSON并想向用户展示它们。不幸的是它没有显示出来。我使用Yii2来编写提取数据的代码。这是控制器代码:

public function actionIndex()
{
    $cmd = Yii::$app->db->createCommand('SELECT ST_AsGeoJSON(geom) FROM "dar-es-salaam_tanzania.land_coast";');
    $gjsons = [];
    foreach($cmd->queryAll() as $row)
    {
        $gjsons[] = json_decode($row['st_asgeojson']);
    }
    return $this->render('index', ['geojson'=>json_encode($gjsons)]);
}

并在视野中

<?php
/* @var $this yii\web\View */
$this->title = 'Map Application';

$this->registerJs("
    var map = L.map('map').setView([6.8000, 39.2833], 13);
    L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href=\http://openstreetmap.org\">OpenStreetMap</a> contributors, ' +
            '<a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA</a>, ' +
            'Imagery © <a href=\"http://mapbox.com\">Mapbox</a>',
        id: 'examples.map-20v6611k'
    }).addTo(map);
    var geojsonList = $geojson;
    L.geoJson(geojsonList).addTo(map);


"); 
?>
 <div id="map">
 </div>

有什么遗失的东西吗?我是传单库的新手,我正在他们的网站上关注他们的教程

更新

这是HTML输出(剥离的Geojson适合SO)here是完整的源代码 注意我设置了地图的高度,我可以看到一个带有(+)和( - )符号的灰色框,但没有显示地图。

<!DOCTYPE html>
<html lang="en-US">
<head>
    <title>Map Application</title>
    <link href="/tech/maps-app/web/assets/7edc4543/css/bootstrap.css" rel="stylesheet">
    <link href="/tech/maps-app/web/css/site.css" rel="stylesheet">
    <link href="/tech/maps-app/web/css/map.css" rel="stylesheet">
    <link href="/tech/maps-app/web/leafletjs/leaflet.css" rel="stylesheet"></head>
<body>

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

</script><script src="/tech/maps-app/web/assets/c29e8e0a/jquery.js"></script>
<script src="/tech/maps-app/web/assets/f3e1524/yii.js"></script>
<script src="/tech/maps-app/web/leafletjs/leaflet.js"></script>
<script src="/tech/maps-app/web/assets/7edc4543/js/bootstrap.js"></script>
<script type="text/javascript">
    jQuery(document).ready(function () {

        var map = L.map('map').setView([6.8000, 39.2833], 13);
        L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {
            maxZoom: 18,
            attribution: 'Map data &copy; <a href=\http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
                '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
                'Imagery © <a href="http://mapbox.com">Mapbox</a>',
            id: 'examples.map-20v6611k'
        }).addTo(map);
        var geojsonList = [{"type":"MultiPolygon","coordinates":[[[[39.094989013528,-7.2022471828125],[38.638288027056,-7.2022471828125],[38.638288027056,-6.638130390625],[39.094989013528,-6.638130390625],[39.094989013528,-7.2022471828125]]]]}];
        L.geoJson(geojsonList).addTo(map);



    });
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您没有设置高度容器。在您的css文件中设置:

#map {
    height: 400px;
}

并设置缩放。像那样:

L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {zoom: 10, ...