0
我尝试了一些操作:我有一个带标记的gMap。访问模板中的JavaScript变量的Rails - Google地图
在我的index
我初始化我的谷歌地图,把标记等一切工作。
在我的页面左侧,我显示了地图上5个最后标记的信息(名称,日期等)。
因此,我称之为模板_timeline
。
当我点击一个元素,我做了一个动作 - >移动地图。但就目前而言,每次我执行此操作时,地图都会重新加载(并且标记消失)。我只想看看地图移动到标记居中。
但是让我们看看我的代码,这可能是更简单:
我index.html.erb
<div class="row">
<% @statuses_t.each do |status| %>
<%= render partial: "statuses/timeline", locals: { status: status } %>
<% end %>
</div>
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({
provider: {
disableDefaultUI: true
// pass in other Google Maps API options here
},
internal: {
id: 'map'
}
},
function(){
markers = handler.addMarkers((<%=raw @hash.to_json %>));
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
}
);
</script>
handler
是GMAP
在statuses/timeline
... # some things
<script type="text/javascript">
$(<%="timeline_div#{status.id}"%>).on("click", function() {
var centerpoint = new google.maps.LatLng(<%=status.latitude%>, <%=status.longitude%>);
gMap = new google.maps.Map(document.getElementById('map'));
gMap.setZoom(13);
gMap.setCenter(centerpoint); // HERE
gMap.setMapTypeId(google.maps.MapTypeId.ROADMAP);
});
</script>
这个创建一个新的地图,当点击该项目时,问题是:我不想重新加载一个页面,我只是想改变用setCenter
重新映射地图。
所以我想在我的模板中使用Index中的变量handler
。
我该怎么做?
返回假(“点击”)处理程序?或停止事件传播? –
if'handler' is global:'handler.map.centerOn(<%= status.latitude%>,<%= status.longitude%>);' – apneadiving
是的,谢谢! 使用centerOn更好。 但是centerOn直接将地图放在位置上,是否有可能发生移动?就像在googlmap网站上一样? – F4Ke