multiple leaflet maps using div and rails not displaying second map

56 views
Skip to first unread message

Kiran Patil

unread,
Jul 18, 2016, 9:31:21 AM7/18/16
to Leaflet
Hi,

I am trying to display multiple maps on new window using divs on rails as below and at any given time it displays only one map which is top one from js file.

Please help me to resolve this issue.

app/views/show_map/index.html.erb file:
---------------------------------------------------------
<%= button_to('Map1', {:action => 'show1'}, {:onclick => "this.form.target = 'mywin'; window.open('','mywin', 'height = 650, width = 750, top = 20, left = 100, scrollbars = 0, toolbar = 0, status = 0, menubar = 0, location = 0')"}) %>

<%= button_to('Map2', {:action => 'show2'}, {:onclick => "this.form.target = 'mywin1'; window.open('','mywin1', 'height = 650, width = 750, top = 20, left = 100, scrollbars = 0, toolbar = 0, status = 0, menubar = 0, location = 0')"}) %>

app/views/show_map/show1.html.erb file:
---------------------------
<div id="map1" class="map" ></div>

app/views/show_map/show2.html.erb file:
-----------------------------------
<div id="map" class="map" ></div>

app/assets/javascripts/map.js file:
-------------------------------------------------
$('document').ready(function() {

var mymap = L.map('map1').setView([gon.lat1, gon.lng1], 13);


L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(mymap);

var marker = L.marker([gon.lat1, gon.lng1]).addTo(mymap);

marker.bindPopup("<b>Hello world!</b><br>I am a popup 1.").openPopup();

var map = L.map('map').setView([gon.lat1, gon.lng1], 14);

L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

var marker = L.marker([gon.lat1, gon.lng1]).addTo(map);

marker.bindPopup("<b>Hello Friend!</b><br>I am a popup 2.").openPopup();

});


show_map_controller.rb
---------------------------------
class ShowMapController < ApplicationController
  def index
  end

  def show1
     @lat1 = 12.9773
     @lng1 = 77.5712

     gon.lat1 = @lat1
     gon.lng1 = @lng1

  end

  def show2
     @lat1 = 12.9773
     @lng1 = 77.5712

     gon.lat1 = @lat1
     gon.lng1 = @lng1
  end
end

config/routes.rb file
-----------------------------
Rails.application.routes.draw do

  get 'show_map/index'
  get 'show_map/show'
  get 'show_map/show_route'
  post 'show_map/show'
  post 'show_map/show_route'
  post 'show_map/index'

  get 'home/index'
  root to: "home#index"



Thanks,
Kiran.
Reply all
Reply to author
Forward
0 new messages