2016-08-22 18 views
0

У меня есть карта Mapbox с несколькими кластерами и некластерными слоями, которые работают с исключениями. Но когда я пытаюсь добавить уровень подсчета кластера, я получаю следующую ошибку в консоли: Uncaught TypeError: Невозможно использовать оператор 'in' для поиска 'point_count' в undefined.Mapbox JS - Исключение на уровне подсчета кластера

Я следовал the Mapbox cluster tutorial, но не могу понять, почему эта ошибка появляется.

Вот мой код:

function setMyApplicationLayers(map, layers) { 
    getMyApplicationLayers().forEach(function(layer) { 
     map.addLayer(layer); 
    }) 
} 

function getMyApplicationLayers() { 
    var layers = [getMyApplicationRestaurantItemLayer()]; 

    ['#ef59a1', '#6ecff6', '#d7df21'] 
      .forEach(function(color, index) { 
     layers.push(getMyApplicationRestaurantGroupLayer(index, color)); 
    }); 

    layers.push(getMyApplicationRestaurantCountLayer()); 

    return layers; 
} 

function getMyApplicationRestaurantItemLayer() { 
    return { 
     id: 'unclustered-restaurants', 
     type: 'symbol', 
     source: 'my-application-restaurants', 
     layout: { 
      'icon-image': 'marker-15' 
     } 
    } 
} 

function getMyApplicationRestaurantGroupLayer(index, color) { 
    return { 
     id: 'clustered-restaurants-' + index, 
     type: 'circle', 
     source: 'my-application-restaurants', 
     paint: { 
      'circle-color': color, 
      'circle-radius': 18 
     }, 
     filter: getMyApplicationRestaurantGroupLayerFilter(index) 
    } 
} 

function getMyApplicationRestaurantGroupLayerFilter(index) { 
    switch (index) { 
     case 0: 
      return ['>=', 'point_count', 25] 
     case 1: 
      return ['all', 
       ['>=', 'point_count', 5], 
       ['<', 'point_count', 25]] 
     case 2: 
      return ['all', 
       ['>=', 'point_count', 0], 
       ['<', 'point_count', 5]] 
    } 
} 

function getMyApplicationRestaurantCountLayer() { 
    return { 
     id: 'cluster-count', 
     type: 'symbol', 
     source: 'my-application-restaurants', 
     layout: { 
      'text-field': '{point_count}', 
      'text-font': [ 
       'DIN Offc Pro Medium', 
       'Arial Unicode MS Bold' 
      ], 
      'text-size': 12 
     } 
    } 
} 
+1

Я не вижу никаких проблем с посланным кодом. Можете ли вы опубликовать runable-пример, демонстрирующий ошибку? –

+0

Вот [JSFiddle] (https://jsfiddle.net/dyhhqLda/2/). Попробуйте увеличить/развернуть и посмотреть на консоль. Благодаря ! – OhmWang

ответ

0

Это исключение связано с неправильной структурой GeoJSON.

В поставленном JSFiddle включить ряд GeoJSON Характеристика структурирована как этот:

{ 
    type: 'Feature', 
    geometry: { 
     type: 'Point', 
     properties: { 
      description: 'kuk' 
     }, 
     coordinates: [2.7, 46.95] 
    } 
} 

Спецификация GeoJSON говорит:

A feature object must have a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value).

properties член должен быть на Feature объекта, а не объект Point. Правильно отформатированный функция будет выглядеть

{ 
    type: 'Feature', 
    properties: { 
     description: 'kuk' 
    }, 
    geometry: { 
     type: 'Point', 
     coordinates: [2.7, 46.95] 
    } 
} 

Вот модифицированный дем без ошибок: https://jsfiddle.net/dyhhqLda/2/