Skip to content

Instantly share code, notes, and snippets.

@beigna
Last active October 17, 2015 00:34
Show Gist options
  • Save beigna/3ee41121081560c2c05e to your computer and use it in GitHub Desktop.
Save beigna/3ee41121081560c2c05e to your computer and use it in GitHub Desktop.
var Route = function(data) {
this.id = data.id;
this.waypoints = data.waypoints;
this._L_LatLng_list;
this._L_Polyline_obj;
};
Route.prototype._fill_L_LatLng = function() {
this._L_LatLng_list = this.waypoints.map(function(obj) {
return new L.LatLng(obj.lat, obj.lon);
});
};
Route.prototype._draw_L_Polyline = function() {
this._L_Polyline_obj = new L.Polyline(this._L_LatLng_list, {
color: 'red',
weight: 3,
opacity: 1,
smoothFactor: 1
});
};
Route.prototype.showOn = function(map) {
if (!this._L_LatLng_list) { this._fill_L_LatLng(); }
if (!this._L_Polyline_obj) {this._draw_L_Polyline(); }
map.addLayer(this._L_Polyline_obj);
map.fitBounds(this._L_Polyline_obj.getBounds());
}
Route.prototype.showOff = function(map) {
map.removeLayer(this._L_Polyline_obj)
}
var current_location;
var rendered_routes = {}
$(document).ready(function() {
console.log('Ready!');
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function(p) {
current_location = Array(p.coords.latitude, p.coords.longitude);
map.setView(current_location, 15);
});
}
$('a.route_link').click(function() {
var route_id = $(this).data('id');
if (rendered_routes.hasOwnProperty(route_id)) {
if (map.hasLayer(rendered_routes[route_id]._L_Polyline_obj)) {
rendered_routes[route_id].showOff(map);
}
else {
rendered_routes[route_id].showOn(map);
}
}
else {
$.ajax({
url: '/ajax/route/' + route_id,
}).success(function(data) {
route = new Route(data);
route.showOn(map);
rendered_routes[route_id] = route;
});
}
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment