开发者

Populating Google Maps for Rails markers based on geolocation

I'm trying to use the Google Maps for Rails gem to populate a map with markers based on a custom gmaps4rails_callback that uses the current location, but I can't seem to get the code to replace the markers to trigger after the user's geolocation has been determined.

My application.js defines a custom maps callback function, along with a callback for 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);
}

For now, I'm using hard coded lat/long data, but eventually I'm going to key off of the real data in the position object passed to me. Running the getJSON call manually works fine, and my locations/nearby.json returns location data in the proper format that updates the markers as expected.

My view template just uses

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

Now I realize that I'm probably doing a geo lookup twice, by using both detect_location and then calling getCurrentPosition, but in a earlier attempt, code in gmaps4rails_callback was being executed before the geolocation permission from the browser (Safari) had even completed, so I thought this would delay execution until the location had been determined.

I've previously displayed a map without a custom gmaps4rails_callb开发者_JAVA技巧ack and it worked fine, so I know my jQuery and JavaScript files are being included just fine.

Is it possible that Google Maps for Rails somehow already sets up its own getCurrentPosition callback and mine is being ignored? After adding some debug logging, it doesn't look like nearby_locations is called at all.


Basically, when you set "detect_location" => true, this triggers the following function:

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); }
  }
}

So yes the geolocation is already made... or in progress. This function is triggered before the gmaps4rails_callback but it's the tough part with browser geolocation: it takes an undefined time.

I thought about proposing a callback on failure but didn't think it was necessary to include a callback on success (I'm still not convinced).

Unfortunately, I don't understand the problem with your gmpas4rails_callback: I've both detection_locationand callback and it works as expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜