// first stable release of Biketoworks mapper.js
var map
var points = $A()
var polyline = null
var mapper = {
load: function() {
map = new GMap($('map'))
mapper.initializeMap()
mapper.registerBehaviours()
},
initializeMap: function(){
map.setMapType(G_SATELLITE_MAP)
map.addControl(new GLargeMapControl())
map.addControl(new GOverviewMapControl(new GSize(200,200)))
var bangalore = new GLatLng(parseFloat(12.921458133100641), parseFloat(77.58471965789795));
map.centerAndZoom(bangalore, -1)
},
registerBehaviours: function() {
mapper.addZoomLimiter()
mapper.addClickListeners()
},
addClickListeners: function(){
GEvent.addListener(map, 'click', function(overlay, point) {
if(overlay != null) return
mapper.addPointsToMap($A([new GLatLng(point.y, point.x)]))
})
},
load_route : function(request) {
geopoints = $A(JSON.parse(request.responseText))
pointz = []
map.removeOverlay(polyline)
pointz = geopoints.collect(function(geopoint) {
return new GLatLng(geopoint['longitude'], geopoint['latitude'])
})
mapper.addPointsToMap(pointz)
},
addPointsToMap: function(pointz) {
pointz.each(function(point){
map.addOverlay(new GMarker(point))
points.push(point)
})
$('distance').innerHTML = mapper.distance(pointz)
polyline = new GPolyline(points)
map.addOverlay(polyline)
},
distance: function(pointz) {
dist = 0
for(i = 1; i < pointz.lengh; i++) {
dist = dist + pointz[i].distanceFrom(pointz[i-1])
}
return (dist.toFixed(5))/1000
},
addZoomLimiter: function() {
GEvent.addListener(map, 'zoom', function() {
if(map.getZoomLevel() < -1) { map.zoomTo(-1);}
});
},
behaviours : {
'#submit' : function(e) {
e.onclick = function() {
json_points = JSON.stringify(points)
new Ajax.Request('/map/save_route', { onSuccess : alert("Route saved"), parameters: 'markers=' + json_points })
}
},
'#load' : function(e) {
e.onclick = function() {
new Ajax.Request('/map/load_route', { onSuccess: mapper.load_route })
}
}
}
}
Behaviour.addLoadEvent(mapper.load);
Behaviour.register(mapper.behaviours)