I am using HTML5 Geolocation API in which I map the route between client current position and the final position. I used Distance Services API which displays the route but the problem is that I am unable to change the default markers.
When I used "suppressMarkers: true" then default markers hide but none of custom markers are displaying.
Here is my js:
var icons = {
start: new google.maps.MarkerImage(
// URL
'https://maps.google.com/mapfiles/kml/shapes/man.png',
new google.maps.Size(44, 32),
new google.maps.Point(0, 0),
new google.maps.Point(22, 32)
),
end: new google.maps.MarkerImage(
// URL
'https://maps.google.com/mapfiles/kml/shapes/police.png',
new google.maps.Size(44, 32),
new google.maps.Point(0, 0),
new google.maps.Point(22, 32)
)
};
function Init() {
if (navigator.geolocation) {
var options = {
frequency: 3000,
enableHighAccuracy: true,
maximumAge: 30000,
time: 27000
};
watchProcess = navigator.geolocation.watchPosition(geolocation_query, handle_errors, options);
} else {
alert("Geolocation services are not supported by your web browser.");
}
}
function geolocation_query(position) {
if (CltLatitude == null || CltLatitude == "")
if (CltLongitude == null || CltLongitude == "") {
CltLatitude = position.coords.latitude + (Math.random() / 10 * ((2.55 % 2) ? 1 : -1));
CltLongitude = position.coords.longitude + (Math.random() / 10 * ((2.54 % 2) ? 1 : -1));
}
var currentPos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var clientLoc = new google.maps.LatLng(CltLatitude, CltLongitude);
var mapp = {
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: currentPos
};
var mapCanvas = new google.maps.Map(document.getElementById("map-canvas"), mapp);
var rendererOptions = {
map: mapCanvas,
suppressMarkers: true
};
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
directionsDisplay.setMap(mapCanvas);
calculateAndDisplayRoute(directionsService, directionsDisplay, currentPos, clientLoc);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay, curentLoc, clntNLocation) {
directionsService.route({
origin: curentLoc,
destination: clntNLocation,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var leg = response.routes[0].legs[0];
makeMarker(leg.start_location, icons.start, "Start");
makeMarker(leg.end_location, icons.end, 'End');
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
function makeMarker(position, icon, title) {
new google.maps.Marker({
position: position,
map: map,
icon: icon,
title: title
});
}
google.maps.event.addDomListener(window, 'load', Init);
Tell me where I am doing mistake or missing something?
I tried a lot but none of them worked.. THanks in advance