gmaps4rails replaceMarkers 不起作用(javascript) [英] gmaps4rails replaceMarkers not working (javascript)

查看:64
本文介绍了gmaps4rails replaceMarkers 不起作用(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

堆栈详细信息红宝石 1.9.2p180,导轨 3.0.9,gmaps4rails 1.0.2,jquery.json-2.3.min.js

stack details ruby 1.9.2p180, rails 3.0.9, gmaps4rails 1.0.2, jquery.json-2.3.min.js

背景我是 gmaps4rails 的新手,非常喜欢这个 gem.到目前为止一切正常,但我第一次尝试动态更新标记.我在 application.js 中执行以下操作:

Background I am a newbie to gmaps4rails and really like the gem. All is working well so far, but I am trying to update markers dynamically for the first time. I am doing the following in application.js:

var markers_json = $.toJSON(markers_array);
Gmaps.map.replaceMarkers(markers_json);

这不起作用并出现以下错误

This does not work and gives the following error

Uncaught TypeError: Cannot read property 'position' of undefined
extendBoundsWithMarkers in gmaps4rails.googlemaps.js:204
Gmaps4Rails.adjustMapToBounds in gmaps4rails.base.js:443
Gmaps4Rails.create_markers in gmaps4rails.base.js:321
Gmaps4Rails.addMarkers in gmaps4rails.base.js:389
Gmaps4Rails.replaceMarkers in gmaps4rails.base.js:381

目前已完成调查

  1. 通过将标记作为 json 字符串提供,确认地图的初始创建已完成.

  1. Confirmed that the initial creation of the map is done by providing the markers as a json string.

确认我在replaceMarkers调用中给出了相同格式的json字符串

Confirmed that I am giving a json string of the same format in the replaceMarkers call

确认在源代码中,在初始页面加载时调用 addMarkers 时,标记采用对象数组的形式,但 replaceMarkers 调用(如我上面提到的)包含一个 JSON 字符串

Confirmed that in the source, when addMarkers is called on initial page load, the markers are in the form of an object array, but the replaceMarkers call (as I mentioned above) contains a JSON string

其他尝试试图在不转换为 JSON 的情况下传递标记

Other attempts Tried to pass the markers without converting to JSON

Gmaps.map.replaceMarkers(markers_array);

但这并不奏效.

推荐答案

对于 JS 和 Rails 的新手,我为此纠结了很长一段时间......我无法从 RAILS JSON 转换将在我的控制器中创建的字符串转换为 JSON 对象的 JavaScript 数组.

I broke my brain over this for quite a while being new to both JS and Rails ... I was unable to get my head around converting from a RAILS jSON string created in my controller to a JavaScript array of JSON objects.

我尝试抓取 _to_gmaps4rails 生成的字符串,但它充满了转义字符.我现在知道这是由于 Rails 中的更改以防止脚本被数据插入.

I tried just grabbing the string generated by _to_gmaps4rails but it was full of escaped characters. I now know this was due to changes in Rails to prevent scripts from getting inserted by data.

我尝试了很多东西,比如在浏览器端解析 JSON,单独传递数据元素等.

I tried lots of things, like parsing the JSON on the browser side, passing the data elements individually, etc.

原来我需要的只是防止字符串被转义的 raw() 函数.这是我的工作代码:

Turns out all I needed was the raw() function which prevented the string from being escaped. Here's my working code:

在我的控制器中:

@markers = plots.to_gmaps4rails do |plot, marker|
    escaped_comment = ERB::Util.html_escape plot.comment
    marker.infowindow render_to_string(:partial => 'my_partial', :locals => { :plot => plot})
    marker.picture ( {
        "picture" => ActionController::Base.helpers.asset_path(plot.marker) ,          # string,  mandatory
        "width" =>   64,          # integer, mandatory
        "height" => 32,          # integer, mandatory
    })
    marker.title   plot.title
    marker.json({ :id => plot.id, :comment => escaped_comment})
end

在我的 JS 中(从 format.js Ajax 调用返回):

in my JS (returned from format.js Ajax call):

markers = <%=raw(@markers)%>
Gmaps.map.replaceMarkers(markers)

希望这对其他人有帮助!

Hope this helps someone else!

这篇关于gmaps4rails replaceMarkers 不起作用(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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