The goal:
- Display a map with some markers (from an array or file)
- Let users search for a place/address
- Mark the place on the map
- Determine what marker is closest to the place
- Draw & display directions from the marker to the place
- Update the directions if a new search is performed
I think I am close. I've cobbled together some code from the maps API documentation & other questions here on Stack Overflow. I have it correctly marking the place that's searched and determining the closest marker, but am having trouble getting it to update the direction markers & drawing the path.
Is something wrong with the onChangeHandler
or the listener that should trigger it?
This example from Google Maps API Documentation is where I was looking to for the last part with the listener, to update & draw new directions.
Eventually I intend to add the steps as seen on another Google Maps API sample
function initMap() {
var map;
var myOptions = {
zoom: 14,
center: {lat: 40.7484, lng: -73.9857},
scrollwheel: false,
mapTypeId: 'roadmap'
};
// map for search & directions
map = new google.maps.Map(document.getElementById('map'), myOptions);
// create initial origin marker
// to-do: error if not defined
var markerA = new google.maps.Marker({
position: {lat: 40.7484, lng: -73.9857},
map: map,
});
// create array of locations and assign values
var locs = [];
locs[0] = ['Location 1 Name', '40.73297438','-73.97860823'];
locs[1] = ['Location 2 Name', '40.72824765','-73.98219971'];
locs[2] = ['Location 3 Name', '40.73181838','-73.97871015'];
var infowindow = new google.maps.InfoWindow;
var loc, i;
var locations = [];
// create markers for each loc and place them on map
for (i = 0; i < locs.length; i++) {
loc = new google.maps.Marker({
position: new google.maps.LatLng(locs[i][1], locs[i][2]),
map: map
});
google.maps.event.addListener(loc, 'click', (function(loc, i) {
return function() {
infowindow.setContent(locs[i][0]);
infowindow.open(map, loc);
}
})(loc, i));
locations.push(loc.position);
}
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function () {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// Listen for the event fired when the user selects a prediction and
// retrieve more details for that place.
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function (marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function (place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// update the origin, markerA, and add to map
markerA.setPosition(place.geometry.location);
markerA.setIcon(icon);
markerA.setTitle(place.name);
markers.push(markerA);
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
map.setOptions({
zoom: 17
});
});
var pointA = markerA.getPosition();
var markerB;
findClosest(pointA, locations);
// improved garage search to determine closest location
function findClosest(pointA, locations) {
var closestDistance = Number.MAX_SAFE_INTEGER;
var closest;
for (i = 0; i < locations.length; i++) {
var distance = google.maps.geometry.spherical.computeDistanceBetween(pointA, locations[i]);
if (distance < closestDistance) {
closestDistance = distance;
closest = locations[i];
}
}
markerB = closest;
// alert(closest);
}
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer({
map: map
});
var pointB = markerB;
calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB, map);
var onChangeHandler = function() {
calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB, map);
};
map.addListener('change', onChangeHandler);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB, map) {
directionsService.route({
origin: pointA,
destination: pointB,
travelMode: google.maps.TravelMode.WALKING
}, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Could not load directions due to ' + status);
}
});
}
#pac-input {
background-color: #ffffff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
margin-top: 9px;
padding: 5px 11px 5px 13px;
text-overflow: ellipsis;
width: 400px;
}
#map { height: 475px; }
<input type="text" id="pac-input" class="controls" placeholder="Where are you headed?" /></p>
<div id="map"> </div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC8MQrdVaz8JKiUnSU0usHzGRQGrI_x3DQ&callback=initMap&libraries=geometry,places" async="" defer="defer" type="text/javascript"></script>