Google Maps in Rails 3

| Geen reacties

I was looking for a good Google Maps solution in Ruby on Rails 3. I got a bit frustrated with solutions like YM4R, which basically require you to dive into javascript.

gmaps4rails favours a more Railsesque approach.

Basically you create a model with lon and lat fields (floats) in your model as the markers on the map.

Then you tell gmaps4rails to create a map out of that by adding this to your model:

  acts_as_gmappable :lat=>:lat,:lng=>:lon,:process_geocoding=>false,:check_process=>false

Basically you tell it which columns contain the latitude and longitude values, and since you can also use geocoding to translate address values (which I didn't need for my project) there is a 'process_geocoding' switch. Check_process is also geocoding related.

The interesting bit is that gmaps4rails uses the V3 Api, meaning that you don't need to create a key as you did with Ym4r. Saves some hassle when moving stuff from test environment to production.

This also means that you can use it to add stuff to the map. For instance, I use it to track shipping movements. The position alone is not enough, I also need to see where that ship has been the last few hours. The easiest way is to create 360 icons (one for each bearing) and add a line to the ship.

The ship's bearing (course) is in the database, so I only needed this to create the markers which show the ship directions in the model:

  

def gmaps4rails_marker_picture
  course = self.last_course 
  { 
     "picture" => "/images/shipred/meshes" + sprintf("%04d",course.to_i) + ".png", 
     "width" => "32", 
     "height" => "32" 
  } 
end
(the marker pictures, one for each direction are in /images/shipred/meshes0000.png where 0000 can go up to 0365).

In the controller I have something like this:

  @markers << vessel.to_gmaps4rails

for the markers. For the lines that I draw behing the ships I needed to create a JSON structure. Easy enough. Rails can do that. All we need is an empty array, where add one array per line to, with the first element of the array in the array the color of the line that we wish to draw.

  @polylines = [] 
  ...
  for vessel in @vessels #then in a loop we make a new line 
     @polyline = [{"strokeColor"=>color}] 
     for position in vessel.todaypositions #todaypositions returns the positions that the vessel had today 
         @polyline << {"longitude" => postion[:lon],"latitude" => position[:lat] } 
     end 
     @polylines << @polyline 
  end

That sums up our controller.
This is my view:
  <%= 
      gmaps( { 
         "map_options" => {"map_container_id" => "map_container", "auto_adjust" => "false"}, 
         "markers" => { "data" => @markers }, 
         "polylines" => { "data" => @polylines.to_json } 
      }) %>

That sums it up. The result: A google map with shipping info and a trail behind every marker on the screen and look ma, I didn't need to use any javascript.

 

I just upgraded to Rails 3 on an Apache box running mod_passenger.

All works fine until I use a gem that utilizes Git:

Gemfile:

gem 'will_paginate' #, :git => 'git://github.com/mislav/will_paginate.git', :branch => 'rails3'


bundle install and bundle pack will do their voodoo, however Passenger will not stop giving errors like the one below.
This seems to be something in either bundler or passenger, and relates to where the gems are installed.

The curious thing is that bundle check will tell me that will_paginate has been installed correctly.

The solution for me was to create a directory called .bundle in my rails root. create a file called configand place the following line in it:

BUNDLE_PATH: /home/.path..to...your...rails...root/.bundler

copy it exactly and adapt the path. now
run bundle install and restart apache.
Voila. Problem solved.




Ruby on Rails application could not be started

These are the possible causes:
  • There may be a syntax error in the application's code. Please check for such errors and fix them.
  • A required library may not installed. Please install all libraries that this application requires.
  • The application may not be properly configured. Please check whether all configuration files are written correctly, fix any incorrect configurations, and restart this application.
  • A service that the application relies on (such as the database server or the Ferret search engine server) may not have been started. Please start that service.
Further information about the error may have been written to the application's log file. Please check it in order to analyse the problem.
Error message:
git://github.com/mislav/will_paginate.git (at rails3) is not checked out. Please run `bundle install` (Bundler::GitError)
Exception class:
PhusionPassenger::UnknownError
 

Exporting STL files from a 3d package can be a pain. I've had exports fail completely.

My Makerbot wouldn't import the Gcode and STL files .

Cinema 4d can export STL files but its often not clear why exports fail.

The catch is to set the scale to 0.01 (0.01 meters = 10cm which is the dimension of the platform of the Makerbot), even if your model is 10x10x10cm large.



After that, startup ReplicatorG and import the STL directly into it. Ive had loads of troubles with Skeinforge, but importing an STL into ReplicatorG seems to work fine for my setup.

 

First post...

| Geen reacties
Well, this is my first post on my development weblog. I'll try to post stuff about software development here.

 

Recente mediabestanden

De nieuwste berichten zijn te vinden op de hoofdpagina of kijk in de archieven om alle berichten te zien.