gpt4 book ai didi

ruby-on-rails - 使用 gmaps4rails 动态加载谷歌地图标记

转载 作者:数据小太阳 更新时间:2023-10-29 07:11:20 26 4
gpt4 key购买 nike

如何只加载 map 边界内的标记 gmaps4rails ?当然,在平移和/或缩放后加载新的。

与此直接相关的是,如何获取 map 的当前边界和缩放级别?

最佳答案

我是这样做的,我只在用户完成平移或缩放后替换标记,如果您需要不同的行为,请使用不同的事件监听器:

在你看来(index.html.erb):

<%= gmaps({ "map_options" => { "zoom" => 15, 
"auto_adjust" => false,
"detect_location" => true,
"center_on_user" => true }}, false, true) %>

在 View 的底部添加:

<script type="text/javascript" charset="utf-8">

function gmaps4rails_callback() {
google.maps.event.addListener(Gmaps4Rails.map, 'idle', function () {
var bounds = Gmaps4Rails.map.getBounds();
drawItems(bounds);
});
}

</script>

在 application.js 中(使用 jQuery):

function drawItems(theBounds) {
var url = '/venues.json/?sw_y=' + theBounds.getSouthWest().lng() +
'&sw_x=' + theBounds.getSouthWest().lat() +
'&ne_y=' + theBounds.getNorthEast().lng() +
'&ne_x=' + theBounds.getNorthEast().lat();
$.get(url, function(newItemData) {
Gmaps4Rails.replace_markers(newItemData);
});
}

field Controller #index:

def index
# Only pull venues within the visible bounds of the map
if (params[:sw_y] && params[:sw_x] && params[:ne_y] && params[:ne_x])
bounds = [ [params[:sw_x].to_f, params[:sw_y].to_f],
[params[:ne_x].to_f, params[:ne_y].to_f] ]
@venues_within_bounds = Venue.within_bounds(bounds)
else
@venues_within_bounds = Venue.all
end

respond_to do |format|
format.html # index.html.erb
format.json {
@data = @venues_within_bounds.collect {|v| {
:longitude => v.longitude,
:latitude => v.latitude,
:picture => v.marker_picture,
:title => v.marker_title
}
render :json => @data
}
end
end

Venue.rb 模型(使用 mongodb 和 mongoid):

def self.within_bounds(bounds)
self.where(:location.within => {"$box" => bounds })
end

关于ruby-on-rails - 使用 gmaps4rails 动态加载谷歌地图标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5046987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com