在mapbox / leaflet上聚类标记

时间:2013-06-09 02:18:45

标签: javascript json leaflet geojson mapbox

我正在尝试在地图框上设置聚集地图,例如http://leaflet.github.io/Leaflet.markercluster/example/marker-clustering-realworld.388.html

但他们的示例使用普通的.js文件作为数据 http://www.mapbox.com/mapbox.js/assets/realworld.388.js

我从mapbox得到的唯一东西是.geojson http://api.tiles.mapbox.com/v3/thebteam.map-w9jzcznw/markers.geojson

有没有办法可以将geojson转换为js(定期)?或者从mapbox导出javascript数组?

编辑:最终将我的数据切换为CSV并找到解析器。如果有人需要,这是有效的代码:

var url = 'https://docs.google.com/spreadsheet/pub?key=abc123';

$.get(url, function(data) {
  var addressPoints = $.csv.toArrays(data);
  var map = L.mapbox.map('map', 'map-abc123').setView([20.30, 18.98], 2);
  var markers = new L.MarkerClusterGroup({ showCoverageOnHover: false });

  for (var i = 0; i < addressPoints.length; i++) {
    var a = addressPoints[i];
    var title = a[2];
    var marker = L.marker(new L.LatLng(a[0], a[1]), {
      icon: L.mapbox.marker.icon({'marker-size': 'small', 'marker-color': 'e8168c'}),
      title: title
    });
    marker.bindPopup(title);
    markers.addLayer(marker);
  }

  map.addLayer(markers);

});

2 个答案:

答案 0 :(得分:9)

创建geoJson图层,然后将该图层添加到MarkerCluster:

 var markers = new L.MarkerClusterGroup();
 var geoJsonFeature =  = {
  "type": "Feature",
  "properties": {
    "name": "Coors Field",
    "amenity": "Baseball Stadium",
    "popupContent": "This is where the Rockies play!"
  },
  "geometry": {
    "type": "Point",
    "coordinates": [-104.99404, 39.75621]
  }
 };
 var geoJsonLayer = L.geoJson(geoJsonFeature);

 markers.addLayer(geoJsonLayer);
 map.addLayer(markers);

答案 1 :(得分:2)

var geojson = dataFromMapbox;
var lat;
var lng;
for(var i= 0;i<geojson.features.length;i++)
  {
    lat = geojson.features[i].geometry.coordinates[0];
    lng = geojson.features[i].geometry.coordinates[1];

    //create a marker with those values, pass it to a MarkerCluster object

  }