使用gmaps4rails显示多边形 [英] Displaying a polygon with gmaps4rails

查看:86
本文介绍了使用gmaps4rails显示多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby noob在这里.尝试使用gmaps4rails gem(顺便说一句很棒的宝石)在Google地图上将点列表显示为多边形.基于以下代码示例的任何建议将不胜感激!我可以看到地图的轮廓,但是看不到地图,也没有多边形.更新:此代码已更新,问题已解决.

Ruby noob here. Trying to display a list of points as a polygon on a google map using the gmaps4rails gem (awesome gem by the way). Any suggestions based on code sample below would be much appreciated! I can see the outline for the map, but no map and no polygon. Update: this code has been updated and the problem is solved.

Class Schoolpoint是属于School的经纬度对的列表

Class Schoolpoint is a list of lat/long pairs that belong to School

在我的控制器中:

@polyjson = []
schoolpoints = []    
Schoolpoint.where(:school_id => params[:id]).each do |point|
  schoolpoints << { :lng => point.longitude, :lat => point.latitude}
end
@polyjson = [schoolpoints]
@polyjson = @polyjson.to_json

然后在视图中:

<%= gmaps({"polygons" => { "data" => @polyjson }})

在Schoolpoint模型中:

in Schoolpoint model:

class Point < ActiveRecord::Base

  belongs_to :place

  acts_as_gmappable :process_geocoding => false

  def gmaps4rails_address
    "#{longitude}, #{latitude}" 
  end
end

更新:此错误现在不再存在,但为防止它对解决类似问题的人有所帮助,我将其保留.最后,这是带有未捕获的Syntaxerror =>意外令牌的结果js.

Update: This error now longer exists, but I've left it in case its helpful to someone dealing with similar problem. Finally, here is the resulting js with an uncaught Syntaxerror => unexpected token.

Gmaps.map = new Gmaps4RailsGoogle();
Gmaps.load_map = function() {
Gmaps.map.initialize();
Gmaps.map.polygons = [[{"lng"=>-80.190262, "lat"=>25.774252, "strokeColor"=>"#FF0000", "strokeOpacity"=>0.3, "strokeWeight"=>1, "fillColor"=>"#FF0000", "fillOpacity"=>0.7}, {"lng"=>-87.6245284080505, "lat"=>41.8868315803506}, {"lng"=>-87.6241636276245, "lat"=>41.8674515900783}, {"lng"=>-87.6203870773315, "lat"=>41.8674835487326}, {"lng"=>-87.6167392730712, "lat"=>41.8579591627635}, {"lng"=>-87.6348495483398, "lat"=>41.8577034549953}, {"lng"=>-87.6342701911926, "lat"=>41.8588701133785}, {"lng"=>-87.6341199874878, "lat"=>41.858946025344}, {"lng"=>-87.6341146230697, "lat"=>41.8590858629394}, {"lng"=>-87.6341199874878, "lat"=>41.8600767034266}, {"lng"=>-87.6342219114303, "lat"=>41.8612433185139}, {"lng"=>-87.634157538414, "lat"=>41.8613112372298}, {"lng"=>-87.6342540979385, "lat"=>41.8621502271823}, {"lng"=>-87.6341950893402, "lat"=>41.8622580965204}, {"lng"=>-87.6342433691024, "lat"=>41.8626336402037}, {"lng"=>-87.6341092586517, "lat"=>41.8630930789441}, {"lng"=>-87.6342326402664, "lat"=>41.8631010691539}, {"lng"=>-87.6342862844467, "lat"=>41.8651984646832}, {"lng"=>-87.6342165470123, "lat"=>41.865314318812}, {"lng"=>-87.6342540979385, "lat"=>41.865929540668}, {"lng"=>-87.6343238353729, "lat"=>41.8661652409794}, {"lng"=>-87.6343667507171, "lat"=>41.8664728485533}, {"lng"=>-87.6342701911926, "lat"=>41.866564731048}, {"lng"=>-87.6343882083892, "lat"=>41.8673317449823}, {"lng"=>-87.6344525814056, "lat"=>41.8680388278011}, {"lng"=>-87.6346457004547, "lat"=>41.8691693450993}, {"lng"=>-87.6346671581268, "lat"=>41.8696886572982}, {"lng"=>-87.6345813274383, "lat"=>41.8698804022745}, {"lng"=>-87.6347583532333, "lat"=>41.869992253245}, {"lng"=>-87.634892463684, "lat"=>41.8706873227465}, {"lng"=>-87.6353269815445, "lat"=>41.8726167002032}, {"lng"=>-87.6352626085281, "lat"=>41.8728443868687}, {"lng"=>-87.6354557275772, "lat"=>41.8730081609862}, {"lng"=>-87.6353698968887, "lat"=>41.8732797854267}, {"lng"=>-87.6356971263885, "lat"=>41.8740227522642}, {"lng"=>-87.6356971263885, "lat"=>41.8746458790817}, {"lng"=>-87.6359224319458, "lat"=>41.87509724279}, {"lng"=>-87.6361316442489, "lat"=>41.8754088017203}, {"lng"=>-87.6364105939865, "lat"=>41.8754727110567}, {"lng"=>-87.6364642381668, "lat"=>41.8757642965932}, {"lng"=>-87.6371240615844, "lat"=>41.876678987795}, {"lng"=>-87.637939453125, "lat"=>41.8801059676767}, {"lng"=>-87.6379930973053, "lat"=>41.8806172030015}, {"lng"=>-87.6378536224365, "lat"=>41.8829017358812}, {"lng"=>-87.6375961303711, "lat"=>41.8844593251054}, {"lng"=>-87.6372849941253, "lat"=>41.8857213439117}, {"lng"=>-87.6371347904205, "lat"=>41.8860408383893}, {"lng"=>-87.6355576515197, "lat"=>41.8870552227663}, {"lng"=>-87.6282513141632, "lat"=>41.8870951588295}, {"lng"=>-87.6281654834747, "lat"=>41.8868076186168}]];
153:2439 Uncaught SyntaxError: Unexpected token >
Gmaps.map.create_polygons();
Gmaps.map.adjustMapToBounds();
Gmaps.map.callback();
};
window.onload = function() { Gmaps.loadMaps(); };

推荐答案

我通过了他们的基础教程(来自屏幕投射),标记效果很好.但是我对折线有问题(与多边形一样).最后,我解决了我的问题(它可以帮助您处理多边形). 因此,视图与他们给出的视图相同:

I passed their base tutorial (from screen cast) and markers works fine. But I had problem with polylines (as you with polygons). At the end I resolved my problem (it can help you with polygons). So, view is the same as they gave :

<%= gmaps({
       "polylines"    => { "data" => @bemap_polylines }
     })
 %>

模型也与他们相同.

class Character < ActiveRecord::Base
  belongs_to :bemap
  acts_as_gmappable

  def gmaps4rails_address
    #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
    address
    logger.info address
  end

end

因此,主要问题在于控制器.这是我的:

So, the main problem was into controller. Here is mine :

  def show
    @bemap = Bemap.find(params[:id])
    @bemap_polylines = []
    @bemap_characters = []
    @bemap.characters.each do |v|
      @bemap_characters << { :lng => v[:longitude], :lat => v[:latitude]}
    end
    @bemap_polylines << @bemap_characters
    @bemap_polylines =  @bemap_polylines.to_json
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @bemap }
    end
  end

这篇关于使用gmaps4rails显示多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆