Last active
December 21, 2015 10:59
-
-
Save delba/6295364 to your computer and use it in GitHub Desktop.
Map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ClubController < ApplicationController | |
| def address | |
| address = Geocoder.search([params[:lat], params[:lng]])[0].try(:formatted_address) | |
| render json: { address: address } | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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