如何在gmaps4rails v2中添加折线

时间:2013-10-24 03:04:02

标签: javascript ruby-on-rails gmaps4rails gmaps4rails2

我已升级到最新版本的gmaps4rails(v2),并且在绘制折线时遇到了一些问题。

我可以使用维基上的示例代码处理标记,但是如果我将addMarkers更改为addPolylines,则地图中不会呈现任何内容。这是处理程序代码:

handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
polylines = handler.addPolylines([{"lat":45.678295,"lng":-121.603813,"ele":438.626221,"time":1381851321},{"lat":45.678196,"lng":-121.603607,"ele":423.202148,"time":1381851363},{"lat":45.678181,"lng":-121.603607,"ele":421.108398,"time":1381851373},{"lat":45.678162,"lng":-121.603584,"ele":420.153442,"time":1381851383},{"lat":45.678123,"lng":-121.603569,"ele":422.561096,"time":1381851393},{"lat":45.678165,"lng":-121.6036,"ele":425.191284,"time":1381851403},{"lat":45.678169,"lng":-121.603607,"ele":425.262329,"time":1381851413}]);
handler.bounds.extendWith(polylines);
handler.fitMapToBounds();
});

javascript控制台中没有错误,只是没有地图渲染。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

有几件事:

  • 首先是我的2.0.0中的一个错误,我升级了2.0.3

  • 然后您的解释中出现一个问题:折线是一个lat / lng数组,因此您的代码无法正常工作。

  • 谷歌没有提供get the bounds of a polyline的任何方式,因此很难自动调整地图。

那说,你可以这样做:

polyline = [{"lat":45.678295,"lng":-121.603813,"ele":438.626221,"time":1381851321},{"lat":45.678196,"lng":-121.603607,"ele":423.202148,"time":1381851363},{"lat":45.678181,"lng":-121.603607,"ele":421.108398,"time":1381851373},{"lat":45.678162,"lng":-121.603584,"ele":420.153442,"time":1381851383},{"lat":45.678123,"lng":-121.603569,"ele":422.561096,"time":1381851393},{"lat":45.678165,"lng":-121.6036,"ele":425.191284,"time":1381851403},{"lat":45.678169,"lng":-121.603607,"ele":425.262329,"time":1381851413}]

handler.addPolyline(polyline); // and not addPolylines
handler.bounds.extend(polyline[0]);
handler.bounds.extend(polyline[ polyline.length - 1]);
handler.fitMapToBounds();

您最终会得到:enter image description here