谷歌地图的标记变量范围(gmaps4rails gem) [英] scope of markers variable for google maps (gmaps4rails gem)
问题描述
我试图理解javascript,以及变量传递的方式,但我做得不太好。我试图用ajax刷新谷歌地图的标记,但没有发生任何事情。
我提交一个表单,它向控制器发送一些变量。处理了一些内容,并以.js响应... ...
find.js.erb
$('#collapseTwo ul')。html(<%= j渲染部分:'events / sidebar',集合:@events%> );
alert(<%= raw @ hash.to_json%>);
clearMarkers();
markers = handler.addMarkers(<%= raw @ hash.to_json%>,{
draggable:false
});
渲染效果很好,所以一切都在那里工作,但我似乎无法获得保留刷新地图的标记。该警报显示 [object Object]
,所以这可能是正确的,但不会对标记执行任何操作。
以
的形式对 marker.coffee
进行了一些修改: clear: - >
@getServiceObject()。setMap(null)
显示: - >
@getServiceObject()。setVisible(true)
hide: - >
@getServiceObject()。setVisible(false)
使 clearMarkers();
起作用。它在另一个文件中,我在其中存储一些其他操作
events.js.coffee
jQuery - >
...
...
@clearMarkers = - >
for Gmaps.store.markers中的marker
marker.clear()
Gmaps.store.markers = []
所以...我最初打电话给地图......
jQuery - >
handler = Gmaps.build'Google'
handler.buildMap {
provider:{
minZoom:3
},internal:{id:'map'}} , - >
markers = handler.addMarkers($('#map')。data('events'),
draggable:false
flat:false
)
#moves map to marker clicked + open infowindow
$(document).on'click','#sideBar li', - >
marker [$(this).data('marker')]。panTo()
markers [$(this).data('marker')]。click()
该函数在底部有唯一的方法可以访问标记
数组是因为它位于 handler.buildMap
函数中。那么,如果标记
变量是全局变量?
另外,处理程序
变量...在我看来,这似乎是我喜欢的这应该是无处不在的。我尝试手动将 @hash
放入 .addMarkers()
中,但处理程序未构建标记。 / p>
您需要一个全球可访问的商店。
:
Gmaps.store = {}
jQuery - >
Gmaps.store.handler = Gmaps.build'Google'
Gmaps.store.handler.buildMap ...
Gmaps.store.markers = Gmaps.store.handler.addMarkers(.. 。)
您也可以访问js.erb中的变量。
I'm trying to understand javascript, and the way variables are passed around, but I'm not doing too well. I'm trying to refresh a google map's markers with ajax, but nothing is happening.
I submit a form, which sends some variables to the controller. Some stuff is processed, and it responds with .js to...
find.js.erb
$('#collapseTwo ul').html("<%= j render partial: 'events/sidebar', collection: @events %>");
alert(<%= raw @hash.to_json %>);
clearMarkers();
markers = handler.addMarkers(<%= raw @hash.to_json %>, {
draggable: false
});
The render fires fine, so everything is working there, but I can't seem to get a hold of the markers to refresh the map. That alert shows [object Object]
, so that's likely correct, but no action on the markers.
I made some modifications to marker.coffee
in the form of
clear: ->
@getServiceObject().setMap(null)
show: ->
@getServiceObject().setVisible(true)
hide: ->
@getServiceObject().setVisible(false)
so that clearMarkers();
works. It's in another file, where I'm storing some other actions
events.js.coffee
jQuery ->
...
...
@clearMarkers = ->
for marker in Gmaps.store.markers
marker.clear()
Gmaps.store.markers = []
So... I call the map initially in with...
jQuery ->
handler = Gmaps.build 'Google'
handler.buildMap {
provider: {
minZoom: 3
}, internal: {id: 'map'} }, ->
markers = handler.addMarkers( $('#map').data('events'),
draggable: false
flat: false
)
#moves map to marker clicked + open infowindow
$(document).on 'click', '#sideBar li', ->
markers[$(this).data('marker')].panTo()
markers[$(this).data('marker')].click()
That function on the bottom there... the only way I had access to the markers
array was because it was inside the handler.buildMap
function. So, should the markers
variable be global? How else can I act on them?
Also, the handler
variable... that's it seems to me like that should be available everywhere. I tried manually putting the @hash
into .addMarkers()
, but the handler didn't build the markers.
You need a store accessible globally.
I suggest the following:
Gmaps.store = {}
jQuery ->
Gmaps.store.handler = Gmaps.build 'Google'
Gmaps.store.handler.buildMap...
Gmaps.store.markers = Gmaps.store.handler.addMarkers(...)
You can access the variables within your js.erb as well.
这篇关于谷歌地图的标记变量范围(gmaps4rails gem)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!