move common function from directive to controller\nuse AngularJS forEach when looking for coordinates (instead of for loop)