Setting up the Leaflet routing feature on an Ionic 2 app

Recently, I set up a basic Ionic 2 project that integrates leaflet with the help of this repository: https://github.com/SBejga/ionic2-map-leaflet. Following that, I ran the command:

sudo npm install --save leaflet-routing-machine

to add leaflet routing machine to my project. Subsequently, in my map.ts file, I included the following code:

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';

import "leaflet";
import "leaflet-routing-machine"

declare var L: any;

/*
  Generated class for the Map page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  selector: 'page-map',
  templateUrl: 'map.html'
})
export class MapPage {
  map: L.Map;
  center: L.PointTuple;

  constructor(public navCtrl: NavController, public navParams: NavParams) {}

  ionViewDidLoad() {
    console.log('ionViewDidLoad MapPage');

    //set map center
    //this.center = [48.137154, 11.576124]; //Munich
    this.center = [48.775556, 9.182778]; //Stuttgart

    //setup leaflet map
    this.initMap();

    L.Routing.control({
      waypoints: [
        L.latLng(48.776, 9.183),
        L.latLng(48.786, 9.193)
      ]
    }).addTo(this.map);

  }

  initMap() {
    this.map = L.map('map', {
      center: this.center,
      zoom: 13
    });

    //Add OSM Layer
    L.tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
      .addTo(this.map);
  }

}

The map loads correctly but the routing feature is not working as expected. Upon checking the console, I noticed the error message:

router.project-osrm.org/route/v1/driving/9.183,48.776;9.193,48.786?overview=false&alternatives=true&steps=true&hints=; Failed to load resource: the server responded with a status of 503 (Service Unavailable: Back-end server is at capacity) localhost/:1 Failed to load ;: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 503. leaflet-routing-machine.js:10477 Routing error: Object

Do you have any idea why the routing isn't functioning properly?

Answer №1

If you want to use Chrome efficiently, make sure to download and activate the Chrome plugin Allow Control allow origin.

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

The Angular change detection mechanism is only triggered once when there are consecutive updates to the Ngrx store

I am facing an issue with Angular change detection in a specific scenario while using ngrx-store to manage my application state. Initially, I have state S1. When action A1 is triggered, the state updates to S2 by a reducer. Subsequently, an effect trigge ...

Application: The initialization event in the electron app is not being triggered

I am facing an issue while trying to run my electron app with TypeScript and webpack. I have a main.ts file along with the compiled main.js file. To troubleshoot, I made some edits to the main.js file to verify if the "ready" function is being called. ...

Leveraging data from a service in Angualr 5 within createServerRenderer

I am currently utilizing the .Net Core Angular CLI based template available at this link. When it comes to server side rendering, this template generates a crucial file named main.server. import 'zone.js/dist/zone-node'; import 'reflect-me ...

The For loop causing crashes in the Filter button functionality

I am currently working on implementing a buy it now only filter button for listings that allow that option. However, I am facing an issue where the app crashes when the button is clicked due to a for loop in my code. Strangely, if I remove the for loop, ...

What is the primary purpose of the index.d.ts file in Typescript?

There are some projects that include all types declarations within the index.d.ts file. This eliminates the need for programmers to explicitly import types from other files. import { TheType } from './somefile.ts' Is this the proper way to use ...

Difficulty updating Angular packages using NCU (npm-check-update)

I attempted to update everything on my project by running the following commands: G:\Projects\salarkazazi> npm i -g npm-check-updates The output displayed the following warnings: npm WARN deprecated [email protected]: this library is n ...

Quill editor fails to trigger the (change) event when I modify the toolbar settings

I am facing an issue with capturing Quill editor events. The code snippet below shows how I am trying to capture the event, but when I change something using the toolbar, the event is not captured. Can someone please help me understand how to get this ev ...

What is the process for integrating a third party API certificate into my Heroku application?

When my backend service calls a third-party API (Kamer van koophandel) to retrieve data, it requires a certificate to be set. It functions correctly locally, but upon pushing to Heroku, the following error occurs: Warning: Ignoring extra certs from `Privat ...

The horizontal scrollbar in PrimeNG's p-tree is cleverly concealed until I reach the bottom of the div and start scrolling

I'm facing an issue where the horizontal scrollbar in my p-tree component is hidden until I scroll all the way down. I've tried various CSS modifications, but nothing seems to work. It's surprising that I couldn't find anyone else with ...

Error encountered with npm version 5.8.0 while trying to create a new project using ng new

I keep encountering this error whenever I try to create a new Angular project. I'm unsure whether it is an npm or angular-cli issue. https://i.sstatic.net/MJ0ek.png node -v v8.11.1 npm -v 5.8.0 ng -v 6.0.0 Everything seemed fine before this occur ...

"Production environment encounters issues with react helper imports, whereas development environment has no trouble with

I have a JavaScript file named "globalHelper.js" which looks like this: exports.myMethod = (data) => { // method implementation here } exports.myOtherMethod = () => { ... } and so forth... When I want to use my Helper in other files, I import it ...

Angular Navigation alters the view

I want to navigate to a different page using Angular routing, but for some reason it's not working. Instead of moving to the designated Payment Component page, the content is staying on my Main Component. Why is this happening? app.routing.module.ts ...

Angular design featuring numerical staircase pattern

I'm attempting to implement a numeric version of the staircase pattern using Angular, but I have yet to find the solution. Here is the desired outcome: https://i.sstatic.net/KTU3k.png Below is the code I have developed thus far: main.ts import { Co ...

How do you go about making a prop optional in Typescript based on a generic type?

In my app, I have a specific type with optional props based on a generic type: type MyCustomType<R extends Record<string, string> | undefined, A extends string[] | undefined> = { record: R, array: A } There is a function that directly uses ...

Responsive column layout with Angular Material 2's md-card

Currently, I am on the hunt for an Angular 2/4 example showcasing a specific layout. Essentially, I want to initiate with two columns of cards, with two cards in the left column and one card in the right column. However, when the screen size is reduced, I ...

Ways to maximize your final export value

My data file, named data.ts, contains a large dataset: export data = [ ... // huge data ] The lib.ts file only utilizes a portion of the data: import { data } from './data.ts'; const fitteredData = data.splice(0,2) // only use some of them ...

Explore the potential of utilizing Reactive Forms in conjunction with storybook templates

Currently, I am working on developing some custom components and form elements that I intend to include in Storybook. To ensure completeness, I want the stories to utilize FormControl and FormGroup to demonstrate a real-world use case. Here is an example ...

Tips for properly narrowing a function parameter that includes "an object key or a function"

Working on a function to retrieve a property using either a string key or a callback, I've encountered an issue with TypeScript narrowing the type parameter. Here is the function in question: function get<T, V>(value: T, fn: (value: T) => V) ...

What's the best way to show a submenu when the main menu is scrollable?

I'm facing a challenge with the following issue: I am working with angular 8 and utilizing tieredMenu from primeng 8. The problem arises when I have a scroll, causing the tieredMenu's submenu to be hidden. Do you have any suggestions on how to ...

Encountering a timeout issue with the Sinch API within an Angular 2 project during the onCallProgressing

We successfully integrated Sinch into our angular 2 web application. Most functionalities are working perfectly, except for the user calling feature using the sinch phone demo. When the application is in the foreground, the call rings and connects withou ...