Untitled
raw download clone
JAVASCRIPT
views 24
,
size 3768 b
$(document).ready(function(e) {
	jQuery.support.cors = true;

	var host; // = "http://localhost:9000/api/1";

	var defaultKey = '3f3e19cc-b624-4d0e-b810-325322cba330';
	var profile = 'car';

	// create a routing client to fetch real routes, elevation.true is only supported for vehicle bike or foot
	var ghRouting = new GraphHopper.Routing({ key: defaultKey, host: host, vehicle: profile, elevation: false });
	var ghGeocoding = new GraphHopper.Geocoding({
		key: defaultKey,
		host: host,
		limit: 8,
		locale: 'en' /* currently fr, en, de and it are explicitely supported */
	});

	var routingMap = createMap('routing-map');
	routingMap.setView([ 52.521235, 13.3992 ], 12);

	var routingLayer = L.geoJson().addTo(map);
	routingLayer.options = {
		style: { color: '#00cc33', weight: 5, opacity: 0.6 }
	};

	$('#kurwa').on('click', function(e) {
		setupRoutingAPI(routingMap, ghRouting);
	});
});

licznik = 0;

start = '39.099724';
start_a = '-94.578331';

stop = '45.523064';
stop_a = '-122.676483';

vegas = '36.114647';
vegas_b = '-115.172813';

function setupRoutingAPI(map, ghRouting) {
	if (ghRouting.points.length > 1) {
		ghRouting.clearPoints();
		routingLayer.clearLayers();
	}
	licznik = licznik + 1;

	ghRouting.addPoint(new GHInput(start, start_a));
	if (licznik == 1) {
		ghRouting.addPoint(new GHInput(vegas, vegas_b));
	}
	ghRouting.addPoint(new GHInput(stop, stop_a));

	ghRouting
		.doRequest()
		.then(function(json) {
			var path = json.paths[0];
			routingLayer.addData({
				type: 'Feature',
				geometry: path.points
			});
			var outHtml = 'Distance in meter:' + path.distance;
			outHtml += '<br/>Times in seconds:' + path.time / 1000;
			outHtml += "<br/><a href='" + ghRouting.getGraphHopperMapsLink() + "'>GraphHopper Maps</a>";
			$('#routing-response').html(outHtml);

			if (path.bbox) {
				var minLon = path.bbox[0];
				var minLat = path.bbox[1];
				var maxLon = path.bbox[2];
				var maxLat = path.bbox[3];
				var tmpB = new L.LatLngBounds(new L.LatLng(minLat, minLon), new L.LatLng(maxLat, maxLon));
				map.fitBounds(tmpB);
			}

			instructionsDiv.empty();
			if (path.instructions) {
				var allPoints = path.points.coordinates;
				var listUL = $('<ol>');
				instructionsDiv.append(listUL);
				for (var idx in path.instructions) {
					var instr = path.instructions[idx];

					// use 'interval' to find the geometry (list of points) until the next instruction
					var instruction_points = allPoints.slice(instr.interval[0], instr.interval[1]);

					// use 'sign' to display e.g. equally named images

					$(
						'<li>' +
							instr.text +
							' <small>(' +
							ghRouting.getTurnText(instr.sign) +
							')</small>' +
							' for ' +
							instr.distance +
							'm and ' +
							Math.round(instr.time / 1000) +
							'sec' +
							', geometry points:' +
							instruction_points.length +
							'</li>'
					).appendTo(listUL);
				}
			}
		})
		.catch(function(err) {
			var str = 'An error occured: ' + err.message;
			$('#routing-response').text(str);
		});
}

function createMap(divId) {
	var osmAttr = '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';

	var omniscale = L.tileLayer.wms('https://maps.omniscale.net/v1/ghexamples-3646a190/tile', {
		layers: 'osm',
		attribution: osmAttr + ', &copy; <a href="http://maps.omniscale.com/">Omniscale</a>'
	});

	var osm = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
		attribution: osmAttr
	});

	var map = L.map(divId, { layers: [ omniscale ] });
	L.control
		.layers({
			Omniscale: omniscale,
			OpenStreetMap: osm
		})
		.addTo(map);
	return map;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.