最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Leaflet change circle marker color based on text field - Stack Overflow

programmeradmin1浏览0评论

I have circle markers on my leaflet map using the below code which all works fine.

However I want to show the markers in a different color based on the attribute field named 'stype'.

Any help or guidance on how I can achieve this?

        function siteslabels (feature, layer){
        layer.bindPopup("<p class='info header'>"+ 
        "<b>" + feature.properties.SITE + "</b>" + 
        "</br>" + feature.properties.Address1 +
        "</br>" + feature.properties.stype +
        "</p>");
        };


        var geojsonMarkerOptions = {
            radius: 8,
            fillColor: 'green',
            color: 'black',
            weight: 1,
            opacity: 1,
            fillOpacity: 0.8
        };

        L.geoJson(sites, {
            pointToLayer: function (feature, latlng) {
                return L.circleMarker(latlng, geojsonMarkerOptions);
            },
            onEachFeature: siteslabels
        }).addTo(map);

I have circle markers on my leaflet map using the below code which all works fine.

However I want to show the markers in a different color based on the attribute field named 'stype'.

Any help or guidance on how I can achieve this?

        function siteslabels (feature, layer){
        layer.bindPopup("<p class='info header'>"+ 
        "<b>" + feature.properties.SITE + "</b>" + 
        "</br>" + feature.properties.Address1 +
        "</br>" + feature.properties.stype +
        "</p>");
        };


        var geojsonMarkerOptions = {
            radius: 8,
            fillColor: 'green',
            color: 'black',
            weight: 1,
            opacity: 1,
            fillOpacity: 0.8
        };

        L.geoJson(sites, {
            pointToLayer: function (feature, latlng) {
                return L.circleMarker(latlng, geojsonMarkerOptions);
            },
            onEachFeature: siteslabels
        }).addTo(map);
Share edited May 30, 2017 at 11:20 Sukh_Internazionale asked May 26, 2017 at 16:16 Sukh_InternazionaleSukh_Internazionale 811 gold badge1 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Got it sorted. Here is my code

    <script>    
        <!-- long and lat for UK & Zoom level for whole of UK  -->
        var map = L.map('map',{ center:[54.038486, -1.948915], zoom: 5});

        L.tileLayer('http://{s}.tile.osm/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm/copyright">OpenStreetMap</a> contributors'
        }).addTo(map);



        <!-- LAYERS/SITES -->
        <!-- LAYERS/SITES POP UP CONTENT-->
        function siteslabels (feature, layer){
        layer.bindPopup("<p class='info header'>"+ 
        "<b>" + feature.properties.SITE + "</b>" + 
        "</br>" + feature.properties.Address1 +
        "</br>" + feature.properties.stype +
        "</p>");
        };

        <!-- LAYERS/SITES POP UP COLOUR CIRCLE MARKERS->
        function getColor(stype) {
          switch (stype) {
            case 'POP':
              return  'orange';
            case 'Regen':
              return 'green';
            case 'LLU':
              return 'blue';
            case 'Colo':
              return 'purple';
            case 'DMSU':
              return 'blue';
            default:
              return 'white';
          }
        }

        <!-- LAYERS/SITES ADD LAYER->
        L.geoJson(sites, {
            pointToLayer: function (feature, latlng) {
            return new L.CircleMarker(latlng, {radius: 8, 
                                                fillOpacity: 1, 
                                                color: 'black', 
                                                fillColor: getColor(feature.properties.stype), 
                                                weight: 1,});
            },
            onEachFeature: siteslabels
        }).addTo(map);


    </script>

Check the Leaflet GeoJSON tutorial again, pointToLayer option section.

If you render your points as CircleMarkers, you can easily set their style to use different colours.

If you stick with Markers, you have to provide custom Icons. You can look for marker plugins, e.g. Leaflet.Extra-Markers.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>