Is it possible to customize the Maki icon color on mapbox?

I've been racking my brain trying to figure out how to dynamically add color to Mapbox's maki icons using plain Javascript. The documentation offers solutions using deprecated tools like tilemill or CSS, but my icons are generated on-the-fly from an array of objects containing Lat/Lon coordinates. What I really need is a way to color them based on other data within the objects - for example, I want the size and color of a transit station icon to reflect the amount of daily traffic it receives. Unfortunately, Mapbox's default icons offer limited size options (large, medium, small) but not much flexibility for color control.

Here's the code snippet I've been working with:

featureArray = []

// stationData is an array of objects
stationData.forEach(function(station) {

            var markerObj = {
                type: 'Feature',
                properties: {
                    title: station.StationName,
                    "icon": {
                        "iconUrl": "public/maki/renders/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1dcd0c3daf9c1dfd6">[email protected]</a>",
                        "iconSize": [40, 40],
                        "icon-fill": "#DF0101",
                        "popupAnchor": [0, -15],
                        "className": "dot",
                        "iconColor": '#fa0' // does not work
                    }
                },

                geometry: {
                    type: 'Point',
                    coordinates: [parseFloat(station.Lng), parseFloat(station.Lat)]
                }
            }

            featureArray.push(markerObj);
        });

        var geojson = {
            type: 'FeatureCollection',
            features: featureArray
        };

        stationLayer.on('layeradd', function(e) {
            var marker = e.layer,
            feature = marker.feature;

            marker.setIcon(L.icon(feature.properties.icon));
        });

        stationLayer.setGeoJSON(geojson);

        stationLayer.on('mouseover', function(e) {
            e.layer.openPopup();
        });

        stationLayer.on('mouseout', function(e) {
            e.layer.closePopup();
        }); 

After hours of searching and experimenting, I'm still coming up short on a solution. Any advice or guidance would be greatly appreciated.

Thanks,

William

Answer №1

Regrettably, the default mapbox icons lack robust sizing options, offering only limited choices (large, medium, small) for determining size.

To overcome this limitation, you will need to utilize Mapbox.js and L.mapbox.markerLayer or L.mapbox.marker.icon. Sizes beyond the basic options (large, medium, small) would be scaled in the browser and may appear blurry. Additionally, if you use the maki download, the icons may not be recolorable since our markers API recolors icons dynamically using server-side code with node-blend.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Moving through content on a single page

import React, { Component, useState } from "react"; import { Content, List, ListItem, Left, Right, Icon, Container, Header, Body, Button, Title, } from "native-base"; //Chapter One expor ...

Encountering an issue while attempting to test geolocation functionality in the web browser

I've been working on integrating the geolocation API into my app and came across a suitable resource at the MDN website. However, when I attempted to test for the existence of the geolocation object in the browser, I encountered this error: Server Err ...

How can you ensure that an element returns to its original position after being animated in jQuery?

I want to add some animations to my website using jQuery, but I lack the skills to create them from scratch. I have managed to copy some code snippets, but I am facing an issue with one of my animations. When I click a button, the block animates, but I wan ...

I'm having trouble figuring out how to access response headers with HttpClient in Angular 5. Can anyone

I recently developed an authentication service in Angular 5, where I utilize the HttpClient class to make a POST request to my backend server. The backend server then responds with a JWT bearer token. Here is a snippet of how my request looks: return thi ...

Unable to adjust the padding of a div while the Bootstrap 4 navbar is in a collapsed state on mobile devices

I am facing an issue with a fixed navbar at the top of my page. Below the navbar, I have the page content which includes a Bootstrap 4 image carousel. The problem arises on mobile devices where I need to add top-padding to the page-container (below the nav ...

Using ReactJS setInterval with Parameters

One of the functionalities in my code involves setting a time interval based on a specific condition. If the condition is met, the function should continue running until it is no longer true, then it should stop. class TestClassName extends React.Component ...

IE11 blocking .click() function with access denied message

When attempting to trigger an auto click on the URL by invoking a .click() on an anchor tag, everything works as expected in most browsers except for Internet Explorer v11. Any assistance would be greatly appreciated. var strContent = "a,b,c\n1,2,3& ...

React Navigation - CSS file identified as "text/html" after introducing a dynamic route parameter (/userId)

Our customized stylesheet "styles.css" seems to be incorrectly identified with the MIME type "text/html", even though it is specified as: rel="stylesheet" type="text/css" This issue only arises when navigating to routes with a variable parameter such as ...

Tips for sharing posts and photos using FB.api platform

Can anyone help with my code for posting feed and photos within FB.api? var requestData = { message : data1, access_token: response.authResponse.accessToken, link : 'http://jonmosley.co.uk', description : 'Descriptionhdofh o ...

Tips for adjusting column width with flexbox intersections

I am currently working on a React web application that has minimal styling. I have divided the content into 3 columns, with the leftWrap being col-3, rightWrap being col-4, and the remaining width is for centerWrap. I want to apply flex ...

I am facing an issue where both curl and file_get_contents are not functioning properly after

When attempting to access a user's city information using coordinates, I have encountered an issue with the response not being displayed in my console. The process involves a javascript function that takes latitude and longitude data, sends it to a PH ...

Combining the redux toolkit function createAsyncThunk with Angular's HttpClient by leveraging the ApiService

Currently, I am working on incorporating @reduxjs/toolkit into an Angular project. Is there a way to pass an Angular service as a parameter to the callback of createAsyncThunk instead of utilizing fetch directly? I referenced the documentation for an exa ...

The performance of the Ionic app is significantly hindered by lagging issues both on Google

After starting to work with the ionic framework, I noticed a significant change in performance when testing an android app on Chrome for the first time. It was fast and responsive until I added a button that led to a screen with navigation bars, side men ...

How can Angular be used to fetch the name associated with a user_id in Laravel?

There seems to be a problem with fetching the name attribute along with the title and body attributes. The name appears empty on page refresh but shows up automatically upon submission. Interestingly, angularjs is unable to retrieve the name successfully. ...

How come when I click on the edit button, the selected data's model doesn't appear in the dropdown menu as I would like it to?

this is the function in my controller public function getModel($make_id = null) { $make_id = request()->make_id; $carsmodel = CarModel::where('make_id', $make_id)->orderBy('model', 'ASC')->get(); return re ...

Is there a way to find out the ultimate destination of a shortened URL without actually opening the webpage?

I am trying to implement a feature where I can click on links within email messages and have the full link displayed in a separate pane using jQuery. However, I am facing some challenges with this implementation. My plan is to use AJAX to call a PHP scrip ...

Guide on utilizing ajax to post data when the URL parameter changes, all without refreshing the page

As the URL parameter changes, the values in my HTML also change. I am passing these values to a JSON file, but they are getting erased when the page refreshes due to the post request. I have attempted to use event.preventDefault(), however, it seems to n ...

Parse the HTML document using an HTTP request to access the Document Object Model (DOM)

I am facing an issue with accessing elements of an HTML page through JavaScript. I have tried different methods, but I keep getting the error message: CORS header "Access-Control-Allow-Origin" missing. Here is the code snippet I tried: <!DOCTYPE html&g ...

Variations of a particular software package are necessary

My current project requires Expo, React, and React-Native as dependencies. The configuration in the package.jason file looks like this: "main": "node_modules/expo/AppEntry.js", "private": true, "dependencies": { "expo": "^28.0.0", "expo-three": "^ ...

A step-by-step guide on combining multiple STL files into a single geometry

Is there a simpler way to handle this code for exporting to an STL file or for the cutting function? I find the current method too troublesome. Thank you! var material1 = new THREE.MeshPhongMaterial( { color:0xffffff, } ); loader.load( 'fanban1.s ...