Skip to content
This repository has been archived by the owner on May 25, 2019. It is now read-only.

Support for tilesloaded? #16

Open
LIDJungle opened this issue Jul 22, 2013 · 1 comment
Open

Support for tilesloaded? #16

LIDJungle opened this issue Jul 22, 2013 · 1 comment

Comments

@LIDJungle
Copy link

I need to do geocoding on a map object, and as best I could see, there is no support for "addListenerOnce" and/or the tilesloaded event.

It was trivial to add it. And yes, I know this is NOT elegant. :)

  function bindMapEvents(scope, eventsStr, googleObject, element) {
  // Jason's hack. This makes the "ready" function fire on map load.
      google.maps.event.addListenerOnce(googleObject, 'tilesloaded', function() {
        scope.ready();
      });
    angular.forEach(eventsStr.split(' '), function (eventName) {
     ...
    }
  }

In the controller:

/* ready() is a custom definition that I added to the ui-map code.
   It sets up a one time listener that will run on 'tilesloaded' */
$scope.ready = function() {
    $scope.getCoords($scope.address1 + " " + $scope.city + " " + $scope.selectedState + " " + $scope.zip);
};

Not a show stopper, just an FYI.

@slai
Copy link

slai commented Sep 17, 2013

I needed something similar, but for the 'idle' event instead. Using your hints, I made something slightly less hacky :)

Essentially, instead of the 'map-' prefix, if the 'map-once-' prefix is used then it is triggered once only. It seems to work, and there aren't any clashes with the event names as far as I know (particularly as the event names use an underscore to separate words).

Let me know if you want me to create a PR for this.

  //Setup map events from a google map object to trigger on a given element too,
  //then we just use ui-event to catch events from an element
  function bindMapEvents(scope, eventsStr, googleObject, element) {
    angular.forEach(eventsStr.split(' '), function (eventName) {
      //Prefix all googlemap events with 'map-', so eg 'click' 
      //for the googlemap doesn't interfere with a normal 'click' event
      google.maps.event.addListener(googleObject, eventName, function (event) {
        element.triggerHandler('map-' + eventName, event);
        //We create an $apply if it isn't happening. we need better support for this
        //We don't want to use timeout because tons of these events fire at once,
        //and we only need one $apply
        if (!scope.$$phase) scope.$apply();
      });

      //Allow binding to same events once only using the 'map-once-' prefix instead
      google.maps.event.addListenerOnce(googleObject, eventName, function (event) {
          element.triggerHandler('map-once-' + eventName, event);
          //We create an $apply if it isn't happening. we need better support for this
          //We don't want to use timeout because tons of these events fire at once,
          //and we only need one $apply
          if (!scope.$$phase) scope.$apply();
      });
    });
  }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants