0

我正在尝试使用Google Maps for Rails gem 使用基于使用当前位置的自定义 gmaps4rails_callback 的标记填充地图,但我似乎无法获取代码来替换标记以在用户的​​地理位置之后触发已确定。

application.js定义了一个自定义地图回调函数,以及一个回调getCurrentPosition

function nearby_locations(position) {
    $.getJSON('locations/nearby.json',
          { lat: "33.7012", long: "-117.8683" },
          function(data) {
                    Gmaps4Rails.replace_markers(data);
          });   
}

function gmaps4rails_callback() {
    navigator.geolocation.getCurrentPosition(nearby_locations);
}

目前,我使用的是硬编码的纬度/经度数据,但最终我将关闭position传递给我的对象中的真实数据。手动运行 getJSON 调用可以正常工作,并且我locations/nearby.json以正确的格式返回位置数据,可以按预期更新标记。

我的视图模板只是使用

<%= gmaps("map_options" => {"auto_adjust" => true, "detect_location" => true}) %>

现在我意识到我可能通过同时使用 detect_location 然后调用来进行两次地理查找getCurrentPosition,但是在较早的尝试中,在gmaps4rails_callback浏览器(Safari)的地理定位权限甚至完成之前执行了代码,所以我想这将推迟执行,直到确定地点为止。

我之前展示了一张没有自定义的地图gmaps4rails_callback,它工作得很好,所以我知道我的 jQuery 和 JavaScript 文件被包含在内了。

是否有可能 Google Maps for Rails 已经以某种方式设置了自己的getCurrentPosition回调而我的被忽略了?添加一些调试日志后,它看起来根本不像nearby_locations被调用。

4

1 回答 1

1

基本上,当您设置 时"detect_location" => true,这会触发以下功能:

findUserLocation: function() {
    if(navigator.geolocation) {
    //try to retrieve user's position
    navigator.geolocation.getCurrentPosition(function(position) {
      //saves the position in the userLocation variable
      Gmaps4Rails.userLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
           //change map's center to focus on user's geoloc if asked
            if(Gmaps4Rails.map_options.center_on_user === true) {
                Gmaps4Rails.map.setCenter(Gmaps4Rails.userLocation);
            }
    },
    function() {
          //if failure, triggers the function if defined
          if(this.fnSet("gmaps4rails_geolocation_failure")) { gmaps4rails_geolocation_failure(true); }
        });
  }
  else {
      //if failure, triggers the function if defined
      if(this.fnSet("gmaps4rails_geolocation_failure")) { gmaps4rails_geolocation_failure(false); }
  }
}

所以是的,地理定位已经完成......或正在进行中。此功能在 之前触发,gmaps4rails_callback但它是浏览器地理定位的难点:它需要一个未定义的时间。

我考虑过在失败时提出回调,但认为没有必要在成功时包含回调(我仍然不相信)。

不幸的是,我不明白你的问题gmpas4rails_callback:我有detection_location和回调,它按预期工作。

于 2011-05-24T19:19:25.877 回答