I am currently working on a project that involves displaying hit markers on google maps along with a route from start to finish.
Although I have successfully displayed the route, I encountered an issue where both the origin and destination have identical co-ordinates. As a result, the destination marker (marker 'D') overlaps the origin marker, as shown below:
My goal is to hide only the destination marker, but I'm uncertain of how to accomplish this for an individual marker.
The displayRoute function
displayRoute(directionsService, directionsDisplay) {
let numberOfHits = this.geo.Data.rows.length - 1;
let firstHit = this.geo.Data.rows[numberOfHits];
let lastHit = this.geo.Data.rows[0];
let wayPoint, i;
let wayPointsArray = [];
this.checkForDuplicates('Id', this.geo.Data.rows);
for (i = 1; i < numberOfHits; i++) {
wayPoint = this.geo.Data.rows[i];
if (this.duplicatesArr.indexOf(wayPoint.Id) === -1) {
wayPointsArray.unshift({
location: {
lat: wayPoint.latitude,
lng: wayPoint.longitude
}
});
} else if (this.duplicatesArr.indexOf(wayPoint.Id) > -1) {
console.log('wayPoint', wayPoint.Id, 'has already been hit')
}
}
let request = {
origin: {
lat: firstHit.latitude,
lng: firstHit.longitude
},
destination: {
lat: lastHit.latitude,
lng: lastHit.longitude
},
waypoints: wayPointsArray,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
if((request.origin.lat == request.destination.lat) && (request.origin.lng == request.destination.lng)) {
// Code to hide destination marker
}
directionsService.route(request, (response, status) => {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
});
}
The geo data
this.geo = {
"Data": {
"records": 7,
"total": 1,
"page": 1,
"rows": [
{
"Id": 2778,
"latitude": 51.509697,
"longitude": -2.2
},
{
"Id": 53,
"latitude": 50.980598,
"longitude": -1.36827
},
{
"Id": 2750,
"latitude": 51.152599,
"longitude": -1.34676
},
{
"Id": 2778,
"latitude": 51.509697,
"longitude": -2.2
}
]
}
}
Any assistance with this issue would be highly appreciated!
EDIT
To provide clarification, here is where I have initialized the directionsDisplay:
createMap() {
let directionsDisplay = new google.maps.DirectionsRenderer;
let directionsService = new google.maps.DirectionsService;
let map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});
let bounds = new google.maps.LatLngBounds();
map.fitBounds(bounds);
directionsDisplay.setMap(map);
this.displayRoute(directionsService, directionsDisplay);
};
This snippet is utilized just before the displayRoute() function is called.