从本地 geojson 文件地图框/传单创建 var
create var from local geojson file mapbox/leaflet
我想在 html 文件中创建一个 var myGeojson
,该文件使用单独本地存储的 .geojson 或.js文件中的数据。我可以通过在 .geojson 文件中创建一个 var 来做到这一点,该文件可以在 html 文件中使用。但是我需要使用多个未更改的大型 geojson 文件,有没有办法在 html 中创建变量但将数据存储在 geojson 中?
索引.html
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
<script src='data/example.geojson'></script>
</head>
<body>
<div id='map'></div>
<script>
L.mapbox.accessToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
L.mapbox.map('map', 'examples.map-xxxxxxxx')
.setView([37.8, -96], 4)
.featureLayer.setGeoJSON(myGeojson);
</script>
</body>
</html>
data/example.geojson
var myGeojson =
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "Dinagat Islands"
}
}
使用L.mapbox.featureLayer
中已经包含的 XHR 功能怎么样?
索引.html:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
L.mapbox.accessToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var map = L.mapbox.map('map', 'examples.map-xxxxxxxx').setView([37.8, -96], 4);
var layer1 = L.mapbox.featureLayer('data.geo.json').addTo(map);
// You could add as much layers as you want
// var layer2 = L.mapbox.featureLayer('moredata.geo.json').addTo(map);
// Or you could load new data into an existing layer:
//layer1.loadURL('newdata.geo.json');
</script>
</body>
</html>
data.geo.json:
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "Dinagat Islands"
}
}
以下是Plunker上的一个工作示例:http://plnkr.co/edit/jAkQ7v9XVIDCDnQQzpWK?p=preview
但正如评论中所说,如果你真的想采取额外的(在我看来是不必要的)步骤,那么你可以使用选择的 XHR 库并获取文件并将其分配给一个变量(在这里使用 jQuery 的 $.getJSON):
// Empty featureLayer
var featureLayer = L.mapbox.featureLayer().addTo(map);
// Variable for your data
var geojsonData;
// Fetch the file
$.getJSON('data.geo.json', function (results) {
// Assign the results to the geojsonData variable
geojsonData = results;
// Assign the data to the layer
featureLayer.setGeoJSON(geojsonData);
});
这是Plunker上的一个工作示例:http://plnkr.co/edit/ayYgF5fi1MKgTRBg3YAt?p=preview
但是我不明白为什么要引入另一个依赖项,例如jQuery,如果功能层本身具有您需要的完整XHR功能。但还好:)
相关文章:
- 在表单中创建一个黑名单过滤器以避免某些单词
- 如何在表单中创建html元素,而无需重新加载页面
- 在 javascript 中使 split() 方法使用双引号而不是单引号创建数组
- Rails/Ajax 在提交后将表单从创建更改为更新
- JQuery / Javascript弹出框和表单提交创建
- 如何创建动态html表单的创建JSON对象(表单元素)
- 通过多次单击创建字符串
- 从表单输入创建多维关联数组
- JSON 的新增内容.如何在提交表单时创建 json 文件以及在何处查看它
- 使用表单输入创建 PDF 文档
- 如何在HTML表单中创建美元金额输入字段
- 在JavaScript提交表单时创建URL
- 单击创建 DIV;然后通过单击它删除相同的 DIV
- 使用动态表单 jquery 创建多维数组
- 为链接单击创建简单的侦听器
- 尝试使用数据从谷歌表单响应创建谷歌表单每当有人提交表单
- 在PHP表单中创建一个带有自动完成功能的输入字段
- 在javascript生成的表单中创建输入字段
- 在Meteor.js中填写向导表单后创建用户
- 如何在HTML表单中创建两组单选按钮,以及在a组中的更改何时会在B组中更改