Skip to content

Commit

Permalink
[feature] add query status output
Browse files Browse the repository at this point in the history
  • Loading branch information
explooosion committed Oct 23, 2018
1 parent 97b085d commit 2a2aec0
Showing 1 changed file with 99 additions and 84 deletions.
183 changes: 99 additions & 84 deletions src/directive/agm-direction.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export class AgmDirection implements OnChanges, OnInit {
// Send a custom infowindow
@Output() sendInfoWindow: EventEmitter<InfoWindow> = new EventEmitter<InfoWindow>();

// Status of Directions Query (google.maps.DirectionsStatus.OVER_QUERY_LIMIT)
@Output() status: EventEmitter<string> = new EventEmitter<string>();

public directionsService: any = undefined;
public directionsDisplay: any = undefined;

Expand Down Expand Up @@ -158,99 +161,111 @@ export class AgmDirection implements OnChanges, OnInit {

this.onResponse.emit(response);

if (status === 'OK') {
this.directionsDisplay.setDirections(response);

/**
* Emit The DirectionsResult Object
* https://developers.google.com/maps/documentation/javascript/directions?hl=en#DirectionsResults
*/

// Custom Markers
if (typeof this.markerOptions !== 'undefined') {

// Remove origin markers
try {
if (typeof this.originMarker !== 'undefined') {
google.maps.event.clearListeners(this.originMarker, 'click');
this.originMarker.setMap(null);
}
if (typeof this.destinationMarker !== 'undefined') {
google.maps.event.clearListeners(this.destinationMarker, 'click');
this.destinationMarker.setMap(null);
}
this.waypointsMarker.forEach((w: any) => {
if (typeof w !== 'undefined') {
google.maps.event.clearListeners(w, 'click');
w.setMap(null);
// Emit Query Status
this.status.emit(status);

/**
* DirectionsStatus
* https://developers.google.com/maps/documentation/javascript/directions#DirectionsStatus
*/
switch (status) {
case 'OK':
this.directionsDisplay.setDirections(response);

/**
* Emit The DirectionsResult Object
* https://developers.google.com/maps/documentation/javascript/directions?hl=en#DirectionsResults
*/

// Custom Markers
if (typeof this.markerOptions !== 'undefined') {

// Remove origin markers
try {
if (typeof this.originMarker !== 'undefined') {
google.maps.event.clearListeners(this.originMarker, 'click');
this.originMarker.setMap(null);
}
});

} catch (err) {
console.error('Can not reset custom marker.', err);
}
if (typeof this.destinationMarker !== 'undefined') {
google.maps.event.clearListeners(this.destinationMarker, 'click');
this.destinationMarker.setMap(null);
}
this.waypointsMarker.forEach((w: any) => {
if (typeof w !== 'undefined') {
google.maps.event.clearListeners(w, 'click');
w.setMap(null);
}
});

// Set custom markers
const _route = response.routes[0].legs[0];
try {
// Origin Marker
if (typeof this.markerOptions.origin !== 'undefined') {
this.markerOptions.origin.map = map;
this.markerOptions.origin.position = _route.start_location;
this.originMarker = this.setMarker(
map,
this.originMarker,
this.markerOptions.origin,
_route.start_address,
);
}
// Destination Marker
if (typeof this.markerOptions.destination !== 'undefined') {
this.markerOptions.destination.map = map;
this.markerOptions.destination.position = _route.end_location;
this.destinationMarker = this.setMarker(
map,
this.destinationMarker,
this.markerOptions.destination,
_route.end_address,
);
} catch (err) {
console.error('Can not reset custom marker.', err);
}

// Waypoints Marker
if (typeof this.markerOptions.waypoints !== 'undefined') {

this.waypoints.forEach((waypoint: any, index: number) => {

// If waypoints are not array then set all the same
if (!Array.isArray(this.markerOptions.waypoints)) {
this.markerOptions.waypoints.map = map;
this.markerOptions.waypoints.position = _route.via_waypoints[index];
this.waypointsMarker.push(this.setMarker(
map,
waypoint,
this.markerOptions.waypoints,
_route.via_waypoints[index],
));
} else {
this.markerOptions.waypoints[index].map = map;
this.markerOptions.waypoints[index].position = _route.via_waypoints[index];
this.waypointsMarker.push(this.setMarker(
map,
waypoint,
this.markerOptions.waypoints[index],
_route.via_waypoints[index],
));
}
// Set custom markers
const _route = response.routes[0].legs[0];
try {
// Origin Marker
if (typeof this.markerOptions.origin !== 'undefined') {
this.markerOptions.origin.map = map;
this.markerOptions.origin.position = _route.start_location;
this.originMarker = this.setMarker(
map,
this.originMarker,
this.markerOptions.origin,
_route.start_address,
);
}
// Destination Marker
if (typeof this.markerOptions.destination !== 'undefined') {
this.markerOptions.destination.map = map;
this.markerOptions.destination.position = _route.end_location;
this.destinationMarker = this.setMarker(
map,
this.destinationMarker,
this.markerOptions.destination,
_route.end_address,
);
}

}); // End forEach
// Waypoints Marker
if (typeof this.markerOptions.waypoints !== 'undefined') {

this.waypoints.forEach((waypoint: any, index: number) => {

// If waypoints are not array then set all the same
if (!Array.isArray(this.markerOptions.waypoints)) {
this.markerOptions.waypoints.map = map;
this.markerOptions.waypoints.position = _route.via_waypoints[index];
this.waypointsMarker.push(this.setMarker(
map,
waypoint,
this.markerOptions.waypoints,
_route.via_waypoints[index],
));
} else {
this.markerOptions.waypoints[index].map = map;
this.markerOptions.waypoints[index].position = _route.via_waypoints[index];
this.waypointsMarker.push(this.setMarker(
map,
waypoint,
this.markerOptions.waypoints[index],
_route.via_waypoints[index],
));
}

}); // End forEach

}
} catch (err) {
console.error('MarkerOptions error.', err);
}
} catch (err) {
console.error('MarkerOptions error.', err);
}
}
break;

}
default:
// console.warn(status);
break;
} // End switch

});
}
Expand Down

0 comments on commit 2a2aec0

Please sign in to comment.