如何在地图上添加标记来自geoJson有很多点(Leaflet)

时间:2014-05-21 17:16:30

标签: javascript leaflet

我试图在地图上添加一些标记,使用带有Points的geoJson,我跟随传单documentation,但它仍然说:

  

错误:GeoJSON对象无效   抛出新错误('无效的GeoJSON对象。');

我的GeoJson:

var meta1nJson={
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -38.3613558,
          -8.8044875
        ]
      },
      "properties": {
        "Ordem": "193",
        "Eixo": "Leste",
        "Meta": "1L",
        "Municipio": "Petrolândia",
        "Estado": "PE",
        "Nome da Comunidade": "Agrovila 4"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -38.3445892,
          -8.7940031
        ]
      },
      "properties": {
        "Ordem": "194",
        "Eixo": "Leste",
        "Meta": "1L",
        "Municipio": "Petrolândia / Floresta",
        "Estado": "PE",
        "Nome da Comunidade": "Agrovila 5"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -37.8521847,
          -8.6774657
        ]
      },
      "properties": {
        "Ordem": "195",
        "Eixo": "Leste",
        "Meta": "1L",
        "Municipio": "Inajá/Ibimirim",
        "Estado": "PE",
        "Nome da Comunidade": "Indígena Kambiwá - Baxa da Alexandra"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -37.9229577,
          -8.645232
        ]
      },
      "properties": {
        "Ordem": "196",
        "Eixo": "Leste",
        "Meta": "1L",
        "Municipio": "Inajá",
        "Estado": "PE",
        "Nome da Comunidade": "Indígena Kambiwá -  Barracão"
      }
    }
  ]
};

以及如何尝试渲染标记:

var layerComunidades1N = L.geoJson(meta1nJson).addTo(map);

我无法找到我做错的事情,根据传单文档,如果我没有通过选项pointToLayer它应该呈现默认标记,我错了吗?

2 个答案:

答案 0 :(得分:2)

我发现你的geojson没有任何问题。我将您的geojson复制到geojson.ioworks fine

看起来您确实错误地调用了GeoJSON(上面的示例显示了对geoJson的调用),但这并不能解释您获得的错误......

无论如何,here is a working jsfiddle that visualizes your geojson as markers on a leaflet map

这是相关的代码(有一个很好的衡量基础层):

var map = L.map('map', {
    center: [-9, -38],
    zoom: 7
});

L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {       
    id: 'examples.map-20v6611k'
}).addTo(map);

new L.GeoJSON(meta1nJson).addTo(map);

希望这有帮助。

答案 1 :(得分:1)

试试这个:

/ *实例化图标。您还可以有多个图标* /

var ComunidadeIcon = L.icon({
  iconUrl: 'http://iambrony.dget.cc/mlp/gif/angel_stand_right.gif',
  iconSize: [32, 37],
  iconAnchor: [16, 37],
  popupAnchor: [0, -28]
});

/ *列出来自GJson * /

的FeatureCollection的对象特征
var layerComunidades1N = L.geoJson(meta1nJson, {
  pointToLayer: function (feature, latlng) {
    return L.marker(latlng, {icon: ComunidadeIcon});
  }, onEachFeature: onEachFeature
}).addTo(map);

/ *这个功能可以容纳很多东西。我用来显示弹出窗口* /

function onEachFeature(feature, layer) {
  var popupContent = "<p>DaTerra Web</p>";
  if (feature.properties && feature.properties.popupContent) {
    popupContent += feature.properties.popupContent;
  }
  layer.bindPopup(popupContent);
}