问题描述
我正在使用PostGIS学习GIS,我想尝试一些有趣的东西,所以我从OSM下载了形状文件,在PostGres中导入了PostGIS扩展,现在我想用视觉表示PostGIS的数据。我已经使用查询拉取数据
I'm learning GIS with PostGIS and I wanted to try something funny so I downloaded shape files from OSM, imported in PostGres with PostGIS extension and now I want to represent data from PostGIS visually. I have Pulled data with Query
SELECT ST_AsGeoJSON(geom) FROM "dar-es-salaam_tanzania.land_coast;
我收到一堆GeoJSON并希望向用户展示它们。不幸的是它没有显示它。我使用Yii2来编写代码拉取数据。这是控制器代码:
I got bunch of GeoJSON and wanted to show them to user. Unfortunately it does not show it. I use Yii2 to write codes that pulls the data. Here is the controller code:
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 © <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>
是否有遗漏的东西?我是传单库的新手,我正在他们的网站上关注他们的教程
Is there something that am missing? I'm new to leaflet library and am following their tutorials on their site
更新
UPDATE
这是HTML输出(剥离的Geojson适合SO)是完整来源
注意我设置了地图的高度,我可以看到一个带有(+)和( - )符号的灰色框,但没有显示地图。
Here is the HTML Output (stripped Geojson to fit SO) here is full sourceNote that I set Height of the map and I can see a grey box with (+) and (-) signs on it but no map is shown.
<!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 © <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>
推荐答案
您没有设置高度容器。在你的css文件中设置:
You didn't set height container. Set in your css file:
#map {
height: 400px;
}
并设置缩放。像那样:
L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {zoom: 10, ...
这篇关于使用Leafletjs显示PostGIS数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!