Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing Color of Way points. #96

Open
Hokino opened this issue Jul 10, 2020 · 5 comments
Open

Changing Color of Way points. #96

Hokino opened this issue Jul 10, 2020 · 5 comments
Labels
featured Specially important new features or corrections.

Comments

@Hokino
Copy link

Hokino commented Jul 10, 2020

Suppose I have an Origin of A and Destination of D with a way point of B and C.

Is it possible to change the stroke color of the waypoint section of the agm-direction.

Something like:
waypoints: [
{
icon: 'your-icon-url',
label: 'marker label',
opacity: 0.8,
polylineOptions: {
strokeColor: '0080ff',
strokeWeight: 6,
strokeOpacity: 0.55
}
},
],

For example could I have :
A -> B (default blue)
B -> C (red)
C -> D (default blue)

I have tried to do multiple directions so that each of the above routes (a -> b, b -> c, c -> d) are its own direction and setting the strokeColor in the renderOptions. But then the markers are not automatically showing as a,b,c,d. Rather they are showing up like this.

image

Please tell me if such a functionality already exists.

Thanks!

@explooosion explooosion added the enhancement Indicates new feature requests label Jul 10, 2020
@explooosion
Copy link
Owner

Hi @Hokino !

There is no way to custom waypoints style now with any waypoint params in GoogleMapAPI - directions#Waypoints. 😞

But I have found this post, you can create your own polylines to replace exist polylines from result.

I have try and it seems to be successful.

Hope to help you.

  • Demo

image

  • HTML
<agm-map [latitude]="lat" [longitude]="lng" (mapReady)="onMapReady($event)">
  <agm-direction
   [origin]="origin"
   [destination]="destination"
   [waypoints]="waypoints"
   [renderOptions]="renderOptions"
   (onResponse)="onResponse($event)"
   >
  </agm-direction>
</agm-map>
  • TS
import { Component } from '@angular/core';
import { Polyline, PolylineOptions, GoogleMap } from '@agm/core/services/google-maps-types';

declare var google: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {

  public lat = 24.799448;
  public lng = 120.979021;

  public origin: any = 'Taichung Railway Station, Taiwan';
  public destination: any = 'Wenxin Forest Park, Taiwan';

  public waypoints: Array<any> = [
    { location: 'Chung Shan Medical University, Taiwan', stopover: true },
    { location: 'National Chung Hsing University, Taiwan', stopover: true },
  ];

  // Hide origin polylines
  public renderOptions = { suppressPolylines: true };

  // Custom polylines
  public polylines: Array<Polyline> = [];

  // Current map
  public map: GoogleMap;

  // Save GoogleMap instance
  public onMapReady(event: GoogleMap) {
    this.map = event;
  }

  public async onResponse(event: any) {

    // Default style
    const polylineOptions: PolylineOptions = {
      strokeWeight: 6,
      strokeOpacity: 0.55,
    };

    // Polylines strokeColor
    const colors = ['#0000FF', '#FF0000', '#0000FF'];

    // Clear exist polylines
    this.polylines.forEach(polyline => polyline.setMap(null));

    const { legs } = event.routes[0];

    legs.forEach((leg, index) => {

      leg.steps.forEach(step => {
        const nextSegment = step.path;
        const stepPolyline: Polyline = new google.maps.Polyline(polylineOptions);

        // Custom color
        stepPolyline.setOptions({ strokeColor: colors[index] });

        nextSegment.forEach(next => stepPolyline.getPath().push(next));

        this.polylines.push(stepPolyline);
        stepPolyline.setMap(this.map);
      });
    });
  }
}

Your suggestion is great, it can be a new feature. 🙂

Ref

@explooosion explooosion added featured Specially important new features or corrections. and removed enhancement Indicates new feature requests labels Jul 10, 2020
@explooosion explooosion pinned this issue Jul 10, 2020
explooosion added a commit that referenced this issue Jul 11, 2020
@Hokino
Copy link
Author

Hokino commented Jul 13, 2020

Hi @explooosion I have another question. Say that the directions are circular. So for the same previous example, if A -> B -> C -> A (D). If we are going in a loop where the origin matches the destination is there a way to prevent the A from becoming labelled with a D pin.

Where
Origin = { latitude: 41.4214, longitude: -112.4768 }
Destination = { latitude: 41.4214, longitude: -112.4768 }
Waypoints = { latitude: 43.6686, longitude: -116.6462}, { latitude: 44.083, longitude: -116.7836 }

My example:
image

Im thinking that maybe i could have a click event that modifies the z-index of the marker selected that would send it to the back. So maybe swap the two values? That way once a user clicks once marker A would be visible and then on another click marker D would be visible?

Does that seem like the correct solution? I looked into marker cluster and spider marker solutions but those dont look like they will work for my purposes.

@Hokino

This comment has been minimized.

@explooosion explooosion added this to the @types/googlemaps milestone Jul 17, 2020
@explooosion explooosion mentioned this issue Jul 17, 2020
Merged
@Hokino
Copy link
Author

Hokino commented Jul 17, 2020

Sorry for all the questions!

I noticed that if i want to change the labels while still having different colored routes that the labels do not behave as expected while the waypoints stopover property is set to false.

In this example i set the [markerOptions] to the below and the labels didnt work as expected.

markerOptions = {
origin: {
label: { color: 'white', text: '1' }
},
destination: {
label: { color: 'white', text: '4' }
},
waypoints: [
{
label: { color: 'white', text: '2' }
},
{
label: { color: 'white', text: '3' }
}
]
};

image

However, if i set stopover to false:
public waypoints: Array = [
{ location: 'Chung Shan Medical University, Taiwan', stopover: false },
{ location: 'National Chung Hsing University, Taiwan', stopover: false },
];

The labels work as expected, but now the different route colors are no longer being changed:
image

My question is, can you have both custom colored routes and custom markers?
Or is it possible to suppress a single marker? Rather than all of them?

EDIT:
I found a way around this by suppressing all markers.
renderOptions = { suppressPolylines: true, draggable: true, suppressMarkers: true };

And then creating individual agm-markers that point to the lat and lngs and have the label text to a letter of the alphabet..

markerList: { lat: number; lng: number; labelText: { text: string; color: string } }[] = [];

Then i call this method to populate the markerList array:
populateMarkerList(mapLocations) {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const alphabetLength = 26;
mapLocations.forEach((element, index) => {
if (index >= alphabet.length) index = index - alphabetLength;
const location = {
lat: element.lat,
lng: element.lng,
labelText: { text: alphabet.charAt(index), color: 'white' }
};
this.markerList.push(location);
});
}

Then I loop through them in the html.

<agm-map
[latitude]="origin ? origin.lat : lat"
[longitude]="destination ? destination.lng : lng"
[scrollwheel]="null"
[gestureHandling]="'cooperative'"
(mapReady)="onMapReady($event)"

<ng-container *ngIf="origin && destination">
<agm-marker
*ngFor="let marker of markerList; let i = index"
[latitude]="marker.lat"
[longitude]="marker.lng"
[label]="marker.labelText"
>

<agm-direction
  [visible]="done"
  [origin]="origin"
  [destination]="destination"
  [waypoints]="waypoints"
  [provideRouteAlternatives]="true"
  [renderOptions]="renderOptions"
  (onResponse)="onResponse($event)"
></agm-direction>

@MarcosPauloSouzaMiranda

Good Morning @Hokino

I have same problem with markers and option stopover in direction, thanks for the solution.

Att,
Marcos Paulo Souza Miranda

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
featured Specially important new features or corrections.
Projects
None yet
Development

No branches or pull requests

3 participants