How do I incorporate a "Fly to marker" feature into an existing button on Leaflet maps?

I'm currently working on implementing a fly function to a marker when a button is clicked. I've already created buttons on the right side of the map and now I want to add this feature to one of them.

 var latlng = map.getCenter(); // or [47.478058, 9.720154]
var marker = L.marker(latlng).addTo(map);
L.DomEvent.on(button,'click',function(){
    var zoomlvl = 16;
    map.flyTo(marker.getLatLng(),zoomlvl)
});

All I need is to integrate this code into my project --> https://jsfiddle.net/paulito92/p92sfz7o/1/ Specifically, I want this functionality for the Availability Zones button. When that button is clicked, I want the map to navigate to the location [48.6333, 2.2333]. I've been encountering issues while trying to implement this function, so any help would be greatly appreciated!

If anyone has insights on how to achieve this, I'd be very grateful.

Thank you all and have a wonderful day :)

Answer №1

Ensure to add the button event either at the end of the script or right after marker creation. Remember to utilize the myDataPoint variable instead of the undefined marker variable.

  
  var button = document.getElementById('availability')
L.DomEvent.on(button,'click',function(e){
    console.log('Button clicked')
});

L.DomEvent.on(button,'click',function(){
    var zoomlvl = 16;
    map.flyTo(myDataPoint.getLatLng(),zoomlvl)
});

https://jsfiddle.net/falkedesign/f3etmzjn/

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

Module.exports causing keyword errors in eslint

Encountering some unusual errors from eslint CI regarding my jest.config.js file. 1:1 error Rule 'no-empty-label' has been replaced by: no-labels no-empty-label 1:1 error Rule 'no-reserved-keys' has been replaced ...

No data found in Node.js after receiving AngularJS POST request

I've been working on sending a straightforward POST request to my server using AngularJS. The request successfully goes through and reaches the controller on the backend, but strangely, req.data is appearing as undefined. Front End Controller: funct ...

Creating a Stunning Image Collage with Bootstrap and NuxtJS

I'm currently working on creating an image mosaic using a combination of Bootstrap, CSS, and NuxtJS. I have a total of five square images, with the first one needing to be positioned in the center. Specifically, two images should be aligned to the lef ...

Adjusting div container width for better layout

My website is designed with a 640px page-width for mobile devices, like so: html, body { width: 640px; padding: 25px; margin-left: auto; margin-right: auto; } All elements have the same margin on both sides and a padding of 25px. While th ...

A guide to update values in mongodb using node.js

I'm working on tracking the number of visitors to a website. To do this, I've set up a collection in my database using Mongoose with a default count value of 0. const mongoose = require('mongoose'); const Schema = mongoose. ...

Having trouble with displaying a Bootstrap modal in HTML and CSS?

I am new to programming and facing a challenge with my modal not displaying as expected. Instead of appearing in a popup, it is showing on the page itself. I really need help finding a solution for this issue. Any assistance would be highly appreciated. Be ...

Combining JSON Data with Fuzzy Matching

I have a JSON data structure that looks like this { "array": { "InvestmentsDeposits": { "NAME": "Investments & Deposits", "PARENT": [ { "CONTENT_ID": "Promotions", "DISP ...

Steps for extracting information from an html webpage that runs javascript

I'm currently working on a program to extract the IUPACcondensed from this specific website. On this page, G03307GF serves as the ID. The desired information is as follows: HexNAc(b1-?)[Fuc(a1-?)]GlcNAc(b1-2)Man(a1-3)[HexNAc(b1-?)[Fuc(a1-?)]GlcNAc(b ...

I am looking to filter an array to only include products with IDs that match those in the first array

I'm attempting to filter an array to only include products with IDs found in another array. Here's what I've tried so far: let first = [1, 4] let second = [{id: 1}, {id: 2}, {id: 4}] second.filter((es, i) => es.id.includes(first)) https: ...

issue encountered when attempting to execute the function during the on-change event

While attempting to develop a todo app, I encountered an issue where calling the function markComplete on each checked or unchecked checkbox resulted in an error message stating cannot read property 'completed' of undefined. What could be the so ...

Arranging an array in reverse order (starting with underscore) can alter the attributes of the objects within

REVISION: To provide more clarity on the issue, I have recorded a bug using quicktime. Here is the link for your reference: For additional code details, please visit: ORIGINAL INQUIRY: A strange issue has come up while working with Meteor and its depen ...

Remove the initial section of the text and provide the rest of the string

I am a beginner in the world of Javascript and unfortunately I have not been able to find an answer to my current problem. Here is the situation. On a webpage, I am receiving a URL that is sometimes presented in this format: http://url1.come/http://url2.c ...

Vue child component not displaying updates after property is cleared (utilizing Vue.js 3 without any bundler)

Currently, I am diving into learning vue.js version 3.0 and I'm in the process of experimenting with child components without using a build system for Vue. In my project, I pass an array to the child component and then clear it within that component. ...

What is the method for specifying the HTML file extension within Visual Studio?

I am having issues with my project recognizing the CSS and other files in the HTML files I have created, even though I have double-checked the extension paths. How can I resolve this problem? https://i.stack.imgur.com/85ooE.png https://i.stack.imgur.com/F ...

Using JQuery to handle click events on an array of checkboxes and displaying the value of the selected

I am struggling to show the label text and checkbox value from a checkbox array whenever each one is clicked (for testing purposes, assume I want it to appear in an alert). My HTML looks like this: <label class="checkbox"> <input type="checkbox" ...

Change the input field to uppercase using JavaScript but do not use inline JavaScript

Hey there! I have a basic script set up (see below) with an input field allowing users to enter a query. This query is then sent to a socrata webservice to request specific data, which is displayed in an alert box. So far, everything works smoothly. var l ...

Increasing the gap between columns using Bootstrap 4

How I envision it to appear: https://i.sstatic.net/Hrlv4.png I'm facing a challenge in Bootstrap 4 where I want to create space between the columns both horizontally and vertically without disrupting the breakpoints. I have tried adjusting margins i ...

"Troubleshooting: Passing selected checkbox values from jQuery popup to parent window is not functioning correctly

Friendz I am currently working on a JSP project where I am trying to create a pop-up window using jQuery. This pop-up window contains checkboxes that allow users to select values, and upon submitting the form, the selected checkbox values should be displa ...

Change the DER encoded ANS.1 format of an AWS KMS ECDSA_SHA_256 Signature to JWT base64url encoded R || S format using NodeJS/Javascript

I am currently working on generating a JWT Signature in NodeJS using the ES256 algorithm and AWS KMS Customer Managed Keys. However, I have encountered an issue where the signature created by AWS KMS with ECDSA_SHA_256 cryptographic Signing Algorithms is ...

I'm wondering why myDivId.toggle() is functioning properly but myDivClass.toggle() is not working as expected

Using JQuery's toggle() function, I have been able to hide and show some DIVs successfully. Recently, I discovered relationships between certain DIVs that allowed me to group them into a class. I decided to try toggling the entire class rather than ...