无法在Leaflet地图上绘制从geoJson获得的点

时间:2017-06-08 10:01:13

标签: javascript android html leaflet geojson

我必须实现一个Android应用程序,它在带有Leaflet的WebView中显示一个地图,并使用来自JSON的数据来绘制一些点。

这是WebView中加载的HTML代码:

<title>Leaflet example</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" integrity="sha512-07I2e+7D8p6he1SIM+1twR5TIrhUQn9+I6yjqD53JQjFiMf8EtC93ty0/5vJTZGF8aAocvHYNEDJajGdNx1IsQ==" crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js" integrity="sha512-A7vV8IFfih/D732iSSKi20u/ooOfj/AGehOKq0f4vLT1Zr2Y+RX7C+w8A1gaSasGtRUZpF/NZgzSAu4/Gc41Lg==" crossorigin=""></script>

<style>
    #map {
        width: 600px;
        height: 400px;
    }
</style>

</head>
<body>

<div id='map'></div>
<script src="file:///android_asset/provaquake.js" type="text/javascript">
</script>
<script src="file:///android_asset/map.js"></script>

</body>

这是使用Leaflet调用地图的javascript文件,并绘制一行进行测试:

var map = L.map('map').setView([39.74739, -105], 13);

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
    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: 'mapbox.light'
}).addTo(map);

var myLines = [{
    "type": "LineString",
    "coordinates": [[-105, 40], [-110, 45], [-115, 55]]
}];

var myStyle = {
    "color": "#ff7800",
    "weight": 5,
    "opacity": 0.65
};

L.geoJSON(myLines, {
    style: myStyle
}).addTo(map);

provaquake.js是一个文件,其中包含使用此geoJson初始化的名为“quake”的变量: https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson

我编写了这段代码,将每次地震的位置添加到地图中:

L.geoJSON(quake, {
    style: function (feature) {
        return feature.properties && feature.properties.style;
    },
    pointToLayer: function (feature, latlng) {
        return L.circleMarker(latlng, {
            radius: 8,
            fillColor: "#ff7800",
            color: "#000",
            weight: 1,
            opacity: 1,
            fillOpacity: 0.8
        });
    }
}).addTo(map);

但它似乎不起作用。

除了最后一条关于积分的指令外,所有的代码都运行良好,我对Leaflet来说是全新的,我不知道它为什么不绘制它们

1 个答案:

答案 0 :(得分:0)

这是一个工作示例。我改变你的代码的唯一方法是将fetch添加到quake json并将地图缩放设置为3以进行演示。仔细检查您的quake变量是否正确填充了地震FeatureCollection数据。

&#13;
&#13;
var map = L.map('map').setView([39.74739, -105], 3);

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
  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: 'mapbox.light'
}).addTo(map);

var myLines = [{
  "type": "LineString",
  "coordinates": [
    [-105, 40],
    [-110, 45],
    [-115, 55]
  ]
}];

var myStyle = {
  "color": "#ff7800",
  "weight": 5,
  "opacity": 0.65
};

L.geoJSON(myLines, {
  style: myStyle
}).addTo(map);

fetch('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson')
  .then(function(res) {
    if (res.ok === true) {
      return res.json();
    } else {
      alert('Geojson request failed.');
    }
  })
  .then(function(quake) {
    L.geoJSON(quake, {
      style: function(feature) {
        return feature.properties && feature.properties.style;
      },
      pointToLayer: function(feature, latlng) {
        return L.circleMarker(latlng, {
          radius: 8,
          fillColor: "#ff7800",
          color: "#000",
          weight: 1,
          opacity: 1,
          fillOpacity: 0.8
        });
      }
    }).addTo(map);
  });
&#13;
<title>Leaflet example</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" integrity="sha512-07I2e+7D8p6he1SIM+1twR5TIrhUQn9+I6yjqD53JQjFiMf8EtC93ty0/5vJTZGF8aAocvHYNEDJajGdNx1IsQ==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js" integrity="sha512-A7vV8IFfih/D732iSSKi20u/ooOfj/AGehOKq0f4vLT1Zr2Y+RX7C+w8A1gaSasGtRUZpF/NZgzSAu4/Gc41Lg==" crossorigin=""></script>

<style>
  #map {
    width: 600px;
    height: 400px;
  }
</style>

</head>

<body>

  <div id='map'></div>
  <script src="file:///android_asset/provaquake.js" type="text/javascript">
  </script>
  <script src="file:///android_asset/map.js"></script>

</body>
&#13;
&#13;
&#13;