Using mdg:geolocalisation inside a service/Factory in AngularJS


#1

Hello !
I am actually in full dev of an App wich have a map integration.
Actually, I am trying to get the position of the user, and dynamically refresh the position of the user while he walks.

I am using mdg:geolocalisation to get the position of the user… But I HAVE To get the position inside a service, and the only way to get the position through mdg:geolocation is inside a helpers…

Does anyone have any idea ?


#2

What do you mean with ‘inside a service’? Just use the Javascript geolocation API on the client and call a method (to save the location to the db)?


#3

I mean, inside a service (a factory, if you prefer)
Yes, sure, but I can’t call a method outside the helpers.

Is there a way to call a return helpers inside a service/factory ?


#4

Hi @rollins i have a lot of experience using Geo stuff on Javascript, i dont understand what is a Service/Factory. Its an API ? a Method ?. Im confused.

But… getting away from that you can do this to set the Location:

  someFn() {
    const geolocation = (
      canUseDOM && navigator.geolocation || {
        getCurrentPosition: (success, failure) => {
          alert(`Your browser dont support this feature.`);
        },
      }
    );
    geolocation.getCurrentPosition((position) => {
      console.log(position.coords.latitude)
      console.log(position.coords.longitude)
    }, (reason) => {
      //if there is an error set some default lat,lng
      // or manage errors...
      alert(`Error: (${ reason }).`);
    });
  }

#5

That could work in JS… But I’m currently working on AngularJS. Arg. I forgot to tell that. I’m ashamed. :frowning:

Anyway.

A service/Factory is a simplification of comunication between controller an DOM. But if you don’t use AngularJS, I doubt that you can help me through this ! :frowning: