Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Gmaps4rails:如何在Rails 4中默认设置地图上的当前用户位置_Ruby On Rails_Ruby On Rails 4_Gmaps4rails - Fatal编程技术网

Ruby on rails Gmaps4rails:如何在Rails 4中默认设置地图上的当前用户位置

Ruby on rails Gmaps4rails:如何在Rails 4中默认设置地图上的当前用户位置,ruby-on-rails,ruby-on-rails-4,gmaps4rails,Ruby On Rails,Ruby On Rails 4,Gmaps4rails,我的应用程序中有一个属性搜索栏,用户在上面搜索城市中的属性,并使用Gmaps4railsgem在地图上填充属性的位置。搜索栏中的城市名称由typeahead.js自动填充 一切都很好。我在地图上找到了那个地方的标记。但我的问题是,地图只在上显示灰色图像或蓝色图像,而不是输入任何城市名称。下面是两种情况下的地图图像 案例1:当给出城市名称时 案例2:未给出城市名称时 在案例2中,如何修改代码以在地图上显示当前用户位置,而不干扰案例1 下面是我的代码 看法 你可以这样做: @hash = Gma

我的应用程序中有一个属性搜索栏,用户在上面搜索城市中的属性,并使用Gmaps4railsgem在地图上填充属性的位置。搜索栏中的城市名称由typeahead.js自动填充

一切都很好。我在地图上找到了那个地方的标记。但我的问题是,地图只在上显示灰色图像蓝色图像,而不是输入任何城市名称。下面是两种情况下的地图图像

案例1:当给出城市名称时

案例2:未给出城市名称时

案例2中,如何修改代码以在地图上显示当前用户位置,而不干扰案例1

下面是我的代码

看法
你可以这样做:

@hash = Gmaps4rails.build_markers(@properties) do |property, marker|
    marker.lat property.latitude
    marker.lng property.longitude
    marker.picture({
        "url"    => view_context.image_path("Orange-House-Icon.png"),
        "width"  => 50,
        "height" => 50
         })
    marker.infowindow  render_to_string( partial: 'layouts/mapinfo', locals: { property: property.id })
    marker.title "i'm the title"
end
if @hash.empty?
  @hash.push({
    lat: current_user.latitude,
    lng: current_user.longitude
  })
end


顺便说一句,将
@hash
重命名为
@array

会很好,因此添加当前用户位置会完全破坏地图,而您只会得到一个带有控件的空白画布?听众都被标记上了吗?@ValAsensio不!我还没有加进去。当搜索栏没有输入时,我得到空白画布。我的问题是如何修改代码以在地图上显示当前用户位置,而不是在空白画布上。好的。我明白了,所以如果用户没有输入搜索城市,您希望使用其当前位置作为搜索城市。这是有道理的。这只是一个想法。我知道应用程序需要申请使用自动定位服务的权限。通常会出现一个弹出窗口,显示“应用程序正在请求您的位置…”如果您拒绝,则他们的地图会转到某个默认位置。您可以更改缩放吗?
def search
@properties = Property.search_available_coming(params[:available_coming]).where(property_active: true).order(property_city: :asc).paginate(:page => params[:page], :per_page => 10)
@hash = Gmaps4rails.build_markers(@properties) do |property, marker|

    marker.lat property.latitude
    marker.lng property.longitude
    marker.picture({
        "url"    => view_context.image_path("Orange-House-Icon.png"),
        "width"  => 50,
        "height" => 50
         })
    marker.infowindow  render_to_string( partial: 'layouts/mapinfo', locals: { property: property.id })
    marker.title "i'm the title"
end
end
@hash = Gmaps4rails.build_markers(@properties) do |property, marker|
    marker.lat property.latitude
    marker.lng property.longitude
    marker.picture({
        "url"    => view_context.image_path("Orange-House-Icon.png"),
        "width"  => 50,
        "height" => 50
         })
    marker.infowindow  render_to_string( partial: 'layouts/mapinfo', locals: { property: property.id })
    marker.title "i'm the title"
end
if @hash.empty?
  @hash.push({
    lat: current_user.latitude,
    lng: current_user.longitude
  })
end