angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

Directions service

Open ramnes opened this issue 10 years ago • 54 comments

Hi,

Do you have any plan to support the directions service?

ramnes avatar Jul 08 '16 16:07 ramnes

Same question for me ..

Can we use the directions service with angular2-google-maps ?

-> directions service -> example

Thank you very much for your work -- Chris

christophechevalier avatar Oct 17 '16 15:10 christophechevalier

I too would like to know if this is something I can access. I'm working on a rather map heavy project that requires one of the maps to show Google's suggested directions between two locations. Please let me know if I've over looked the documentation and just not found it. Either way, this project is fantastic! Thanks for your work @SebastianM

amourinho avatar Nov 06 '16 20:11 amourinho

This directive can solve your needs

import {GoogleMapsAPIWrapper} from 'angular2-google-maps/core/services/google-maps-api-wrapper';
import { Directive,  Input} from '@angular/core';
declare var google: any;



@Directive({
  selector: 'sebm-google-map-directions'
})
export class DirectionsMapDirective {
  @Input() origin;
  @Input() destination;
  constructor (private gmapsApi: GoogleMapsAPIWrapper) {}
  ngOnInit(){
    this.gmapsApi.getNativeMap().then(map => {
              var directionsService = new google.maps.DirectionsService;
              var directionsDisplay = new google.maps.DirectionsRenderer;
              directionsDisplay.setMap(map);
              directionsService.route({
                      origin: {lat: this.origin.latitude, lng: this.origin.longitude},
                      destination: {lat: this.destination.latitude, lng: this.destination.longitude},
                      waypoints: [],
                      optimizeWaypoints: true,
                      travelMode: 'DRIVING'
                    }, function(response, status) {
                                if (status === 'OK') {
                                  directionsDisplay.setDirections(response);
                                } else {
                                  window.alert('Directions request failed due to ' + status);
                                }
              });

    });
  }
}

davidpestana avatar Nov 18 '16 00:11 davidpestana

hey david, thanks for the directive, but i'm having a hard time including it in my code. Should I insert it right in the google map dom. For instance: <sebm-google-map sebm-google-map-directions [origin=x] [destination=y][zoomControl]="false" [latitude]="lat" [longitude]="lng" (mapClick)="mapClicked($event)"> </sebm-google-map>

I'm having a hard time making it work, I'm actually really new to angular 2... thanks for your help

ahardworker avatar Nov 21 '16 17:11 ahardworker

Hi @ahardworker , This is the right way:

<sebm-google-map>
  <sebm-google-map-directions [origin]="origin" [destination]="destination"></sebm-google-map-directions>
</sebm-google-map>

in your component controller class you must be define this properties.

origin = { longitude: 4.333, lattitude: -1.2222 };  // its a example aleatory position
destination = { longitude: 22.311, lattitude: -0.123 };  // its a example aleatory position

davidpestana avatar Nov 22 '16 10:11 davidpestana

Thanks! Its working. But I wonder is it possible to dynamically add waypoints, then update the direction without refreshing the page?

Actually here is what I got : I added to my component :

@ViewChild(DirectionsMapDirective) vc:DirectionsMapDirective; and then I simply call vc.updateDirections() from the component whenever a new waypoint is added. Which will call back the code in the directive. Is it the right way to do it?

ahardworker avatar Nov 22 '16 21:11 ahardworker

Sure @ahardworker, waypoints is Array of positions, you can add logic at the directive or inject this array using a input parameter.

for example...

<sebm-google-map>
  <sebm-google-map-directions [origin]="origin" [destination]="destination" [waypoints]="waypoints"></sebm-google-map-directions>
</sebm-google-map>

Directive:

@Directive({
  selector: 'sebm-google-map-directions'
})
export class DirectionsMapDirective {
  @Input() origin;
  @Input() destination;
  @Input() waypoits;
  constructor (private gmapsApi: GoogleMapsAPIWrapper) {}
  ngOnInit(){
    this.gmapsApi.getNativeMap().then(map => {
              var directionsService = new google.maps.DirectionsService;
              var directionsDisplay = new google.maps.DirectionsRenderer;
              directionsDisplay.setMap(map);
              directionsService.route({
                      origin: {lat: this.origin.latitude, lng: this.origin.longitude},
                      destination: {lat: this.destination.latitude, lng: this.destination.longitude},
                      waypoints: waypoints,
                      optimizeWaypoints: true,
                      travelMode: 'DRIVING'
                    }, function(response, status) {
                                if (status === 'OK') {
                                  directionsDisplay.setDirections(response);
                                } else {
                                  window.alert('Directions request failed due to ' + status);
                                }
              });

    });
  }
}

davidpestana avatar Nov 23 '16 08:11 davidpestana

Hello @davidpestana I tried to include the Directions-Directive but I'm getting this error: Can't bind to 'origin' since it isn't a known property of 'sebm-google-map-directions'.

(I don't know, why the formatting is that bad but I hope you manage to read it anyway)

HTML <sebm-google-map [latitude]="lat" [longitude]="lng" [zoom]="zoom"> <sebm-google-map-marker [latitude]="latHome" [longitude]="lngHome" [title]="'titleHome'"</sebm-google-map-marker> <sebm-google-map-marker [latitude]="latComp" [longitude]="lngComp" [title]="titleComp"</sebm-google-map-marker> <sebm-google-map-directions [origin]="origin" [destination]="destination"></sebm-google-map-directions> </sebm-google-map>

Component `@Component({ selector: 'google-maps', styles: [require('./googleMaps.scss')], template: require('./googleMaps.html'), }) export class GoogleMaps { latHome: number = 48.151839; lngHome: number = 13.831726; latComp: number = 48.329500; lngComp: number = 14.319765; titleComp: string = "dynatrace"; zoom: number = 10;

lat: number = (this.latComp + this.latHome) / 2; lng: number = (this.lngComp + this.lngHome) / 2;

origin = { longitude: 4.333, lattitude: -1.2222 }; destination = { longitude: 22.311, lattitude: -0.123 };
}`

Do I have to register the directive somewhere?

bene-starzengruber avatar Nov 28 '16 23:11 bene-starzengruber

@ProkerBen Yes, you need to register the directive in the module you're using it as a declaration, just like any other directive you use.

lazarljubenovic avatar Nov 29 '16 00:11 lazarljubenovic

Yes @ProkerBen, how @lazarljubenovic saids ,you must need register the directive in your module

import { CustomMapDirective } from '[your route to directive folder]/google-map.directive';
...
@NgModule({
  imports: [...],
  exports: [..],
  declarations:[...,CustomMapDirective]
})

davidpestana avatar Nov 29 '16 12:11 davidpestana

@davidpestana DirectionsMapDirective displays the route but It doesn't zoom-in although I had set zoom=17

nhtua avatar Nov 29 '16 15:11 nhtua

its a google api property, when using DirectionsDisplayService, map automaticly zooms in order to view all waypoints. if you set a Zoom Value,it will be ignored.

davidpestana avatar Nov 29 '16 15:11 davidpestana

@davidpestana ya! I tried the pure func of GMapSDK and it works well.

}, function(response, status) {
  if (status === 'OK') {
    directionsDisplay.setOptions({ preserveViewport: true });
    directionsDisplay.setDirections(response);
  } else {
    window.alert('Directions request failed due to ' + status);
  }

directionsDisplay.setOptions({ preserveViewport: true }) force the map keeps zoom value

nhtua avatar Nov 29 '16 15:11 nhtua

@davidpestana Hi,

I have a problem with the API. I use the directive like you showed in this thread and it works very good! The only problem I have is when I need to show a new route on the same map. I call the same function again from the parent Component and it prints the new route over the old one. This is the code i have: ` export class DirectionsMapDirective { @Input() destination:string; @Input() origin:string;

constructor (private gmapsApi: GoogleMapsAPIWrapper) {

}

ngOnInit(){
    this.setMap();
}

setMap():void{
    this.gmapsApi.getNativeMap().then(map => {
        var directionsService = new google.maps.DirectionsService;
        var directionsDisplay = new google.maps.DirectionsRenderer({draggable: true});
        directionsDisplay.setMap(map);
        directionsService.route({
            origin: this.origin,
            destination: this.destination,
            waypoints: [],
            optimizeWaypoints: true,
            travelMode: 'DRIVING'
        }, function(response, status) {
            if (status === 'OK') {
                directionsDisplay.setDirections(response);
            } else {
                window.alert('Directions request failed due to ' + status);
            }
        });

    });
}

} ` When i need to show the new route the parent component calls the setMap function and the origin and destination variables change. Is there a way to reload the map from the parent Component?

okanok avatar Dec 02 '16 08:12 okanok

@davidpestana I too am having the issue described by okanok. If anyone has a fix for the map displaying all routes without resetting it would be very helpful. @okanok did you ever get this figured out

drewlavine avatar Jan 11 '17 19:01 drewlavine

Hi @davidpestana and @ahardworker, I tried to use the directive and the HTML david specified but i keep getting this error "InvalidValueError: in property origin: not a string; and not a LatLng or LatLngLiteral: in property lat: not a number; and unknown property lat".

Component.ts origin = { longitude: 4.333, lattitude: -1.2222 }; // its a example aleatory position destination = { longitude: 22.311, lattitude: -0.123 }; // its a example aleatory position

Component.html

<sebm-google-map-directions [origin]="origin" [destination]="destination"></sebm-google-map-directions>

Error : InvalidValueError: in property origin: not a string; and not a LatLng or LatLngLiteral: in property lat: not a number; and unknown property lat

Do you have any idea's please?

thanks

developernm avatar Jan 13 '17 14:01 developernm

@developernm you spelled latitude wrong ;)

bene-starzengruber avatar Jan 13 '17 19:01 bene-starzengruber

@bene-starzengruber was in a little bit in a rush lol

developernm avatar Jan 13 '17 20:01 developernm

I have been following this topic with great interest. And I look forward to this being integrated into the project (core).

bonzysalesman avatar Jan 17 '17 01:01 bonzysalesman

I am also intetesTed I need having the route planning and directions api.

figuerres avatar Jan 17 '17 01:01 figuerres