通过 ajax 的 Google-Maps-for-Rails 中心地图
我正在通过 ajax 设置一个新标记,我想知道如何使用自定义缩放级别将地图居中在新点上,我以这种方式制作:
def geocode
pos = Gmaps4rails.geocode(params[:address])
render :update do |page|
unless pos.blank?
page << "$('#poi_latitude').val(#{pos.first[:lat]});"
page << "$('#poi_longitude').val(#{pos.first[:lng]});"
page << "if (marker != null) { marker.setMap(null); }"
page << "var myLatlng = new google.maps.LatLng(#{pos.first[:lat]},#{pos.first[:lng]});"
page << "marker = new google.maps.Marker({position: myLatlng, map: Gmaps4Rails.map});"
page << "Gmaps4Rails.map.centerAndZoom(myLatlng, 0);"
end
end
end
但我收到错误,因为方法 centerAndZoom 未定义到 gmaps4rails 中。 js,我应该将其定义到js中还是有其他方法? 谢谢
I'm setting a new marker via ajax and I was wondering how to center the map on the new point with a custom zoom level, I made in this way:
def geocode
pos = Gmaps4rails.geocode(params[:address])
render :update do |page|
unless pos.blank?
page << "$('#poi_latitude').val(#{pos.first[:lat]});"
page << "$('#poi_longitude').val(#{pos.first[:lng]});"
page << "if (marker != null) { marker.setMap(null); }"
page << "var myLatlng = new google.maps.LatLng(#{pos.first[:lat]},#{pos.first[:lng]});"
page << "marker = new google.maps.Marker({position: myLatlng, map: Gmaps4Rails.map});"
page << "Gmaps4Rails.map.centerAndZoom(myLatlng, 0);"
end
end
end
but I get an error because the method centerAndZoom is not defined into gmaps4rails.js, should i define it into the js or there is another way?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Gmaps4rails
是一个包装器,因此 google 对象保留其所有属性。您的行
Gmaps4Rails.map.centerAndZoom(myLatlng, 0);
不起作用,因为centerAndZoom
不是有效的方法(您在哪里找到它?参考在这里:http://code.google.com/apis/maps/documentation/ javascript/reference.html )否则,您的问题已在此处得到解答:https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Dynamic-%28or-Ajax%29-map-refresh
您可以使用
replace_markers
或add_markers
。您还可以调整地图的设置,使其按照您想要的方式居中: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Map
特别参见
auto_adjust
。Gmaps4rails
is a wrapper, so google objects keep all their properties.Your line
Gmaps4Rails.map.centerAndZoom(myLatlng, 0);
doesn't work sincecenterAndZoom
isn't a valid method (where did you find it? the reference is here: http://code.google.com/apis/maps/documentation/javascript/reference.html )Otherwise, your question is already answered here: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Dynamic-%28or-Ajax%29-map-refresh
You can use either
replace_markers
oradd_markers
.And you can adapt the settings of your map to center it the way you want: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Map
See
auto_adjust
in particular.