Issue with expanding TileLayer using TypeScript 2.0 and Angular 2: Unable to implement Leaflet Leaflet

Trying to incorporate the Microsoft Map API into my app has been a challenge, especially when I encounter errors like the one below when calling L.tileLayer.extend:

Here is the snippet of my code:

import { Component, OnInit } from '@angular/core';
import '../../public/css/styles.css';
import * as L from 'leaflet';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  public map: any;
  public poiLayer: any;

public _quadKey(x:any, y:any, z:any):string {
            var quadKey:any = [];
            for (var i = z; i > 0; i--) {
                var digit:any = '0';
                var mask = 1 << (i - 1);
                if ((x & mask) != 0) {
                    digit++;
                }
                if ((y & mask) != 0) {
                    digit++;
                    digit++;
                }
                quadKey.push(digit);
            }
            return quadKey.join('');
        }
  ngOnInit() {
  this.map = L.map(name, {
            center: L.latLng(46.72139, 2.51028), /*St Amand Montrond */
            zoom: 5,
            maxZoom: 18,
            zoomControl: false
        });

        var BingLayer = L.tileLayer.extend({
            getTileUrl: function (tilePoint:any) {
                return L.Util.template(this._url, {
                    s: this._getSubdomain(tilePoint),
                    id: this.options.id,
                    q: this._quadKey(tilePoint.x, tilePoint.y, this._getZoomForUrl())
                });
            },
            _quadKey: function (x:any, y:any, z:any) {
                let quadKey:any = [];
                for (var i = z; i > 0; i--) {
                    let digit = 0;
                    let mask = 1 << (i - 1);
                    if ((x & mask) != 0) {
                        digit++;
                    }
                    if ((y & mask) != 0) {
                        digit++;
                        digit++;
                    }
                    quadKey.push(digit);
                }
                return quadKey.join('');
            }
        });
        //http:\/\/ecn.t{s}.tiles.virtualearth.net\/tiles\/{id}{q}.jpeg?g=414&mkt=fr
        //http://t{s}.tiles.virtualearth.net/tiles/{id}{q}.jpeg?g=1398&dpi=d1&device=mobile
        let bing = 'http://t{s}.tiles.virtualearth.net/tiles/{id}{q}.jpeg?g=1398&dpi=d1&device=mobile';
        let dmic = '<a href="http://www.dmic.fr"><img height="50px" src="../../../res/img/logo_dmic_small.png" /></a>';
        this.poiLayer = L.layerGroup();
        this.poiLayer.addTo(this.map);
        let layers = {
            'Routière': new BingLayer(bing, { attribution: dmic, id: 'r', subdomains: ['0', '1', '2', '3', '4'], detectRetina: true }),
            'Satellite': new BingLayer(bing, { attribution: dmic, id: 'a', subdomains: ['0', '1', '2', '3', '4'], detectRetina: true })
        };

        let overlays = {
            'Traffic': L.tileLayer('http://trafficservices2.v-trafic.com/mapServer/?mode=tile&tilemode=gmap&mr=cr_trafic_wms.map&layers=fra_rc&tile={x}+{y}+{z}'),
            'Repères': this.poiLayer
        };
        this.map.attributionControl.options.prefix = false;
        layers[Object.keys(layers)[0]].addTo(this.map);
        L.control.layers(layers, overlays).addTo(this.map);
  }
}

This particular script functions seamlessly with Angular 2 and TypeScript 1.

Yet, I seem to encounter issues extending my tileLayer using TypeScript 2.0. Any ideas on resolving this?

Answer №1

Following further testing, I decided to replace the index.d.ts link with the Leaflet provided by TypeScript 2 instead of the one provided by typings. This change resulted in successful functionality, indicating that using npm install --save-dev @types/leaflet did not provide the correct declaration file.

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

Having trouble loading image on web application

Currently, I am facing an issue while attempting to add a background image to an element within my Angular web application. Strangely enough, the relative path leading to the image seems to be causing my entire app to break. https://i.stack.imgur.com/b9qJ ...

Updating a subscribed observable does not occur when pushing or nexting a value from an observable subject

Help needed! I've created a simple example that should be working, but unfortunately it's not :( My onClick() function doesn't seem to trigger the console.log as expected. Can someone help me figure out what I'm doing wrong? @Component ...

Mastering the art of navigating through intricate nested properties within JSON data structures

Presented below is a dynamic JSON structure: data = { "name": "deltha", "type": "object", "important": [ "name", "id", "number" ], "information": { "place": { "editable": false, "visible": true }, "info": { ...

What is preventing me from loading a module using a variable containing a string?

This code snippet demonstrates how the module can be successfully loaded using import('test'), but not with the second example. These lines of code are executed within an Angular 9 application utilizing the default Webpack configuration. var tes ...

What is the best way to adjust the size of an Angular Material Slider?

I'm attempting to insert a slider into my program, but the slider is displaying as too lengthy for the designated space. I'm curious if there's a straightforward method of adjusting its length to fit better within the container. So far, I&a ...

Using Selenium WebDriver to Perform Drag and Drop Functionality on an Angular/Material Design Website

I've been attempting to automate the drag and drop event using Selenium Java WebDriver on a website built with Angular/Material design. Despite trying numerous approaches, none have been successful - there are no error messages, but the dragging and d ...

What is the method for copying table data, including input text as cells, to the clipboard without using jQuery?

I have a basic table that looks like this: <table> <thead> <tr> <th>Savings</th> </tr> </thead> <tbody> <tr> <td>Savings <button type="button" (click)=" ...

The function service.foo is not recognized in Angular

My service function is not being recognized by my component import { Injectable } from '@angular/core'; import { ToastController } from '@ionic/angular'; @Injectable({ providedIn: 'root' }) export class LocationService { ...

The element is inferred to have the 'any' type. No index signature matching the parameter type 'string' was found on the 'User1' type

I have been experimenting with computed properties in TypeScript, but I've encountered a specific issue: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'User1'. ...

Angular provides the capability to sort through data using various criteria

I have received an array of objects in a specific format from the server, which may contain more than 2 objects. [ {processId : 1, processName : "process1", country : "germany", companyCode:"IB" , companyHiringType:"FRE", masterClauses:[ {cl ...

Incorporating an Angular 2 Directive within the body tag of an

My goal is to create a custom directive that can dynamically add or remove a class from the body element in HTML. The directive needs to be controlled by a service, as I want to manage the visibility of the class from various components. Question: How ca ...

add headers using a straightforward syntax

I'm attempting to append multiple header values. This is what I'm currently doing: options.headers.append('Content-Type', 'application/json'); options.headers.append('X-Requested-By', 'api-client'); ... ...

The Influence of Getter Performance in Angular Templates

As I delve into an existing Angular application, I've noticed a pattern where values used in templates across many components are actually properties that are being accessed through getters and setters without any additional logic: <input type="nu ...

Leverage the specific child's package modules during the execution of the bundle

Project Set Up I have divided my project into 3 npm packages: root, client, and server. Each package contains the specific dependencies it requires; for example, root has build tools, client has react, and server has express. While I understand that this ...

collections of Angular2 rc scripts

I am currently in the process of migrating my app from version 0.17 to RC 0 and I have encountered some challenges. In version 0.17, we used the following scripts (bundles): <script src="node_modules/angular2/bundles/angular2-polyfills.js"></scri ...

Tips on leveraging separate files for classes in TypeScript

I'm currently working on developing a TypeScript application where each class is saved in its own .ts file. I prefer to use VS Code for this project. So far, my compile task seems to be functioning correctly (transpiling .ts files into .js files). How ...

The issue arises when attempting to send requests from an Ionic 5 project on the iOS platform, as the session does not seem to be functioning properly. Strang

On the backend, I have set up a Java server and linked it to my ionic 5 project if(origin.contains("ionic")) { httpresponse.setHeader("Access-Control-Allow-Origin", "ionic://localhost"); } else { httpre ...

Getting the Class name in Typescript

How can you retrieve the class name from within a Class in typescript? For instance, consider this code snippet: export class SomeRandomName extends AbstractSomething<SomeType> implements OnDestroy { className = 'SomeRandomName'; Is th ...

Custom fields can be included in the `package.json` file during the build process in an Angular application

My Angular application is up and running smoothly, with a handful of dependencies listed in its Package.json file: { "name": "angular-app", "version": "1.0.0", "scripts": { "ng": "ng ...

What is the best approach to implement server-side rendering in Next.js while utilizing Apollo React hooks for fetching data from the backend?

I have a Next.js project that is utilizing Apollo GraphQL to retrieve data from the backend. My goal is to render the page using server-side rendering. However, I am encountering an obstacle as the React hooks provided by GraphQL Apollo prevent me from cal ...