user567592
user567592

Reputation: 181

Google Maps API with Rails 3.0

I need some help,showing a map inside my rails 3.0 pages I fond cartographer in the stack overflow post Google Maps API with Rails 3.0

I followed the instructions from https://github.com/parolkar/cartographer. In addition I created a cartographer.yml and saved it in the rails_app/config directory with the Google keys for localhost:3000 and localhost:3000/gmap/

my controller looks like:

 def show
    @place = Place.find(params[:id])

    @map = Cartographer::Gmap.new( 'map' )
    @map.zoom = :bound
    marker1 = Cartographer::Gmarker.new(:name=> "taj_mahal", :marker_type => "Building",
              :position => [27.173006,78.042086],
              :info_window_url => "/url_for_info_content")
    marker2 = Cartographer::Gmarker.new(:name=> "raj_bhawan", :marker_type => "Building",
              :position => [28.614309,77.201353],
              :info_window_url => "/url_for_info_content")

    @map.markers << marker1
    @map.markers << marker2


    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @place }
    end
  end

and I got...

undefined method `name' for :normal:Symbol

Extracted source (around line #44):

41: </p>
42: 
43: <%= raw Cartographer::Header.new.to_s %>
44: <%= raw @map.to_html %>
45: 
46: <%= link_to 'Edit', edit_place_path(@place) %> |
47: <%= link_to 'Back', places_path %>

Thanks ...

Upvotes: 1

Views: 1987

Answers (4)

Nic Aitch
Nic Aitch

Reputation: 628

EDIT: This has been fixed in the newest revision :) Be sure to check the readme.


I've fixed this issue in my fork of Cartographer and submitted a pull request to the joshuamiller (author) to merge my fixes.

Before that happens though, you can make these edits to fix this issue:

The README leaves out that you also need to initialize the icons. Add this to your controller

@map.icons << Cartographer::Gicon.new

Then replace line 9 of gmarker.rb with

@icon = options[:icon] || Cartographer::Gicon.new

Or just clone my fork

[email protected]:nicinabox/cartographer.git

Upvotes: 1

CharlieDrPepper
CharlieDrPepper

Reputation: 11

I don't necessarily want to break the plugin for later in terms of icons later, so instead of removing the @icon.name I just put it in a begin, rescue block.. great catch though.

script << "#{@name} = new google.maps.Marker({map: null,position: new google.maps.LatLng(#{@position[0]}, #{@position[1]}), draggable: #{@draggable}, icon: #{@icon.name}}); \n"

To:

begin
  script << "#{@name} = new google.maps.Marker({map: null,position: new google.maps.LatLng(#{@position[0]}, #{@position[1]}), draggable: #{@draggable}, icon: #{@icon.name}}); \n"
rescue
  script << "#{@name} = new google.maps.Marker({map: null,position: new google.maps.LatLng(#{@position[0]}, #{@position[1]}), draggable: #{@draggable}}); \n"
end

Upvotes: 1

user567592
user567592

Reputation: 181

I got this from Abhishek Parolkar and it works ...

class WelcomeController < ApplicationController
  def index
    @map = initialize_map()
    @map.zoom = :bound
    @icon_org = Cartographer::Gicon.new(:name => "org",
          :image_url => '/images/org_icon.gif',
          :shadow_url => '/images/org_icon.gif',
          :width => 32,
          :height => 23,
          :shadow_width => 32,
          :shadow_height => 23,
          :anchor_x => 0,
          :anchor_y => 20,
          :info_anchor_x => 5,
          :info_anchor_x => 1)
    # Add the icons to map
    @map.icons <<  @icon_org
    @marker1 = Cartographer::Gmarker.new(:name=> "org11", :marker_type => "Organization",
              :position => [27.173006,78.042086],
              :info_window_url => "/welcome/sample_ajax",
              :icon => @icon_org) 
    @marker2 = Cartographer::Gmarker.new(:name=> "org12", :marker_type => "Organization",
              :position => [13.031332, -24.09375],
              :info_window_url => "/welcome/sample_ajax",
              :icon => @icon_org)          

    @map.markers << @marker1
    @map.markers << @marker2

  end
  def test

  end
  def sample_ajax
    render :text => "Success"
  end
  private
  def initialize_map
     @map = Cartographer::Gmap.new( 'map' )    
     @map.controls << :type
     @map.controls << :large
     @map.controls << :scale
     @map.controls << :overview
     @map.debug = false 
     @map.marker_mgr = false
     @map.marker_clusterer = true

     cluster_icons = []


     org = Cartographer::ClusterIcon.new({:marker_type => "Organization"})
      org << {
                  :url => '/images/drop.gif',
                  :height => 73,
                   :width => 118,
                  :opt_anchor => [10, 0],
                  :opt_textColor => 'black'
                }
         #push second variant
      org << {
                  :url => '/images/drop.gif',
                  :height => 73,
                  :width => 118,
                  :opt_anchor => [20, 0],
                  :opt_textColor => 'black'
                }

        #push third variant
       org << {
                  :url => '/images/drop.gif',
                  :height => 73,
                  :width => 118,
                  :opt_anchor => [26, 0],
                  :opt_textColor => 'black'
              }
      cluster_icons << org



      @map.marker_clusterer_icons = cluster_icons



     return @map
   end

end

and in the view

<%= raw Cartographer::Header.new.to_s %>
<%= raw @map.to_html %>

<div style="width:600px;height:400px;" id="map" > [Map]</div> 

Upvotes: 1

Lucas Efe
Lucas Efe

Reputation: 146

Same problem here. Quick fix for this is to go to the following file:

vendor/plugins/cartographer/lib/v3/cartographer/gmarker.rb, line 57, and

change it from:

script << "#{@name} = new google.maps.Marker({map: null,position: new google.maps.LatLng(#{@position[0]}, #{@position[1]}), draggable: true, icon: #{@icon.name}}); \n"

to:

script << "#{@name} = new google.maps.Marker({map: null,position: new google.maps.LatLng(#{@position[0]}, #{@position[1]}), draggable: true}); \n"

No icon, but the map will show up. This will work at least until they fix the problem.

Also, as a plus, remember to give some dimension to the map's div, otherwise it will not show up :).

.map {
  border: black 0.5px solid;
  width: 400px;
  height: 400px;
}

Right now, for me this is working.

Upvotes: 2

Related Questions