';
}
}
function getStartingLocation() {
return createLocationFromForm('start');
}
function getEndingLocation() {
return createLocationFromForm('end');
}
function geocodeWithCustomIcon(locationObject, id, iconName) {
MQ.geocode()
.search(locationObject)
.on('success', function(e) {
var best = e.result.best;
var latlng = best.latlng;
var iconObject = new Icon(id);
iconObject.name = iconName;
var popup = L.marker(latlng, {
icon: iconObject.createLeafletIcon(),
draggable: true
});
markerManager.addMarker({
id: id,
latlng: latlng,
marker: popup,
iconObject: iconObject,
});
popup.on('dragend', function(event) {
markerManager.removeMarker(id);
var marker = event.target;
var position = marker.getLatLng().wrap();
markerManager.addMarker({
id: id,
latlng: position,
marker: marker,
iconObject: iconObject,
});
doPrint();
});
var markerGroup = L.featureGroup(markerManager.getLeafletMarkers());
map.fitBounds(markerGroup.getBounds());
doPrint();
});
}
function geocode(location) {
var id = util.uuid();
if (validateLatLng(location.latitude, location.longitude)) {
geocodeWithCustomIcon(location.getLatitudeLongitudeObject(), id, 'mcenter');
} else {
geocodeWithCustomIcon(location.getLocationObject(), id, 'mcenter');
}
addLocationToList(location, id);
$('.directions-well').hide();
$('.location-well').hide();
}
function addLocation() {
clearAlerts();
if (validateLocationForm('location')) {
var location = createLocationFromForm('location');
geocode(location);
}
}
function removeLocation(id) {
markerManager.removeMarker(id);
$('#edit-button-' + id).hide();
$('#' + id).remove();
$('.edit-well').hide();
doPrint();
}
function removeDirections(id) {
$('.edit-well').hide();
directionsManager.removeDirections(id);
$('#' + id).remove();
doPrint();
}
function updateMarker(id) {
var marker = markerManager.getMarker(id);
markerManager.removeMarker(marker.id);
var updatedIcon = updateIcon(id);
var popup = L.marker(marker.latlng, {
icon: updatedIcon.createLeafletIcon(),
draggable: true
});
popup.on('dragend', function(event) {
markerManager.removeMarker(id);
var aMarker = event.target;
var position = aMarker.getLatLng().wrap();
markerManager.addMarker({
id: id,
latlng: position,
marker: aMarker,
iconObject: updatedIcon,
});
$('#' + id).children('.location').text(position.lat.toFixed(5) + ', ' + position.lng.toFixed(5));
doPrint();
});
markerManager.addMarker({
id: id,
latlng: marker.latlng,
marker: popup,
iconObject: updatedIcon,
});
$('.edit-well').hide();
doPrint();
}
function directionsGeocode(location, iconName) {
var id = util.uuid();
if (validateLatLng(location.latitude, location.longitude)) {
geocodeWithCustomIcon(location.getLatitudeLongitudeObject(), id, iconName);
} else {
geocodeWithCustomIcon(location.getLocationObject(), id, iconName);
}
$('.directions-well').hide();
return id;
}
function generateRoute() {
clearAlerts();
var id = util.uuid();
var startingLocation = null;
var endingLocation = null;
if (validateLocationForm('start')) {
startingLocation = getStartingLocation();
}
if (validateLocationForm('end')) {
endingLocation = getEndingLocation();
}
if (startingLocation !== null && endingLocation !== null) {
var startingLocationId = directionsGeocode(startingLocation, 'scenter');
var endingLocationId = directionsGeocode(endingLocation, 'ecenter');
directionsManager.addDirections({
id: id,
startingLocation: {
id: startingLocationId,
location: startingLocation,
},
endingLocation: {
id: endingLocationId,
location: endingLocation,
},
});
$('.directions-well').hide();
$('.location-well').hide();
}
}
function validateKey(keyObj) {
$('#mapquest-key-container').removeClass('has-error');
$('#mapquest-key-container p').remove();
//check valid key
if (isValidKey(keyObj)) {
var thisKey = keyObj.value;
thisKey = thisKey.trim();
if (!thisKey.match('[a-zA-Z]+')) {
// alerting user
$('#mapquest-key-container').addClass('has-error');
$('#mapquest-key-container').append('
Invalid key " + thisKey + ". Please enter a valid key.
');
return;
}
doPrint();
}
}
//validates the width of Static Map image.
//Validation is true only if the width isNumber and inRange 1-2048
function validateWidth() {
$('#map-width-container').removeClass('has-error');
$('#map-width-container p').remove();
var widthObj = document.getElementById('map-width');
var smWidth = widthObj.value;
if (isNaN(smWidth)) {
$('#map-width-container').addClass('has-error');
$('#map-width-container').append('
Please enter a valid number for width.
');
widthObj.value = 600; //set the default width
} else {
if (smWidth > 0 && smWidth < 2049) {
var decPtIndex = smWidth.indexOf('.');
if (decPtIndex !== -1) {
$('#map-width-container').addClass('has-error');
$('#map-width-container').append('
Invalid value for width. Acceptable range of width is 1-2048 px.
');
widthObj.value = 600; //set the default width
}
}
doPrint();
}
//validates the height of Static Map image.
//Validation is true only if the height isNumber and inRange 1-2048
function validateHeight() {
$('#map-height-container').removeClass('has-error');
$('#map-height-container p').remove();
var heightObj = document.getElementById('map-height');
var smHeight = heightObj.value;
if (isNaN(smHeight)) {
$('#map-height-container').addClass('has-error');
$('#map-height-container').append('
Please enter a valid number for height.
');
heightObj.value = 500; //set the default value
} else {
if (smHeight > 0 && smHeight < 2049) {
var decPtIndex = smHeight.indexOf('.');
if (decPtIndex !== -1) {
$('#map-height-container').addClass('has-error');
$('#map-height-container').append('
Value for width cannot be fractional.
');
heightObj.value = 500; //set the default value
}
} else {
$('#map-height-container').addClass('has-error');
$('#map-height-container').append('
Invalid value for height. Acceptable range of height is 1-2048 px.