Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49526

Need help with $resource and (basic) Geocoding

$
0
0

@claudiocarmeli wrote:

Hello,

In my app, I have to figure out where the user is, so that I can send lat and lng as query parameters with my $resource request.
This is my code:

angular.module('starter.services', ['ngResource'])
.factory('Pubs', function($resource) {
  // Get user position
  navigator.geolocation.getCurrentPosition(getPosition);
  function getPosition(position) {

    // Get pubs
    var httpRequest = $resource('http://my-domain.com/pubs', {}, {
      query: {
        method: 'GET',
        isArray: true,
        params: {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        }
      }
    });

    pubs = httpRequest.query(function(response) {
      console.log(response);
    });
  }

  return {
    all: function() {
      return pubs;
    }
  };
});

It doesn't work because by the time that HTML5 Geocoding has got back to me with the coordinates, the function has already returned. Moving all the code inside the callback does not work because I get an error saying 'your services must return something'. I know it's quite simple but I am stuck...
Any help would be greatly apreciated

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 49526

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>