Skip to content

Commit

Permalink
Blank Map loaded with call to API (Part 2 of publiclab#1934) (publicl…
Browse files Browse the repository at this point in the history
…ab#2044)

* Markers loaded from API

* self.notes_map updated

* API url updated

* https instead of http

* MORE THAN 1 MAP ON A PAGE

* Url generalised

* Indentation done

* Indentation 2
  • Loading branch information
sagarpreet-chadha authored and Souravirus committed Mar 12, 2018
1 parent 57a6118 commit 127c997
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
13 changes: 2 additions & 11 deletions app/models/concerns/node_shared.rb
Expand Up @@ -170,26 +170,17 @@ def self.upgrades_grid(body)
end
end

#Blank map loaded only , markers will be loaded using API call .
def self.notes_map(body)
body.gsub(/[^\>`](\<p\>)?\[map\:content\:(\S+)\:(\S+)\]/) do |_tagname|
lat = Regexp.last_match(2)
lon = Regexp.last_match(3)
nids = NodeTag.joins(:tag)
.where('name LIKE ?', 'lat:' + lat[0..lat.length - 2] + '%')
.collect(&:nid)
nids = nids || []
items = Node.includes(:tag)
.references(:node, :term_data)
.where('node.nid IN (?) AND term_data.name LIKE ?', nids, 'lon:' + lon[0..lon.length - 2] + '%')
.limit(200)
.order('node.nid DESC')
a = ActionController::Base.new()
output = a.render_to_string(template: "map/_leaflet",
layout: false,
locals: {
lat: lat,
lon: lon,
items: items
lon: lon
}
)
output
Expand Down
35 changes: 23 additions & 12 deletions app/views/map/_leaflet.html.erb
@@ -1,14 +1,25 @@
<% unique_id = rand(100) %>
<style>
<% unique_id = rand(100) %>
<style>
#map<%= unique_id %> { width:100%; height:300px; margin: 0; position: relative;}
</style>
<div class="leaflet-map" id="map<%= unique_id %>"></div>
<% if defined? people %><p><i><small>Share your own location on <a href='/profile'>your profile</a>.</small></i></p><% end %>
<script>
var map = L.map('map<%= unique_id %>').setView([<%= lat %>,<%= lon %>], <%= lat.to_s.length.to_i %> + 6);
L.tileLayer("//a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png").addTo(map);
</style>
<div class="leaflet-map" id="map<%= unique_id %>"></div>
<% if defined? people %><p><i><small>Share your own location on <a href='/profile'>your profile</a>.</small></i></p><% end %>
<script>
var map<%= unique_id %> = L.map('map<%= unique_id %>').on('load', onMapLoad).setView([<%= lat %>,<%= lon %>], <%= lat.to_s.length.to_i %> + 6);
L.tileLayer("//a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png").addTo(map<%= unique_id %>);
function onMapLoad(e){
var lat = <%= lat %> ;
var lon = <%= lon %> ;
var s = lat+","+lon ;
$.getJSON("/api/srch/locations?srchString="+s , function(data){
if (!!data.items){
for (i = 0; i < data.items.length ; i++) {
var url = data.items[i].docUrl ;
var title = data.items[i].docTitle ;
L.marker([data.items[i].latitude , data.items[i].longitude]).addTo(map<%= unique_id %>).bindPopup("<a href=" + url + ">" + title + "</a>") ;
}
}
});
}
</script>

<% items.each do |item| %>
L.marker([<%= item.lat %>, <%= item.lon %>]).addTo(map).bindPopup("<a href='<%= item.path %>'><%= item.title %></a>");
<% end %>
</script>

0 comments on commit 127c997

Please sign in to comment.