Guide on transferring the marker icon between two geographic coordinates through Leaflet OpenStreetMap within an Angular environment, sourced from the API

In my Angular application, I have integrated the Leaflet open street map for displaying a map. The latitude and longitude array has been retrieved from an API as follows:

{
"Drone": {
    "Droneid": 1001,
    "latlong": [
        {
            "lat": 12.989839,
            "lon": 80.198822
        },
        {
            "lat": 13.051832,
            "lon": 80.194480
        },
        ...
    ]
}
}

Using this array, I have placed a circle of a 5 km radius with the first set of latitude and longitude values (index 0) representing the center. The drone icon is placed at index 1, and the rest of the dots on the map are displayed using the remaining latitude and longitude values.

Now, I need to move the drone icon from one set of coordinates to another one corresponding to the dots fetched from the API.

Dashboard.component.ts

 var map = L.map('map').setView([13.0827, 80.2707], 11);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
 attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
} ).addTo(map);
this.drones.Drone.latlong.forEach((latlong, idx)=>{
  
var latlng = L.latLng(latlong.lat,latlong.lon)
if(idx === 0){
  L.circle(latlng,{radius:5000}).addTo(map);
}else if(idx===1){
 L.marker([latlong.lat,latlong.lon],{icon:myIcon}) .addTo(map)
}
else if(idx>=2){
  L.circle(latlng,{radius:20}) .addTo(map)
}
})

I am seeking guidance on how to animate the movement of the drone icon (index = 1) along the dots on the map starting from index[2] until the end of the array fetched from the API.

If anyone can assist me with this, it would be greatly appreciated.

Answer №1

Modify the latlng of the marker using setTimeout

var TIME = 1000; // 1000 milliseconds = 1 second
var latlngs = this.drones.Drone.latlong;
var START_IDX = 2;
var latlngIdx = START_IDX; // 0 = Circle, 1 = First position
var marker;

latlngs.forEach((latlong, idx)=>{
    var latlng = L.latLng(latlong.lat,latlong.lon)
    if(idx === 0){
      L.circle(latlng,{radius:5000}).addTo(map);
    }else if(idx===1){
      marker = L.marker(latlng,{icon:myIcon}).addTo(map)
    }else if(idx>=2){
      L.circle(latlng,{radius:20}) .addTo(map)
    }
});

function updateLatlng(){
    if(marker){
        if(latlngIdx === latlngs.length){ // Start at idx 2 if last idx is reached
            latlngIdx = START_IDX;
        }
        marker.setLatLng(latlngs[latlngIdx]);
        latlngIdx++;
        setTimeout(updateLatlng,TIME); // call updateLatlng() after 1000 ms
    }
}
updateLatlng();

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

How can I retrieve specific URL parameters from the in-memory web API in Angular 2?

To retrieve data for a specific unit ID, I am using a parameter called "UnitID" in the following code: this.unitDetailsService.getUnitDetailsbyId(this.activeUnitId) I am utilizing the activeUnitId parameter to generate a URL for an in-memory service with ...

Adjusting the alignment of button text in an Angular Kendo grid

I am currently utilizing the grid view feature of Kendo UI for Angular. While I have buttons on the grid, the issue is that the text within the buttons is not centered properly despite applying styles such as text-align:center. Here is the template for my ...

Submitting Additional Information Using Angular 8 and C# Core 3 File Upload Feature

After finally managing to successfully upload files from the Angular 8 frontend to the C# .Net Core 3.0 backend API Controller, I encountered an issue where passing in a separate class along with the files caused an error. The additional class is meant to ...

Having difficulties initiating NPM on an Angular project

I've been grappling with this issue for weeks now. I can't seem to get this project up and running after cloning it from the repository. When I try "npm start" Could not locate module "@angular-devkit/build-angular" from ... When I try "npm i" ...

What is the process for generating an alert box with protractor?

While conducting tests, I am attempting to trigger an alert pop-up box when transitioning my environment from testing to production while running scripts in Protractor. Can someone assist me with this? ...

The error TS2300 arises from the duplicate identifier 'PropertyKey' found in the node_modules/@types/core-js/index.d.ts file

I am encountering errors in the file node_modules/@types/core-js/index.d.ts while using Visual Studio Code IDE: https://i.sstatic.net/fkAej.png After running npm start to serve the application, the following errors are displayed: (list of errors her ...

Error in Angular Apollo V3: Jest unable to locate module 'apollo-angular'

After recently updating angular apollo from version 2.6.0 to v3.0.0, my tests have been broken. I use jest for unit testing, and although the application compiles and runs without any issues, my tests cannot import any dependencies from 'apollo-angul ...

Error: The data type '(number | undefined)[]' cannot be converted to type 'number[]'

Transitioning to Typescript in my NextJS application has presented a challenge that I cannot seem to overcome. The error arises on the line value={value} within the <Slider.Root> The variable value comprises of two numeric elements: a min and a max. ...

ESLint encountering a "Module path unresolved" issue in conjunction with SAM Serverless Monorepo

In the process of developing a basic Hello World function for AWS Lambda/APIGateway, I aim to integrate ESLint and Typescript features. Typically, when working with Typescript and ESlint, inclusion of the eslint-plugin-import package and specifying the ext ...

Is there a way to prevent a mat-menu from closing when clicking on a mat-toggle button?

Whenever I click on one of the toggle buttons, the mat-menu automatically closes. How can I prevent this behavior so that the menu only closes when the user clicks somewhere else? <button [matMenuTriggerFor]="popoverMenu">Menu</bu ...

Enhance Angular Material UI accessibility and implement automatic value checking on load

I am facing an issue with an angular material drop down selector that offers multiple options, including a feature to select all options at once. https://i.stack.imgur.com/VyXxz.png I came across a code snippet on this website, which I found in r ...

Whenever I try to run ionic serve, an error pops up saying: "typescript: node_modules/@types/node/worker_threads.d.ts, line: 8 '=' expected."

My Ionic Info: cli packages: (/usr/local/lib/node_modules) @ionic/cli-utils : 1.9.0 ionic (Ionic CLI) : 3.9.0 Global Packages: Cordova CLI : 9.0.0 (<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec8f839e88839a8dc180858eacd5c2 ...

What is the process for defining the type or interface of an object in Visual Studio Code?

Is there a way to create a new type or interface by replicating the structure of a complex object that is imported from a library? For instance, in the image below, the object Text is taken from react-three/drei. https://i.sstatic.net/BcUzd.png Upon inspe ...

Emails are failing to send through NodeMailer, specifically on the production server

When creating a SMTPtransporter to send emails, I encountered an issue when deploying the code on a server. The code works perfectly fine on my computer with Yarn version '1.22.17'. import * as nodemailer from 'nodemailer'; import * as ...

Android causing multiple triggerings of pause and resume events in Ionic 3

Encountering a peculiar problem with the Ionic 3 pause and resume events. Every time I pause the application, the events trigger multiple times (2, 3 times, etc). Here is the snippet of code: this.platform.ready().then(() => { this.platform.pause.sub ...

Tips on setting up tsconfig.json for type declarations not found in @types?

When setting up my app, I am using a tsconfig.json file to specify which typings should be used. { "compilerOptions": { "types" : ["node", "lodash", "express"] } } This configuration allows me to import typings from ./node_modules/@types/nod ...

Unable to adjust the size of modal popup

I'm currently attempting to adjust the dimensions of the dialog box in the example below. I'm unsure if this is controlled by CSS or specific properties of the dialog box itself. Here's the link for reference: StackBlitz Example I'm wo ...

Ignore unused variables in typescript with eslint disable

When working on a Typescript React project, I often use placeholder variables in the code to lay everything out before implementation. However, this results in numerous eslint no-unused-vars errors, making it difficult to spot real errors. Is there a way ...

Editify Angular Text:Note: The name 'Edit

I am currently working on a coding project that involves allowing a user to click a button on the screen and view a paragraph that they can edit. I removed the readonly part of my code, but the paragraph is still not editable. How can I make it so that the ...

What could be causing the service method in the controller not to be called by Node JS?

In my current Node JS project, the folder structure of my app is as follows: src │ index.js # Main entry point for application └───config # Contains application environment variables and secrets └───controllers # Hou ...