我正在尝试使用gmaps4rails(即使用AJAX回调)动态更新Google地图上的标记。我发现gmaps4rails的文档非常分散且不清晰。gmaps4rails gem:to_gmaps4rails方法undefined
我已经成功地使用build_markers方法(按照视频教程v2)在地图上显示标记。
我的控制器代码:
# GET /telemetry_recordings
def index
@telemetry_recordings = TelemetryRecording.all
@hash = Gmaps4rails.build_markers(@telemetry_recordings) do |telemetry_recording, marker|
marker.lat telemetry_recording.latitude
marker.lng telemetry_recording.longitude
marker.title telemetry_recording.delivery_unit.driver.full_name
marker.infowindow telemetry_recording.delivery_unit.driver.full_name
end
end
我的视图代码:
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
现在,实现动态更新,我已经添加了下面的脚本我的看法:
<script type="text/javascript">
jQuery(function($) {
$('body').on('click', '#replace_markers', function(){
$.getJSON("/telemetry_recordings", function(data){
Gmaps.map.replaceMarkers(data);
});
});
});
</script>
除了按钮:
<button id="replace_markers">Refresh</button>
而且,我下面的代码添加到我的控制器:
respond_to :json, :html
# GET /telemetry_recordings
def index
@json = TelemetryRecording.all.to_gmaps4rails
respond_with @json
end
注:TelemetryRecording类有3个属性:纬度(浮动),经度(浮动)和location_time(DATETIME)
这将导致以下错误:
undefined method `to_gmaps4rails' for #<TelemetryRecording::ActiveRecord_Relation:0x55dee78>
按照文件,我已经安装了gmaps4rails宝石,添加// =需要强调和 // =需要gmaps /谷歌我的application.js文件,包括在我看来,以下脚本:
<script src="//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js" type="text/javascript"></script>
<script src="//maps.google.com/maps/api/js?v=3.13&sensor=false&libraries=geometry" type="text/javascript"></script>
我使用to_gmaps4rails正确吗? (我的理解是它将具有纬度/经度属性的对象转换为标记数组,例如[{“lat”:“x”,“lng”:“y”},{“lat”:“x”,“ LNG“:”?Y“}]为什么未定义
啊,让这么多的感觉....对不起我真的很困惑与版本。现在工作很好。非常感谢。 (包括伟大的宝石)。还有一个问题,当模型中的底层位置数据发生变化时,gem是否包含动态更新地图的选项?如果是这样,有任何机会可以有资源/示例/解释器可以帮助我们在这里?谢谢! – user3711600 2014-09-11 09:42:54
它只是一个使用人体方法显示标记的包装。没有额外的逻辑作为自动更新等 – apneadiving 2014-09-11 09:50:31
抱歉,我不明白这个词 – user3711600 2014-09-11 14:56:10