How can I personalize a Leaflet popup with image thumbnails and additional customization options?

I've been researching and trying out different solutions, but so far none of them have worked for me. My goal is to dynamically add a title, image, address, and name to popups on my leaflet map as data comes in. However, I'm experiencing some challenges with this task.

Below is the TypeScript code that I have:

refresh() {
  this.artworkService.retrieveAll().then((artworkList) => {
    this.artworkList = artworkList;
    for (let artwork of this.artworkList) {
      var popupInfo = "<span class='test'>" + artwork.name + "</span>" + "<br/>" + artwork.filename;
      console.log(artwork.name);
      L.marker([artwork.latitude, artwork.longitude], this.markerIcon).addTo(this.map)
      .bindPopup(popupInfo);
    }
  });
}

In the code above, I tried wrapping the title inside a span element and attempted to apply some CSS styling to it, but unfortunately, it didn't work as expected. Any suggestions or tips on how to achieve this would be highly appreciated!

Answer №1

Most likely, the markers are not appearing because you may have incorrectly declared the markerIcon. The correct way to declare it would be as follows:

markerIcon = {
    icon: L.icon({
      iconSize: [25, 41],
      iconAnchor: [10, 41],
      popupAnchor: [2, -40],
      // specify the path here
      iconUrl: "https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="620e0703040e071622534c564c52">[email protected]</a>/dist/images/marker-icon.png",
      shadowUrl: "https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="68040d090e040d1c2859465c4658">[email protected]</a>/dist/images/marker-shadow.png"
    })
};

In addition, if you want to apply custom CSS to the popup, you should provide popupOptions in the bindPopup method or create a L.popup and assign a classname. You can also refer to this for more information. One way to do this is:

const popupOptions = {
  className: "test"
};
for (let artwork of this.artworkList) {
  const popupInfo =
    "<span class=''>" +
    artwork.name +
    "</span>" +
    "<br/>" +
    artwork.filename;
  console.log(artwork.name);
  L.marker([artwork.latitude, artwork.longitude], this.markerIcon)
    .addTo(this.map)
    .bindPopup(popupInfo, popupOptions);
}

Don't forget to include the style in your global style.css file.

For example:

.test .leaflet-popup-content-wrapper {
      background: #2ce897;
      color: #eee;
      font-weight: bold;
      font-size: 12px;
      line-height: 24px;
      border-radius: 0px;
}

Here is a live demo showcasing an example similar to your use case.

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

typescript throwing an unexpected import/export token error

I'm currently exploring TypeScript for the first time and I find myself puzzled by the import/export mechanisms that differ from what I'm used to with ES6. Here is an interface I'm attempting to export in a file named transformedRowInterfac ...

Using "array_agg" in a "having clause" with Sequelize

I am facing a particular scenario with my database setup. I have three tables named computers, flags, and computerFlags that establish relationships between them. The structure of the computerFlags table is as follows: computerName | flagId computer1 | ...

TypeScript encountered an error with code TS2554, indicating that it was expecting 0 arguments but instead received 1 in an Ionic application

Hello everyone! I'm encountering an issue in my project involving a Type Script error TS2554: Expected 0 arguments, but got 1. This error is preventing me from being able to select other options for custom input pop up. In this forum post, I have shar ...

Error Encountered | Invalid Operation: Unable to access attributes of undefined (referencing 'CodeMirror')

Error image on chrome Using Next.js 13 Encountering an error on Google Chrome, seeking a solution to fix it or possibly just ignore it. Not utilizing Codemirror and prefer not to use it either. Tried troubleshooting methods: Deleting ".next","node_ ...

Exploring the Yahoo Finance Websocket with Angular

Upon visiting the Yahoo finance website (for example, ), I noticed that the page establishes a WebSocket connection. The URL for this WebSocket is wss://streamer.finance.yahoo.com/ I'm currently working on a small project where I need to retrieve dat ...

How to implement a responsive menu using the onPress attribute of TouchableOpacity

Looking to implement a profile picture upload feature with the ability to choose between getting an image from the camera (using getMediaFromCamera) or selecting one from the gallery (using getMediaFromImageLibrary). I currently have a TouchableOpacity set ...

Issue with setting values in Angular2 when using IE 11 and shims included

First off, I want to clarify that I have indeed added the shims in my index.html file. <script src="node_modules/es6-shim/es6-shim.min.js"></script> <script src="node_modules/systemjs/dist/system-polyfills.js"></script> <script ...

How to Establish an Angular Global Variable

What is the process for establishing a global variable in Angular? I have established a variable within a service, entered a value in the login component, and attempted to access this variable from another component. However, I noticed that the value res ...

Tips on updating checkbox background color after being selected

I've been working on creating checkboxes for seat selection in Angular using a TypeScript file, but I'm facing an issue where the background color of the checkbox doesn't change after checking them. Here's my TypeScript function: gener ...

Utilizing Angular Forms for dynamic string validation with the *ngIf directive

I have a challenge where I need to hide forms in a list if they are empty. These forms contain string values. I attempted to use *ngIf but unfortunately, it did not work as expected and empty fields are still visible in the list. How can I address this iss ...

Sorting TypeScript types by required properties first

Can anyone recommend a plugin or ESLint rule that can automatically sort types by assuming required fields come first, followed by optional fields? Here's an example before: type TExampleSorting = { title?: string; value?: number; text: string; ...

developing a collection of Material UI text fields

My goal is to construct an accordion containing several textfield mui-components. I have developed a unique render function incorporating all the essential tags and syntax for creating a text field component. Now, I am looking to generate an array of text ...

Struggling with the functionality of Angular Material Layout Directives

Looking to implement the Child-Alignment feature in Angular Material but running into some issues. Details available here: https://material.angularjs.org/latest/layout/alignment Despite trying to import import { LayoutModule } from '@angular/cdk/l ...

I'm encountering an error in TestCafe that says "TypeError: Cannot read properties of undefined (reading 'match')". Which specific segment of my code is causing this issue?

retrieveUrlFromEmailData(emailData:any){ const emailContent = emailData.email_text; const urlPattern = /(https?:\/\/[^\n]*)/; const foundUrl = emailContent.match(urlPattern)[0]; return foundUrl } ...

Executing a function when the date changes in ng2-datepicker

Currently, I am incorporating ng2-datepicker into my project and the corresponding HTML code appears as follows: <datepicker [(ngModel)]="selectedDate"></datepicker> I am uncertain about how to trigger a function when the date is modified ...

Utilizing Angular formControl validators that are interdependent on other formControls

I am currently working on creating a form that includes two dates: dateFrom and dateTo. The validation requirement is that dateFrom must not come after dateTo, and dateTo must not come before dateFrom. To meet this condition, I have set up a form group wi ...

Learn how to display a tooltip for every individual point on a Highcharts network graph within an Angular

I am currently working on developing network graphs using highcharts and highcharts-angular within my Angular application. I have successfully managed to display the graph with datalabels, but now I need to implement tooltips for each point or node on the ...

A guide on implementing the intl-tel-input plugin within an Angular 2+ project

Component : ng2-tel-input, Framework : Angular 4, JavaScript library : intl-tel-input Upon completing the installation with npm i ng2-tel-input I stumbled upon a note in the node_modules\intl-tel-input\src\js\intlTelInput.js file that ...

Detecting store changes in a component after dispatching an action in NgRx unit testing can be done

Even after dispatching an action to change the state from 'light' to 'dark', the component is still not reflecting this change and remains as 'light'. Is there a way to detect this inconsistency in the component? Below is the ...

Create boilerplate code easily in VS Code by using its feature that generates code automatically when creating a

Is there a way to set up VS Code so that it automatically creates Typescript/React boilerplate code when I create a new component? import * as React from "react"; export interface props {} export const MyComponent: React.FC<props> = (): J ...