Skip to content

Commit 02aeac0

Browse files
I added map functionality and added the map to the index page. The layout and color scheme still needs to updated for the map though.
1 parent d3c2be0 commit 02aeac0

15 files changed

+332
-7
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ gem 'sassc-rails'
1717
gem 'simple_form'
1818
gem 'uglifier'
1919
gem 'webpacker'
20+
gem 'geocoder'
2021

2122
group :development do
2223
gem 'web-console', '>= 3.3.0'

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ GEM
7777
ffi (1.12.2)
7878
font-awesome-sass (5.6.1)
7979
sassc (>= 1.11)
80+
geocoder (1.6.1)
8081
globalid (0.4.2)
8182
activesupport (>= 4.2.0)
8283
http-accept (1.7.0)
@@ -221,6 +222,7 @@ DEPENDENCIES
221222
devise
222223
dotenv-rails
223224
font-awesome-sass (~> 5.6.1)
225+
geocoder
224226
jbuilder (~> 2.0)
225227
listen (~> 3.0.5)
226228
pg (~> 0.21)

app/controllers/masks_controller.rb

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ def index
1111
else
1212
@masks = Mask.all
1313
end
14+
@masks = Mask.geocoded #returns mask with coordinates
15+
16+
@markers = @masks.map do |mask|
17+
{
18+
lat: mask.latitude,
19+
lng: mask.longitude
20+
}
21+
end
1422
end
1523

1624
def show

app/javascript/packs/application.js

+7
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ loadDynamicBannerText();
77

88
import { toggleDateInputs } from '../plugins/flatpickr'
99
toggleDateInputs();
10+
11+
// import "bootstrap";
12+
import 'mapbox-gl/dist/mapbox-gl.css'; // <-- you need to uncomment the stylesheet_pack_tag in the layout!
13+
14+
import { initMapbox } from '../plugins/init_mapbox';
15+
16+
initMapbox();

app/javascript/packs/map.js

Whitespace-only changes.

app/javascript/plugins/init_mapbox.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import mapboxgl from 'mapbox-gl';
2+
3+
const mapElement = document.getElementById('map');
4+
5+
const buildMap = () => {
6+
mapboxgl.accessToken = mapElement.dataset.mapboxApiKey;
7+
return new mapboxgl.Map({
8+
container: 'map',
9+
style: 'mapbox://styles/mapbox/streets-v10'
10+
// style: 'mapbox://styles/bmchavez1991/ck759mmsp0ath1imoksa292rw'
11+
});
12+
};
13+
14+
const addMarkersToMap = (map, markers) => {
15+
markers.forEach((marker) => {
16+
new mapboxgl.Marker()
17+
.setLngLat([ marker.lng, marker.lat ])
18+
.addTo(map);
19+
});
20+
};
21+
22+
const fitMapToMarkers = (map, markers) => {
23+
const bounds = new mapboxgl.LngLatBounds();
24+
markers.forEach(marker => bounds.extend([ marker.lng, marker.lat ]));
25+
map.fitBounds(bounds, { padding: 70, maxZoom: 15 });
26+
};
27+
28+
const initMapbox = () => {
29+
if (mapElement) {
30+
const map = buildMap();
31+
const markers = JSON.parse(mapElement.dataset.markers);
32+
addMarkersToMap(map, markers);
33+
fitMapToMarkers(map, markers);
34+
}
35+
};
36+
37+
export { initMapbox };

app/models/mask.rb

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Mask < ApplicationRecord
88
validates :photo, attached: true
99
validates :category, presence: true
1010
validates :address, presence: true
11+
geocoded_by :address
12+
after_validation :geocode, if: :will_save_change_to_address?
1113

1214
def unavailable_dates
1315
bookings.pluck(:start_date, :end_date).map do |range|
@@ -16,3 +18,4 @@ def unavailable_dates
1618
end
1719

1820
end
21+

app/views/layouts/application.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<%= csrf_meta_tags %>
1010
<%= action_cable_meta_tag %>
1111
<%= stylesheet_link_tag 'application', media: 'all' %>
12-
1312
<%#= stylesheet_pack_tag 'application', media: 'all' %>
1413
<!-- Uncomment if you import CSS in app/javascript/packs/application.js -->
1514
<%= stylesheet_pack_tag 'application'%> <!-- Uncomment if you import CSS in app/javascript/packs/application.js -->
@@ -29,3 +28,4 @@
2928

3029
</html>
3130

31+

app/views/masks/index.html.erb

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
<a class="submit-button" href="">Submit</a>
2424
</div>
2525

26-
26+
<div
27+
id="map"
28+
style="width: 100%;
29+
height: 600px;"
30+
data-markers="<%= @markers.to_json %>"
31+
data-mapbox-api-key="<%= ENV['MAPBOX_API_KEY'] %>"
32+
></div>
2733

2834
<!-- if statement because of Heroku -->
2935

app/views/pages/search.html.erb

-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@
3333
</div>
3434
</div>
3535
</div>
36-

config/initializers/geocoder.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Geocoder.configure(
2+
# Geocoding options
3+
# timeout: 3, # geocoding service timeout (secs)
4+
lookup: :nominatim, # name of geocoding service (symbol)
5+
# ip_lookup: :ipinfo_io, # name of IP address geocoding service (symbol)
6+
# language: :en, # ISO-639 language code
7+
# use_https: false, # use HTTPS for lookup requests? (if supported)
8+
# http_proxy: nil, # HTTP proxy server (user:pass@host:port)
9+
# https_proxy: nil, # HTTPS proxy server (user:pass@host:port)
10+
api_key: 'pk.eyJ1IjoiYm1jaGF2ZXoxOTkxIiwiYSI6ImNrNzU1ZnJpNDA4YmMzZXBmd3c5NmRoOXgifQ.hNPXY1Tt34Ps_WNT_qDjjA', # API key for geocoding service
11+
# cache: nil, # cache object (must respond to #[], #[]=, and #del)
12+
# cache_prefix: 'geocoder:', # prefix (string) to use for all cache keys
13+
14+
# Exceptions that should not be rescued by default
15+
# (if you want to implement custom error handling);
16+
# supports SocketError and Timeout::Error
17+
# always_raise: [],
18+
19+
# Calculation options
20+
units: :km, # :km for kilometers or :mi for miles
21+
# distances: :linear # :spherical or :linear
22+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class AddCoordinatesTomask < ActiveRecord::Migration[5.2]
2+
def change
3+
end
4+
end

db/schema.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_02_27_110721) do
13+
ActiveRecord::Schema.define(version: 2020_02_27_192258) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"bootstrap": "^4.4.1",
77
"flatpickr": "^4.6.3",
88
"jquery": "^3.4.1",
9+
"mapbox-gl": "^1.8.1",
910
"popper.js": "^1.16.1",
1011
"typed.js": "^2.0.11"
1112
},

0 commit comments

Comments
 (0)