c# - Using gps location system services dependency injection -


i looking platform specific location details using xamarin's dependency injection running issues. more doing wrong.

here current setup:

numaps/interfaces/ilocationservice.cs

using xamarin.forms.maps;  namespace numaps {     public interface ilocationservice     {         void initlocationservice();         position getcurrentposition();     } } 

numaps/numaps.droid/interfaces/locationservice.cs

using system; using numaps; using numaps.droid; using xamarin.forms.maps; using android.app; using android.gms.common; using android.gms.common.apis; using android.gms.location; using android.locations; using android.widget;  [assembly: xamarin.forms.dependency (typeof (locationservice))]  namespace numaps.droid {     public class locationservice : java.lang.object, ilocationservice, igoogleapiclientconnectioncallbacks, igoogleapiclientonconnectionfailedlistener, android.gms.location.ilocationlistener     {         readonly igoogleapiclient _googleapiclient;         readonly locationrequest _locrequest;         position _currentposition;         location _currentlocation;          public locationservice()         {             console.writeline ("locationservice constructor");             _currentposition = new position (0, 0);              _googleapiclient = new googleapiclientbuilder (xamarin.forms.forms.context)                 .addapi (locationservices.api)                 .addconnectioncallbacks (this)                 .addonconnectionfailedlistener (this)                 .build ();              _locrequest = new locationrequest ();         }          #region ilocationservice implementation          public void initlocationservice()         {             _googleapiclient.connect ();         }          public position getcurrentposition ()         {             if (_googleapiclient.isconnected) {                 _currentlocation = locationservices.fusedlocationapi.getlastlocation (_googleapiclient);                 _currentposition = new position (_currentlocation.latitude, _currentlocation.longitude);             }              _googleapiclient.disconnect ();              return new position (_currentlocation.latitude, _currentlocation.longitude);         }          #endregion         public void onlocationchanged(location l)         {             console.writeline ("onlocationchanged");             _currentlocation = l;         }          public void onconnectionfailed (connectionresult result)         {             console.writeline ("connectionfailed");         }          #region igoogleapiclientconnectioncallbacks implementation         public void onconnected (android.os.bundle connectionhint)         {             console.writeline ("onconnected");              if (!_googleapiclient.isconnected)                 locationservices.fusedlocationapi.requestlocationupdates (_googleapiclient, _locrequest, this);              _currentlocation = locationservices.fusedlocationapi.getlastlocation (_googleapiclient);         }         public void onconnectionsuspended (int cause)         {             console.writeline ("onconnectionsuspended");         }         #endregion     } } 

usage in numaps/views/mappage.xaml.cs

using xamarin.forms; using xamarin.forms.maps; using system; using system.diagnostics;  namespace numaps {     public partial class mappage : contentpage     {          public mappage ()         {             initializecomponent ();             position p = dependencyservice.get<ilocationservice>().getcurrentposition();             mymap.movetoregion (                 mapspan.fromcenterandradius (                     p, distance.frommiles (1)                 )             );         }     } } 

numaps/views/loginpage.xaml.cs

using system; using xamarin.forms; using numaps;  namespace numaps {     public partial class loginpage : contentpage     {         public loginpage ()         {             initializecomponent ();              /*              * init platform specific location service.              * todo: *shouldn't* need happen, don't location information until              * onconnected callback happens slow put in getcurrentlocation method.              */               dependencyservice.get<ilocationservice>().initlocationservice();         }     } } 

i believe problem in implementation of ilocationservice.

i remove implementing activity (why want use oncreate?) , take @ http://developer.xamarin.com/guides/android/platform_features/maps_and_location/location/.

i'd recommend on android getting gps location via google play apis, require implementing ilocationlistener, igoogleapiclientconnectioncallbacks, , igoogleapiclientonconnectionfailedlistener. hope helps!

edit comments:

if locationservice in question date, don't see you're implementing igoogleapiclientconnectioncallbacks or ilocationlistener. may because mappage using gps, getlastknownlocation works, because location has been obtained.

gps location requesting async operation - 1 of methods igoogleapiclientconnectioncallbacks onconnected, should call like:

locationservices.fusedlocationapi.requestlocationupdates(googleapiclient, locationrequest, this); 

this actively request location updates, , fire onlocationchanged(location location) when location update returned. async, you'll need expose these events in locationservice , subscribe them.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -