Skip to content

Instantly share code, notes, and snippets.

@delba
Last active December 21, 2015 10:59
Show Gist options
  • Select an option

  • Save delba/6295364 to your computer and use it in GitHub Desktop.

Select an option

Save delba/6295364 to your computer and use it in GitHub Desktop.
Map
class Club < ActiveRecord::Base
def self.center
@center ||= south_west.zip(north_east).map do |a|
(a[0] + a[1]) / 2
end
end
def self.south_west
@south_west ||= [minimum(:lat), minimum(:lng)]
end
def self.north_east
@north_east ||= [maximum(:lat), minimum(:lng)]
end
def coordinates
[lat, lng]
end
end
class ClubController < ApplicationController
def address
address = Geocoder.search([params[:lat], params[:lng]])[0].try(:formatted_address)
render json: { address: address }
end
end
<div id="map"></div>
<%= javascript_tag do %>
Club.southWest = <%= Club.south_west %>
Club.northEast = <%= Club.north_east %>
window.clubs = <%= @clubs.to_json.html_safe %>
<% end %>
window.Club = {}
map = new L.mapbox.map 'map', 'example.map-777',
attributionControl: false
map.fitBounds([Club.southWest, Club.northEast])
map.locate().on 'locationfound', (e) ->
window.currentLocation = L.marker e.latlng, title: 'You current Location'
currentLocation.addTo map
map.on 'click', (e) ->
latlng = e.latlng
distance = latlng.distanceTo currentLocation.getLatLng()
popup = L.popup().setLatLng(latlng).setContent("Requesting address")
popup.openOn map
$.ajax
type: 'GET'
url: 'clubs/address'
dataType: 'json'
data: { lat: latlng.lat, lng: latlng.lng }
success: (json) ->
L.popup().setLatLng(latlng)
.setContent(json.address)
.openOn map
$.ajax
type: 'GET'
url: '/clubs'
dataType: 'json'
success: (clubs) ->
L.marker(club.coordinates, title: club.name)
.bindPopup(club.name)
.addTo map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment