How can I draw a line between two google map native markers? (Ionic 3, cordova)

jamesharvey

How can I draw a line between two google maps native markers? In my project, I need one dynamic marker and one fixed marker.

addMarker() {
  this.map.addMarker({
    title: 'My Marker',
    icon: 'blue',
    animation: 'DROP',
    position: {
      lat: this.place.lat,
      lng: this.place.lng
    }
  })
  .then(marker => {
    marker.on(GoogleMapsEvent.MARKER_CLICK).subscribe(() => {
      alert('Marker Clicked');
    });
  });
}

2nd marker:

addMarker2() {
          this.map.addMarker({
            title: 'My Marker',
            icon: 'blue',
            animation: 'DROP',
            position: {
              lat: -33,
              lng: 773231
            }
          })
          .then(marker => {
            marker.on(GoogleMapsEvent.MARKER_CLICK).subscribe(() => {
              alert('Marker Clicked');
            });
          });
        }

and how can I show route between the two markers using a line?

Thanks in advance,

DaveAlden

The documentation for the Polyine class for cordova-plugin-googlemaps indicates you would add a Polyline between your markers like this:

let points = [
    {
      lat: this.place.lat,
      lng: this.place.lng
    },
    {
      lat: -33,
      lng: 773231
    }
];

this.map.addPolyline({
    points: points,
    'color' : '#AA00FF',
    'width': 10,
    'geodesic': true
});

This will draw a straight (geodesic) line between the markers.

However in your question you asked "how can I show route between the two markers using a line". A travel route is of course not as simple as drawing a straight line from A to B - if you want to draw a detailed transport route, then it's a bit more complicated:

The Google Maps Directions API enables you to calculate directions between locations and returns a JSON response which contains a detailed travel route, including an encoded polyline of the route which you can draw straight onto your map. The catch is that the Google APIs endpoint doesn't support CORS headers so if you make a request direct from your Cordova app, the Webview will block the response.

So instead I would suggest including the Google Maps Javascript SDK in your Cordova app (even though you're using the native Google Maps plugin to show the maps) since this includes a directions service which can access the Google Maps Directions API and is not inhibited by CORS so can be used in your Cordova app.

Similar to the Google Maps Directions API endpoint, it returns a JSON response detailing a travel route between locations, including an encoded polyline. You can easily decode the polyline using a library such as mapbox polyline then draw the resulting set of coordinates as a Polyline on your native Google Map to show a detailed travel route.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Draw a line between two markers map-box react-native?

How to draw a line between two point in Google map?

How to draw line between the markers in google map, using laravel5.2

How can I draw polyline Between two location in Google Map using Android?

How to draw a route between two markers in Google Maps API?

how to draw a route between two markers in google maps

Draw Lines between Multiple markers on Google Map

How can I draw "empty" circles as a markers for a line in SVG?

Draw small symmetrical arc line path between two markers on world map in Highmaps

how to show path between two markers in google map

How to zoom between two Google map markers in flutter

Multiple markers on Google Map with ionic 3

How to draw a line / link between two points on a D3 map based on latitude / longitude?

How can I map an array of coordinates into React Native Maps Markers?

How can I add markers in my d3 line chart having two datasets

How to connect two markers with a line in Google maps api3

How can I create numbered map markers in Google Maps V3?

How can I delete everything between two markers in a file?

How can i create multiple markers on a google map?

Can't remove markers - Ionic cordova google maps plugin

Draw Lines between Multiple markers on Google Map using Java Script

How can i draw Two line with some angle between them, later can change angle by dragging any line

How to draw a line between two points over an image in swift 3?

How to draw a line between two views in Swift 3?

How do I draw an Ideal line between two headers?

How do I draw a horizontal line between two circles with CSS?

How to move between markers on Google Map?

I am trying to display multiple markers and a line between them on google map

How can I draw a line between Shapes in SwiftUI?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive