What is the process for customizing the marker icon on a leaflet map using NUXT.js?

I'm currently in the process of changing the marker icon for individual markers on my OpenStreetMap.

  mapIconsReinit(L) {
    delete L.Icon.Default.prototype._getIconUrl;

    L.Icon.Default.imagePath = ''
    L.Icon.Default.mergeOptions({
      iconRetinaUrl: require('@/assets/img/map_markers/default/marker-icon-2x.png'),
      iconUrl: require('@/assets/img/map_markers/default/marker-icon.png'),
      shadowUrl: require('@/assets/img/map_markers/default/marker-shadow.png'),
    });
  },

  getMarkerIcon(L, color) {
    return L.divIcon({
      iconRetinaUrl: require('@/assets/img/map_markers/marker-icon-2x-' + color + '.png'),
      iconUrl: require('@/assets/img/map_markers/marker-icon-' + color + '.png'),
      shadowUrl: require('@/assets/img/map_markers/marker-shadow.png'),
      iconSize: [25, 41],
      iconAnchor: [12, 41],
      popupAnchor: [1, -34],
      shadowSize: [41, 41]
    })
  }

The first function is functioning correctly with paths such as '@/...', however, the second one is not.

The default marker appears to be working fine:

L.marker([marker.lat, marker.lng]).addTo(_context.map)

but when attempting to use a custom marker:

L.marker([marker.lat, marker.lng], {icon: this.getMarkerIcon(L, "red")}).addTo(_context.map)

All that shows up is a white square

https://i.sstatic.net/IAlQX.png

Answer №1

When creating a Leaflet DivIcon, ensure that you are passing options specifically for the DivIcon and not for a regular Icon.

To correctly set up your DivIcon, use L.divIcon instead of L.icon.

The Icon option requires the iconUrl (and other *Url) to display the image on the map.

In contrast, the DivIcon does not display an image but rather a simple HTML div element, allowing you to customize it with any HTML content. By default, it appears as a white square with a black border.

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

Using JavaScript, eliminate a tier of item

Utilizing reactJS currently, my objective is to reindex each object in hooks with the hooks.event nested inside each hook: const supervisor = { "hooks": [ { "is_activate": true, "event&qu ...

Encountering an issue while attempting to assess a Meteor package within a local environment

Hello everyone! I'm a beginner in Meteor programming and currently following the online book discovermeteor.com. I recently came across a chapter that covers the creation of Meteor Packages. Excitedly, I proceeded to create my own package using the ...

Elasticsearch query fails to execute when encountering a special character not properly escaped

I am facing an issue with querying a keyword that includes a dot (.) at the end. While the query works perfectly on Kibana's console, it fails to execute on my application's function. The following function is responsible for creating the query b ...

Enhancing Date formatting in Jquery Data tables following ASP.NET Serialization

Currently, I am facing an issue with formatting dates in a SQL database query that is being serialized by ASP and then converted to JSON for display in Datatables using JavaScript. Instead of the correct date format, I am seeing: /Date(1424563200000)/. I ...

What is the best way to transform a JSON object with various key-value pairs into a list using JavaScript?

There's a JSON string that I need to convert into a different format, here is the original: var jsonData = [{"label":"Hass1(<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="354d4d4d6a465058755d5a4158545c591b565a58">[emai ...

Develop an XML document with the use of either Javascript or PHP

I have an XML file that contains information about bracelets with photo details. <bracelets> <photo filename="b1.jpg" thumbnail="a1.jpg" description="aa" /> <photo filename="b2.jpg" thumbnail="a2.jpg" description="aa" /> & ...

JavaScript Stopwatch Break

Below is the code snippet. How can a button be implemented to pause the timer and then resume it when the resume button is pressed? The // marks indicate where I plan to add my pause and resume functionality. Thank you in advance for your assistance! &l ...

Build a PHP-driven form for data collection

Previously, I posted this question but didn't receive a clear answer. Here's what I'm confused about: Below is the HTML snippet: <body onload="showcontent()"> <!-- onload optional --> <div id="content"><img src ...

What is the best way to incorporate a state variable into a GraphQL query using Apollo?

My current challenge involves using a state as a variable for my query in order to fetch data from graphQl. However, I'm encountering an issue where the component does not read the state correctly. Any suggestions on how to solve this? class usersSc ...

Is there a way to obtain the ultimate outcome from an array of asynchronous functions efficiently?

get-video-duration is a useful npm module designed to fetch the duration of a video. const { getVideoDurationInSeconds } = require('get-video-duration') // Accessing the duration from a local path... getVideoDurationInSeconds('video.mov&ap ...

Transmitting video through a local area network

I am seeking a solution to stream video content to a local network with potentially over 1000 viewers. The streaming will need to be accessible via web browsers such as Internet Explorer, Chrome, and Firefox, as not all users have internet access due to co ...

The AngularJS Controller Function

I'm working with a code snippet that includes an Angular Modular Controller containing a function inside the controller itself with a call. I'm unsure if this coding practice is allowed in JavaScript or Angular, and if it is, how does it work? Ta ...

The lack of HTML pre-rendering in a Next.js app with SSR means that the content is not accessible to web-scrapers

I'm encountering an issue with my next.js app that utilizes server side rendering. Upon running the application locally and in production (on vercel edge), the initial page response seems to be a javascript bundle rather than the fully rendered HTML p ...

Display the HTML content once the work with AJAX and jQuery has been successfully finished

Below are the codes that I have created to illustrate my question. It's not a complete set of code, just enough to explain my query. The process goes like this: HTML loads -> calls ajax -> gets JSON response -> appends table row with the JSO ...

Easily submit both FormData and a string in a single function call

I am trying to send data from a form (including a file input and string input) via ajax to an ASP.NET Function. When sending only files, I use the following code: function readURL() { var input = document.getElementById("fileUpload"); var files ...

Combining Google Calendar authentication with FullCalendar integration

I recently started using the gcal example with full calendar from this source: https://fullcalendar.io/js/fullcalendar-3.8.0/demos/gcal.html. To authenticate, we are passing our calendar API and calendar ID, which is working well. However, I am worried ...

How do I showcase the array value on top of a form using Javascript?

My current form displays errors using alerts, but I want to update it so that the errors are printed above the input fields for easy reference by users. This is the existing code for displaying errors: function showErrors() { if (errList.length >= ...

Creating a basic dynamic slideshow/timelapse from scratch

I have a directory containing webcam images that are saved periodically. I am interested in creating a slideshow using these images for a time-lapse effect, without any transition effects or background music. The slideshow should be dynamic so I can use ...

"Troubleshooting a Node.js JWT issue in a React.js

I've been diving into backend development and recently created some small APIs. They all function perfectly in Postman, but I encountered an issue when attempting to execute this particular call const onSubmit = async () => { try { setIsL ...

Enhance user experience with real-time form validation in Bootstrap 4 as the user inputs

In order to troubleshoot my issue, I created a simple index.php file with 2 input fields and a button that redirects to page2.php. These input tags have a regex pattern for Bootstrap-4 form validation. Issue 1: The validation only occurs after clicking ...