Tips for adding multiple icons to the same location in React-Leaflet 3

Currently, I am in the process of developing an application using React-leaflet version 3 with TypeScript. One of the requirements for my app is to add multiple icons for one position on the map. For example, a specific [lat,long] location could be identified as a blood donation camp in the morning at 8 A.M., and then transform into an outdoor checkup center; followed by a makeshift music concert and free-food center in the afternoon at 3 P.M. This means that I need to place at least two icons for each marker at a given time. Unfortunately, the icon attribute in the marker tag only allows for a single value.

I attempted a solution using L.divIcon, but have not had success thus far.

const mapNodeIcon = L.divIcon({
html: `<div class="multi-icon-div">
<img src="../icon/myIcons/bloodDonCamp.svg" width="30px"/>
<img src="../icon/myIcons/outdoorCheck.svg" width="30px"/>  
</div>`,
iconAnchor: [10, 10],
popupAnchor: [0, -19],
iconSize: [55, 55],
});

Marker Tag

<Marker 
        key={myMap.id} 
        position={[lat, lng]}
        icon={mapNodeIcon}
        >
        <Popup>
          <h3>This is the event location</h3>
        </Popup>
      </Marker>

The issue I'm encountering here is that the SVG icon images are not loading within the div structure, even though the iconDiv appears as a marker on the map. How can I resolve this problem?

While it may seem like a simple question, I wonder if this method is the most efficient way to solve the issue. Since my map will display different SVG icons based on the time of day (morning and afternoon), I am concerned about the speed of rendering for a multi-node map. Any suggestions or assistance would be greatly appreciated.

Answer №1

It seems that the way you imported the svg won't work. Here is the correct method for importing svg files:

import React from 'react';
import {ReactComponent as Logo} from './logo.svg';

const App = () => {
  return (
    <div className="App">
      <Logo />
    </div>
  );
}
export default App;

You can find a comprehensive list of ways to add svg files in React at this link.

-- Update--

import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';
import tileLayer from '../util/tileLayer';
import L from 'leaflet';

import facebook from './facebook-square.svg';
import twitter from './twitter.svg';

const center = [52.22977, 21.01178];

const MapWrapper = () => {

  const mapNodeIcon = L.divIcon({
    html: `<div style="display: flex;">
      <img src="${facebook}" width="30px"/>
      <img src="${twitter}" width="30px"/>  
    </div>`,
    iconAnchor: [10, 10],
    popupAnchor: [0, -19],
    iconSize: [55, 55],
  });

  return (
    <MapContainer center={center} zoom={18} scrollWheelZoom={false}>
      <TileLayer {...tileLayer} />

      <Marker position={center} icon={mapNodeIcon}>
        <Popup>Center Warsaw</Popup>
      </Marker>

    </MapContainer>
  )
}

export default MapWrapper;

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

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

"Exploring the world of Typescript declarations and the CommonJS module

I'm encountering confusion while creating declaration files (d.ts). For instance, I have developed an NPM package called "a" with a single CommonJS module index.ts: export interface IPoint { x: number; y: number; } export default function s ...

Variable Scope is not defined in the TypeScript controller class of an AngularJS directive

I have implemented a custom directive to wrap ag grid like so: function MyDirective(): ng.IDirective { var directive = <ng.IDirective>{ restrict: "E", template: '<div style="width: 100%; height: 400px;" ag-grid="vm.agGrid ...

Understanding DefinitelyTyped: Deciphering the explanation behind 'export = _;'

Having trouble integrating angular-material with an ng-metadata project and encountering some issues. Utilizing DefinitelyTyped for angular material, the initial lines are as follows: declare module 'angular-material' { var _: string; expo ...

New to Angular: I'm getting the error message "tsc is not recognized as an internal or external command" - Help Needed

I am completely new to Angular, so please pardon my lack of knowledge. I encountered an error: C:\Users\Tijl Declerck\Desktop\projects\AngularTestApp\ts-hello>tsc main.ts "tsc" is not recognized as an internal or external ...

Using Angular to handle routes with a specific domain prefix

Let's say I own the domain https://example.com and I'd like to create a subdomain specifically for my blog, like this: https://blog.example.com. How would you handle the routing for this scenario using Angular? ...

Typescript Jest testing reveals inaccurate error line information

While running a sample Jest test on a particular structure, I noticed that the errors in the output summary from Jest are not lining up correctly. Here is a snippet from my package.json file: "devDependencies": { "@types/jest": "^22.0.1", "jest": ...

Angular's keyevent is acting similarly to a tab when the input field is disabled

I've encountered some strange behavior with my input field. Every time a user presses the enter or space key, I want to execute some actions. Once the key is pressed, I need to disable the field until the actions are complete. Pressing the *enter ke ...

Using Types as Variables in TypeScript

Currently in the process of writing a TypeScript class factory, my goal is to have a function output a type as its result. While TypeScript handles types as inputs using generics effectively, I am facing challenges when it comes to dealing with types as ou ...

Angular Dynamic Form Troubles

Recently, I followed a tutorial from https://angular.io/guide/dynamic-form to create a dynamic angular form. So far, everything has been working smoothly. However, I am now facing a challenge as I try to implement a question type where users can add more i ...

"Utilizing provideMockStore in NgRx 8 for setting the state of a specific State Slice

I have been working on testing a smart component for my NgRx implementation, and the test setup looks like this: describe( 'Component', () => { let store: MockStore<State>; beforeEach( async( () => { TestBed.configureTesting ...

Steps for clicking on the center of a leaflet map with protractor

I'm currently facing an issue where I am attempting to click on the center of a map located in the second column of the webpage, which has an offset. However, I am encountering a problem where the cursor always points to the center of the page instead ...

generate an object with the forEach method

When I receive data from a graphql query, my goal is to structure an object like the example below: const MY_DATA = [ { id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba', imageUrl: defaultUrl, name: 'Johann', } ...

Simplifying parameter types for error handling in app.use callback with Express.js and TypeScript

With some familiarity with TypeScript but a newcomer to Express.js, I aim to develop a generic error handler for my Express.js app built in TypeScript. The code snippet below is functional in JavaScript: // catch 404 and forward to error handler app.use((r ...

TypeScript application was constructed with incorrect imports from the src folder instead of the dist folder

Summary: App successfully built but importing files from src folder instead of dist I have a TypeScript-powered Express application. This is the configuration of tsconfig.json file: { "compilerOptions": { "target": "es5&quo ...

Tips for integrating Material-Ui Autocomplete with Formik's Field component to create Multi-Select check boxes

Trying to integrate Formik's Field component with Material-Ui Autocomplete for multiple values that include checkboxes has been challenging. Whenever a value is selected from the dropdown list, the popup closes and needs to be reopened for the next se ...

What is the reason for the typescript check failing?

When attempting to check for the existence of an attribute using if statement, I encountered an error. Can anyone explain why this happened? I would appreciate any insights on this issue. ...

Not all of the Error constructors are displayed by TypeScript

My issue is straightforward: I am attempting to utilize the Error constructor that requires a message and an options object. Despite setting my tsconfig.json file to have "target": "es2020", the Intellisense in VS Code only displays one ...

Autocomplete feature in MUI allows filtering to begin after typing at least 3 characters

I've encountered an issue with the Autocomplete MUI component I'm using to filter a list of checkboxes. The popup with options should remain open at all times, but I only want the filtering to be triggered when the user input is more than 3 chara ...

"Error: Retrieving the body data from the Express request

I am having trouble retrieving the JSON data using TypeScript in the req.body. It keeps showing up as undefined or an empty object. const signUpUser = ({ body }: Request, res: Response): void => { try { res.send(body) console.log(body) } cat ...

How to verify that the user is using the most up-to-date version of the application in React Native

Currently, I am focused on the application and have implemented API endpoints that return the latest version along with information on whether the update is mandatory. Within the application flow, a GET request is sent to these API endpoints to check the ...