var myMap; var GPoints = new Array(0); var routeLengthMiles = 0; var generateDirections = 0; var milesPerKm = 0.621371; var internetExplorer; var notIE; //set when not Internet Explorer var viewType = "Normal"; var followRoad = 1; var setting1Num = 1; var setting1TextOn = "Don't follow road"; var setting1TextOff = "Follow road"; var setting1Default = followRoad; var metricUnits = 0; var setting2Num = 2; var setting2TextOn = "Use U.S. units"; var setting2TextOff = "Use metric"; var setting2Default = metricUnits; var routeLocked = 0; if (navigator.appName == 'Microsoft Internet Explorer'){ // document.ondblclick = handleDblClick; internetExplorer = true; if (navigator.appVersion.search(/MSIE 7/)>-1){ // alert("You seem to be using Internet Explorer 7 (IE7). \nYou may experience errors with long routes because IE7 has a very short timeout.\nTo avoid these problems\n- use another browser (Internet Explorer 6 or Firefox) or \n- edit your registry as described here http://support.microsoft.com/kb/181050 or\n- draw long routes in multiple short sections."); }; } else { notIE = true; // window.ondblclick = handleDblClick; internetExplorer = false; window.onkeyup = handleKeyPress; } // alert("about to add points"); init(); // addPoint(-118.251,34.220); // addPoint(-118.251, 34.221); // alert("done adding points"); function init(){ myMap = new GMap2(document.getElementById("myMap"),{draggableCursor: 'crosshair', draggingCursor: 'crosshair'}); // myMap.centerAndZoom(new GPoint(-101, 40), 12); myMap.addMapType(G_PHYSICAL_MAP); myMap.addControl(new GLargeMapControl()); myMap.addControl(new GMapTypeControl()); myMap.addControl(new GScaleControl()); GEvent.addListener(myMap, 'click', function(overlay, point) { if (point) { if (routeLocked) { //alert("You cannot add points because this route is locked. Drag to pan, and use the zoom control on the left to zoom. \nTo start a new route, click Home and then Bike Ride Planner."); } else { if(myMap.getZoom()<12){ //bigger means closer zoom myMap.setCenter(new GLatLng(point.y, point.x), myMap.getZoom()+2); } else { addPoint(point.x, point.y); } } } }); setUpSettingsList(); } //window.onclick = handleClick; //window.onkeyup = handleKeyPress; function handleKeyPress(e){ if (e.keyCode == 32) removePoint(); } function handleClick(e){ alert("map single click handled"); } function handleDblClick(e){ alert("map double click handled"); } function addPoint(xCoord, yCoord) { if(GPoints.length > 0){ var startLat = GPoints[GPoints.length - 1].y; var startLon = GPoints[GPoints.length - 1].x; //GLog.write("start lat:" + startLat + "startLon:" + startLon); if(followRoad == 1){ var lastSegLength = getDistanceBetweenPoints(xCoord, yCoord, GPoints[GPoints.length-1].x, GPoints[GPoints.length-1].y); if (lastSegLength>50){ alert("The last segment is too long to route along the road ("+Math.round(lastSegLength)+" miles). Please draw in shorter steps or turn off Follow Road."); }else{ //document.body.style.cursor='wait'; //myMap.style.cursor = 'wait'; GPoints.push(new GPoint(xCoord, yCoord)); //GLog.write("Point pushed"); getMapAndRouteString(); var myCallsString = makeCallsString(); document.getElementById('callsString').value = myCallsString; document.getElementById('formRouteLeg').submit(); } }else{ GPoints.push(new GPoint(xCoord, yCoord)) } }else{ GPoints.push(new GPoint(xCoord, yCoord)); } drawRoute(GPoints); } function getRouteLengthMiles(){ return getRouteLengthKm()*milesPerKm; } function getRouteLengthKm() { var totalLength = 0; var pointNum = 1; while (pointNum < GPoints.length){ totalLength = totalLength + getDistanceBetweenPoints(GPoints[pointNum-1].x,GPoints[pointNum-1].y,GPoints[pointNum].x,GPoints[pointNum].y); pointNum = pointNum + 1; } //alert("total: " + totalLength); return totalLength; } function getDistanceBetweenPoints(x1, y1, x2, y2) { var deltaY = y2-y1; var cosine = Math.cos(y1*Math.PI/180); var deltaX = cosine*(x2-x1); var segmentLength = 6371.01*Math.PI/180*Math.sqrt(deltaX*deltaX + deltaY*deltaY); return segmentLength; } function getRouteString(){ var routeString = ""; if (GPoints.length > 0) { routeString = routeString + GPoints[0].y + "Z" + GPoints[0].x ;// two steps to get "Z"'s for (i = 1; i < GPoints.length; i++){ routeString = routeString + "Z" + GPoints[i].y + "Z" + GPoints[i].x; } } return routeString; } function getMapString(){ return myMap.getCenter().x + "Z" + myMap.getCenter().y + "Z" + myMap.getZoom() ; } function getProfile(){ var dataPointString = ""; if (GPoints.length > 0) { dataPointString = dataPointString + GPoints[0].y + "Z" + GPoints[0].x ; for (i = 1; i < GPoints.length; i++){ dataPointString = dataPointString + "Z" + GPoints[i].y + "Z" + GPoints[i].x; } } document.getElementById('dataString').value = dataPointString; document.getElementById('useMetric').value = metricUnits; } function getProfileGpx(){ var dataPointString = ""; alert("Caution: All data from toporoute.com may contain errors. \n Do not rely on any data from toporoute.com for navigation. It is for planning or confirmation purposes only. "); if (GPoints.length > 0) { dataPointString = dataPointString + GPoints[0].y + "Z" + GPoints[0].x ; for (i = 1; i < GPoints.length; i++){ dataPointString = dataPointString + "Z" + GPoints[i].y + "Z" + GPoints[i].x; } } document.getElementById('dataStringGpx').value = dataPointString; } function getElevationDataFile(){ var dataPointString = ""; if (GPoints.length > 0) { dataPointString = dataPointString + GPoints[0].y + "Z" + GPoints[0].x ; for (i = 1; i < GPoints.length; i++){ dataPointString = dataPointString + "Z" + GPoints[i].y + "Z" + GPoints[i].x; } } document.getElementById('dataStringElevation').value = dataPointString; document.getElementById('useMetricElevation').value = metricUnits; } function getMapAndRouteString(){ //GLog.write("In getMapAndRouteString"); var mapAndRouteString = getMapString()+"Z"+getRouteString(); //alert(mapAndRouteString); //GLog.write("In getMapAndRouteString, got map and route string: " + mapAndRouteString); document.getElementById('saveRouteString').value = mapAndRouteString; document.getElementById('routeLegString').value = mapAndRouteString; } function removePoint() { if (GPoints.length > 0) { GPoints.pop(); drawRoute(GPoints); } } function removeAllPoints() { if (confirm("Are you sure you want to clear the whole route?")) { while (GPoints.length > 0) { GPoints.pop(); // drawRoute(GPoints); } drawRoute(GPoints); } } function drawRoute(GPoints){ myMap.clearOverlays(); routeLengthMiles = Math.round(getRouteLengthMiles()*100)/100; if (GPoints.length > 0) { var icon = new GIcon(); //icon.image = "http://maps.google.com/mapfiles/kml/pal4/icon16.png"; icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png"; icon.iconSize = new GSize(12,20); icon.iconAnchor = new GPoint(6,20); if(metricUnits){ myMap.addOverlay(new GMarker(GPoints[0], {icon: icon, title: Math.round(getRouteLengthKm()*100)/100 + " km"})); }else{ myMap.addOverlay(new GMarker(GPoints[0], {icon: icon, title: routeLengthMiles + " miles"})); } myMap.addOverlay(new GPolyline(GPoints)); } if (GPoints.length > 1) { var icon = new GIcon(); // icon.image = "http://maps.google.com/mapfiles/kml/pal4/icon49.png"; icon.image = "http://www.google.com/intl/en_us/mapfiles/markerTransparent.png"; icon.iconSize = new GSize(20,20); icon.iconAnchor = new GPoint(4,5); if(metricUnits){ myMap.addOverlay(new GMarker(GPoints[GPoints.length-1], {icon: icon, title: Math.round(getRouteLengthKm()*100)/100 + " km"})); }else{ myMap.addOverlay(new GMarker(GPoints[GPoints.length-1], {icon: icon, title: routeLengthMiles + " miles"})); } } } function setUpSettingsList(){ var settingList = document.getElementById("settingList"); settingList.options[1].text = setting1TextOn; settingList.options[2].text = setting2TextOff; } function changeSetting() { var settingList = document.getElementById("settingList"); var selectedAction = settingList.options[settingList.selectedIndex].text; if (selectedAction == "Don't follow road"){ followRoad = 0; settingList.options[settingList.selectedIndex].text = "Follow road"; } if (selectedAction == "Follow road"){ followRoad = 1; settingList.options[settingList.selectedIndex].text = "Don't follow road"; } if (selectedAction == "Output directions"){ generateDirections = 1; alert("Output directions turned on. Directions will be displayed in a new window for each leg of the route, as you draw. To stop producing directions, select No directions from the Settings menu."); settingList.options[settingList.selectedIndex].text = "No directions"; } if (selectedAction == "No directions"){ generateDirections = 0; settingList.options[settingList.selectedIndex].text = "Output directions"; } if (selectedAction == setting2TextOff){ setMetric(); } if (selectedAction == setting2TextOn){ clearMetric(); } settingList.selectedIndex = 0; } function setMetric(){ metricUnits = 1; document.getElementById("settingList").options[2].text = setting2TextOn; drawRoute(GPoints); } function clearMetric(){ metricUnits = 0; document.getElementById("settingList").options[2].text = setting2TextOff; drawRoute(GPoints); }; function setFollowRoad(){ followRoad = 1; document.getElementById("settingList").options[1].text = setting1TextOn; } function clearFollowRoad(){ followRoad = 0; document.getElementById("settingList").options[1].text = setting1TextOff; } function setNormalView(){ myMap.setMapType(G_NORMAL_MAP); } function setPhysicalView(){ myMap.setMapType(G_PHYSICAL_MAP); } function setSatelliteView(){ myMap.setMapType(G_SATELLITE_MAP); } function setHybridView(){ myMap.setMapType(G_HYBRID_MAP); } function setRouteLocked(){ routeLocked = 1; } function getViewType(){ var callsString; var currView = myMap.getCurrentMapType(); if (currView == G_NORMAL_MAP){ callsString = "setNormalView"; }else if (currView == G_PHYSICAL_MAP){ callsString = "setPhysicalView"; }else if (currView == G_SATELLITE_MAP){ callsString = "setSatelliteView" }else if (currView == G_HYBRID_MAP){ callsString = "setHybridView"; } //alert("In getViewType, callsString = " + callsString); return callsString; } function makeCallsString(){ var callsString; callsString = getViewType(); if (metricUnits){ callsString = callsString + ",setMetric"; }else{ callsString = callsString + ",clearMetric"; } if (followRoad){ callsString = callsString + ",setFollowRoad"; }else{ callsString = callsString + ",clearFollowRoad"; } if (routeLocked){ callsString = callsString + ",setRouteLocked"; } return callsString; } function takeAction() { var actionList = document.getElementById("actionList"); var selectedAction = actionList.options[actionList.selectedIndex].text; if (selectedAction == "Save as GPX"){ if (GPoints.length < 2){ alert("Error: Can't create GPX. Please draw a route first.") }else if (GPoints.length > 6000) { alert("This route is too long to save as GPX. If you need a GPX for a long route, please email us (see the email link under Help)") }else{ getProfileGpx(); document.getElementById('formGetGpx').submit(); } } if (selectedAction == "Elev. data file"){ if (GPoints.length < 2){ alert("Error: Can't create elevation data file. Please draw a route first.") }else if (GPoints.length > 3000) { alert("This route is too long to save as an elevation data file. If you need a data file for a long route, please email us (see the email link under Help)") }else{ getElevationDataFile(); document.getElementById('formGetElevationFile').submit(); } } if (selectedAction == "Plot elevation"){ if (GPoints.length > 1){ getProfile(); var formVar = document.getElementById('postRouteStringForm'); formVar.submit(); }else{ alert("Error: Can't create elevation plot. Please draw a route first.") } } if (selectedAction == "Undo"){ if (GPoints.length > 0){ removePoint(); }else{ alert("No points to undo.") } }; if (selectedAction == "Clear route"){ if (GPoints.length > 0){ removeAllPoints() }else{ alert("No route to clear.") } routeLocked = 0; } if (selectedAction == "Save route"){ if (GPoints.length > 1){ getMapAndRouteString(); document.getElementById('saveRouteCallsString').value = makeCallsString() + ",setRouteLocked"; document.getElementById('formSaveRoute').submit(); alert("Route saved. Bookmark or email the URL in the new window to return to your route in the future.") }else{ alert("Error: can't save route. Please draw a route first.") } } if (selectedAction == "Help / feedback"){ window.open("/helpAndFeedback.htm"); }; actionList.selectedIndex = 0; }