Skip to content

Commit

Permalink
set pin map.
Browse files Browse the repository at this point in the history
  • Loading branch information
sizer committed Apr 1, 2018
1 parent 8dc92f4 commit 93dc8b2
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 7 deletions.
3 changes: 0 additions & 3 deletions app/assets/javascripts/maps.coffee

This file was deleted.

39 changes: 39 additions & 0 deletions app/assets/javascripts/maps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function makeGmap(id, prop) {
return new google.maps.Map(document.getElementById(id), prop);
}

function makeGmapMarker(map, option) {
new google.maps.Marker(option).setMap(map);
}

function makeGmapLatlng(lat, lng) {
return new google.maps.LatLng(lat, lng);
}

function renderWholeMap() {
const prop = {
center: makeGmapLatlng(35.729503, 139.710900),
zoom: 2,
};
const map = makeGmap('gmap-container', prop);
makeGmapMarker(map, { position: prop.center });

const pins = document.getElementById('pins-data-list').children
for (let i = 0; i < pins.length; i++) {
const pin = pins[i];
makeGmapMarker(map, { position: makeGmapLatlng(pin.attributes['data-lat'].value, pin.attributes['data-lng'].value) });
}
}

function renderPin() {
const pins = document.getElementById('pins-data-list').children
for (let i = 0; i < pins.length; i++) {
const pin = pins[i];
const prop = {
center: makeGmapLatlng(pin.attributes['data-lat'].value, pin.attributes['data-lng'].value),
zoom: 12,
};
const map = makeGmap('gmap-container', prop);
makeGmapMarker(map, { position: prop.center});
}
}
4 changes: 2 additions & 2 deletions app/controllers/me/maps/pins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def create
@pin = @map.pins.build(pin_params)

if @pin.save
redirect_to me_map_path(id: @map), notice: 'Pin was successfully created.'
redirect_to edit_me_map_pin_path(id: @pin), notice: 'Pin was successfully created.'
else
render :new
end
end

def update
if @pin.update(pin_params)
redirect_to me_map_path(id: @map), notice: 'Pin was successfully updated.'
redirect_to edit_me_map_pin_path(id: @pin), notice: 'Pin was successfully updated.'
else
render :edit
end
Expand Down
12 changes: 12 additions & 0 deletions app/decorators/pin_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module PinDecorator # :nodoc:
def as_html_data
content_tag(:div,
'',
'class' => 'pin-data-item',
'data-name' => name,
'data-lat' => lat,
'data-lng' => lng)
end
end
5 changes: 5 additions & 0 deletions app/views/maps/_gmap_container.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#gmap-container[style='width: 100%; height: 520px;']
#pins-data-list
- map.pins.each do |pin|
= pin.as_html_data
= googlemap_include_tag callback: 'renderWholeMap'
2 changes: 2 additions & 0 deletions app/views/maps/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ p
ul
- @map.pins.each do |pin|
li = pin.name

= render partial: 'gmap_container', locals: { map: @map }
2 changes: 2 additions & 0 deletions app/views/me/maps/pins/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
= f.label :lng
= f.text_field :lng
.actions = f.submit

= render partial: 'gmap_container', locals: { pin: @pin }
5 changes: 5 additions & 0 deletions app/views/me/maps/pins/_gmap_container.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#gmap-container[style='width: 100%; height: 520px;']
#pins-data-list
= pin.as_html_data

= googlemap_include_tag callback: 'renderPin'
2 changes: 0 additions & 2 deletions app/views/me/maps/pins/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ h1 Editing pin

== render 'form'

=> link_to 'Show', me_map_pin_path(id: @pin)
'|
=< link_to 'Back', me_map_path(id: @pin.map)
2 changes: 2 additions & 0 deletions app/views/me/maps/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ br
=> link_to 'Edit', edit_me_map_path(@map)
'|
=< link_to 'Back', me_maps_path

= render partial: 'maps/gmap_container', locals: { map: @map }

0 comments on commit 93dc8b2

Please sign in to comment.