2017-08-10 83 views
2

我想根據最近草皮更改地圖點擊上的圖標大小。我如何做到這一點? nearestBuilding.properties['icon-size'] = 0.2;不起作用。如何在點擊時更改mapbox gl中的圖標大小?

var retail = { 
     type: 'FeatureCollection', 
     features: [ 
      { 
       type: 'Feature', 
       properties: { 
        title: 'TEST', 
        description: 'TEST' 
       }, 
       geometry: { 
        type: 'Point', 
        coordinates: [121.051779, 14.550224] 
       } 
      }, 
      { 
       type: 'Feature', 
       properties: { 
        title: 'TEST', 
        description: 'TEST' 
       }, 
       geometry: { 
        type: 'Point', 
        coordinates: [121.04568958282472, 14.552170837008527] 
       } 
      } 
     ] 
    }; 

    map.on('load', function() { 
     map.loadImage('https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Wiki_Loves_Earth_map_marker.svg/600px-Wiki_Loves_Earth_map_marker.svg.png', function(error, image) { 
      if (error) throw error; 
      map.addImage('marker', image); 

      map.addLayer({ 
       id: 'retail', 
       type: 'symbol', 
       source: { 
        type: 'geojson', 
        data: retail 
       }, 
       layout: { 
        'icon-image': 'marker', 
        'icon-size': 0.1 
       }, 
       paint: { } 
      }); 
     }); 
    }); 
var marker = null; 

    map.on('click', function(e){ 
     if(marker != null) { 
      marker.remove(); 
     } 

     var currentLocation = { 
      type: 'Feature', 
      geometry: { 
       type: 'Point', 
       coordinates: [e.lngLat.lng, e.lngLat.lat] 
      } 
     }; 

     var el = document.createElement('div'); 
     el.className = 'currLocMarker'; 

     marker = new mapboxgl.Marker(el, { offset: [-50/2, -50/2] }) 
      .setLngLat(currentLocation.geometry.coordinates) 
      .addTo(map); 

     var currentLocation = turf.point([e.lngLat.lng, e.lngLat.lat]); 
     var nearestBuilding = turf.nearest(currentLocation, retail); 
     var distance = turf.distance(currentLocation, nearestBuilding); 

     if (distance <= 0.5) { 
      nearestBuilding.properties['icon-size'] = 0.2; 
     } 
    }); 

回答

1

由於icon-size支持數據驅動樣式(https://www.mapbox.com/mapbox-gl-js/style-spec/#layout-symbol-icon-size),你試圖做的是,從每個要素的屬性標識的功能?你可以在佈局中配置它,而不是硬編碼0.1。 關於數據驅動樣式的更多文檔在這裏 - https://www.mapbox.com/mapbox-gl-js/style-spec/#function-type

+0

ohhh您的意思是在'var retail'中添加圖標大小? – Merida

+0

我覺得操縱.marker的作品呢?我只是使用mapboxgl.Marker來輕鬆地操作圖標。謝謝! – Merida