Skip to content

Commit

Permalink
added ability save and show point on the google map
Browse files Browse the repository at this point in the history
  • Loading branch information
munya committed Aug 16, 2012
1 parent e1bfeae commit 52e4289
Show file tree
Hide file tree
Showing 22 changed files with 429 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -22,6 +22,8 @@ group :assets do
end

gem 'jquery-rails'
gem 'rgeo'
gem 'activerecord-postgis-adapter'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Expand Up @@ -22,6 +22,9 @@ GEM
activesupport (= 3.2.3)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activerecord-postgis-adapter (0.4.3)
pg (>= 0.11.0)
rgeo-activerecord (~> 0.4.5)
activeresource (3.2.3)
activemodel (= 3.2.3)
activesupport (= 3.2.3)
Expand Down Expand Up @@ -80,6 +83,11 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
rgeo (0.3.15)
rgeo-activerecord (0.4.5)
activerecord (>= 3.0.3)
arel (>= 2.0.6)
rgeo (>= 0.3.10)
sass (3.1.20)
sass-rails (3.2.5)
railties (~> 3.2.0)
Expand All @@ -103,10 +111,12 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord-postgis-adapter
coffee-rails (~> 3.2.1)
jquery-rails
json
pg
rails (= 3.2.3)
rgeo
sass-rails (~> 3.2.3)
uglifier (>= 1.0.3)
14 changes: 14 additions & 0 deletions app/assets/javascripts/application.js
Expand Up @@ -13,3 +13,17 @@
//= require jquery
//= require jquery_ujs
//= require_tree .


function findMe() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
document.getElementById('checkin_latitude').value = position.coords.latitude;
document.getElementById('checkin_longitude').value = position.coords.longitude;
}, function() {
alert('We couldn\'t find your position.');
});
} else {
alert('Your browser doesn\'t support geolocation.');
}
}
3 changes: 3 additions & 0 deletions app/assets/javascripts/checkins.js.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/checkins.css.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the checkins controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
56 changes: 56 additions & 0 deletions app/assets/stylesheets/scaffolds.css.scss
@@ -0,0 +1,56 @@
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px; }

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px; }

pre {
background-color: #eee;
padding: 10px;
font-size: 11px; }

a {
color: #000;
&:visited {
color: #666; }
&:hover {
color: #fff;
background-color: #000; } }

div {
&.field, &.actions {
margin-bottom: 10px; } }

#notice {
color: green; }

.field_with_errors {
padding: 2px;
background-color: red;
display: table; }

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff; }
ul li {
font-size: 12px;
list-style: square; } }
90 changes: 90 additions & 0 deletions app/controllers/checkins_controller.rb
@@ -0,0 +1,90 @@
class CheckinsController < ApplicationController
# GET /checkins
# GET /checkins.json
def index
@checkins = Checkin.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @checkins }
end
end

# GET /checkins/1
# GET /checkins/1.json
def show
@checkin = Checkin.find(params[:id])

@nearby_checkins = Checkin.nearby_to(@checkin, 1000)

respond_to do |format|
format.html # show.html.erb
format.json { render json: @checkin }
end
end

# GET /checkins/new
# GET /checkins/new.json
def new
@checkin = Checkin.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @checkin }
end
end

# GET /checkins/1/edit
def edit
@checkin = Checkin.find(params[:id])
end

# POST /checkins
# POST /checkins.json
def create
@checkin = Checkin.new(params[:checkin])

if @checkin.valid?
puts '---controller'
puts @checkin.inspect
end

respond_to do |format|
if @checkin.save
format.html { redirect_to @checkin, notice: 'Checkin was successfully created.' }
format.json { render json: @checkin, status: :created, location: @checkin }
else
format.html { render action: "new" }
format.json { render json: @checkin.errors, status: :unprocessable_entity }
end
end
end

# PUT /checkins/1
# PUT /checkins/1.json
def update
@checkin = Checkin.find(params[:id])

respond_to do |format|
if @checkin.update_attributes(params[:checkin])
format.html { redirect_to @checkin, notice: 'Checkin was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @checkin.errors, status: :unprocessable_entity }
end
end
end

# DELETE /checkins/1
# DELETE /checkins/1.json
def destroy
@checkin = Checkin.find(params[:id])
@checkin.destroy

respond_to do |format|
format.html { redirect_to checkins_url }
format.json { head :no_content }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/checkins_helper.rb
@@ -0,0 +1,2 @@
module CheckinsHelper
end
24 changes: 24 additions & 0 deletions app/models/checkin.rb
@@ -0,0 +1,24 @@
class Checkin < ActiveRecord::Base
attr_accessible :location, :title, :latitude, :longitude

scope :nearby_to,
lambda { |checkin, max_distance|
where("ST_DWithin(location, ?, ?) AND id != ?", checkin.location, max_distance, checkin.id)
}

def latitude
self.location.nil? ? 0 : self.location.y
end

def latitude=(value)
self.location = "POINT(#{longitude} #{value})"
end

def longitude
self.location.nil? ? 0 : self.location.x
end

def longitude=(value)
self.location = "POINT(#{value} #{latitude})"
end
end
35 changes: 35 additions & 0 deletions app/views/checkins/_form.html.erb
@@ -0,0 +1,35 @@
<%= form_for(@checkin) do |f| %>
<% if @checkin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@checkin.errors.count, "error") %> prohibited this checkin from being saved:</h2>

<ul>
<% @checkin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :title %>
<br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :latitude %>
<br/>
<%= f.text_field :latitude %>
</div>
<div class="field">
<%= f.label :longitude %>
<br/>
<%= f.text_field :longitude %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>


<input type="button" value="Find me!" onclick="findMe();" />
6 changes: 6 additions & 0 deletions app/views/checkins/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing checkin</h1>

<%= render 'form' %>
<%= link_to 'Show', @checkin %> |
<%= link_to 'Back', checkins_path %>
62 changes: 62 additions & 0 deletions app/views/checkins/index.html.erb
@@ -0,0 +1,62 @@
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

<div id="map" style="width: 600px; height: 500px;"></div>

<h1>Listing checkins</h1>

<table>
<tr>
<th>Title</th>
<th>Location</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @checkins.each do |checkin| %>
<tr>
<td><%= checkin.title %></td>
<td><%= checkin.location %></td>
<td><%= link_to 'Show', checkin %></td>
<td><%= link_to 'Edit', edit_checkin_path(checkin) %></td>
<td><%= link_to 'Destroy', checkin, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Checkin', new_checkin_path %>

<script type="text/javascript">
// Create the map
var map = new google.maps.Map(document.getElementById("map"), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});

// Initialize the bounds container
var bounds = new google.maps.LatLngBounds();

<% @checkins.each do |checkin| %>
// Create the LatLng
var latLng = new google.maps.LatLng(<%= checkin.latitude %>, <%= checkin.longitude %>);

// Create the marker
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: '<%= escape_javascript(checkin.title) %>'
});

// Add click event
google.maps.event.addListener(marker, 'click', function() {
document.location = '<%= checkin_path(checkin) %>';
});

// Extend the bounds
bounds.extend(latLng);
<% end %>

// Fit to bounds
map.fitBounds(bounds);
</script>
5 changes: 5 additions & 0 deletions app/views/checkins/new.html.erb
@@ -0,0 +1,5 @@
<h1>New checkin</h1>

<%= render 'form' %>
<%= link_to 'Back', checkins_path %>
22 changes: 22 additions & 0 deletions app/views/checkins/show.html.erb
@@ -0,0 +1,22 @@
<p id="notice"><%= notice %></p>

<p>
<b>Title:</b>
<%= @checkin.title %>
</p>

<p>
<b>Location:</b>
<%= @checkin.location %>
</p>


<%= link_to 'Edit', edit_checkin_path(@checkin) %> |
<%= link_to 'Back', checkins_path %>

<h2>Nearby check-ins</h2>
<ul>
<% @nearby_checkins.each do |checkin| %>
<li><%= link_to checkin.title, checkin %></li>
<% end %>
</ul>
2 changes: 2 additions & 0 deletions config/application.rb
@@ -1,6 +1,8 @@
require File.expand_path('../boot', __FILE__)

require 'rails/all'
#require 'spatial_adapter/postgresql'
require 'active_record/connection_adapters/postgis_adapter/railtie'

if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Expand Down

0 comments on commit 52e4289

Please sign in to comment.