Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

First stable release of BikeToWork - mapper.js (See related posts)

// first stable release of Biketoworks mapper.js
   1  
   2  var map
   3  var points = $A()
   4  var polyline = null
   5  
   6  var mapper = {
   7    load: function() {
   8  	  map = new GMap($('map'))
   9      mapper.initializeMap()		
  10      mapper.registerBehaviours()		
  11    },
  12    
  13    initializeMap: function(){
  14      map.setMapType(G_SATELLITE_MAP)
  15  	
  16  	  map.addControl(new GLargeMapControl())
  17      map.addControl(new GOverviewMapControl(new GSize(200,200)))
  18      
  19      var bangalore = new GLatLng(parseFloat(12.921458133100641), parseFloat(77.58471965789795));
  20  		map.centerAndZoom(bangalore, -1)
  21    },
  22    
  23    registerBehaviours: function() {
  24  	  mapper.addZoomLimiter()
  25  	  mapper.addClickListeners()
  26    },
  27  
  28    addClickListeners: function(){
  29      GEvent.addListener(map, 'click', function(overlay, point) {
  30        if(overlay != null) return
  31        mapper.addPointsToMap($A([new GLatLng(point.y, point.x)]))
  32      })
  33    },
  34    
  35    load_route : function(request) {
  36      geopoints = $A(JSON.parse(request.responseText))
  37      pointz = []
  38      map.removeOverlay(polyline)
  39      
  40      pointz = geopoints.collect(function(geopoint) {
  41        return new GLatLng(geopoint['longitude'], geopoint['latitude'])
  42      })
  43      
  44      mapper.addPointsToMap(pointz)
  45    },
  46  
  47    addPointsToMap: function(pointz) {
  48      pointz.each(function(point){
  49        map.addOverlay(new GMarker(point))
  50        points.push(point)
  51      })
  52      
  53      $('distance').innerHTML = mapper.distance(pointz)
  54      polyline = new GPolyline(points)
  55      map.addOverlay(polyline)
  56    },
  57    
  58    distance: function(pointz) {
  59      dist = 0
  60      for(i = 1; i < pointz.lengh; i++) {
  61        dist = dist + pointz[i].distanceFrom(pointz[i-1])
  62      }
  63      return (dist.toFixed(5))/1000
  64    },
  65    
  66    addZoomLimiter: function() {
  67      GEvent.addListener(map, 'zoom', function() {
  68        if(map.getZoomLevel() < -1) { map.zoomTo(-1);}
  69      });
  70    },
  71    
  72    behaviours : {
  73      '#submit' : function(e) {
  74        e.onclick = function() {
  75          json_points = JSON.stringify(points)
  76          new Ajax.Request('/map/save_route', { onSuccess : alert("Route saved"), parameters: 'markers=' + json_points })
  77        }
  78      },
  79  
  80      '#load' : function(e) {
  81        e.onclick = function() {
  82          new Ajax.Request('/map/load_route', { onSuccess: mapper.load_route })
  83        }
  84      }
  85    }
  86  }
  87  
  88  Behaviour.addLoadEvent(mapper.load);
  89  Behaviour.register(mapper.behaviours)

You need to create an account or log in to post comments to this site.


Click here to browse all 5521 code snippets

Related Posts