[英]Using gps location system services dependency injection
我希望使用Xamarin的依賴項注入獲取特定於平台的位置詳細信息,但遇到了問題。 錯誤做的可能性更大。
這是我當前的設置:
nuMaps /接口/ ILocationService.cs
using Xamarin.Forms.Maps;
namespace nuMaps
{
public interface ILocationService
{
void initLocationService();
Position getCurrentPosition();
}
}
nuMaps / nuMaps.Droid /接口/ 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
}
}
在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 /瀏覽/ 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: This *shouldn't* need to happen, but we don't get location information until
* the OnConnected callback happens which is too slow to put in getCurrentLocation method.
*/
DependencyService.Get<ILocationService>().initLocationService();
}
}
}
我相信問題出在您的ILocationService
實現中。
我將刪除實現活動(為什么要使用OnCreate?),並查看http://developer.xamarin.com/guides/android/platform_features/maps_and_location/location/ 。
我建議你在Android上獲取GPS位置通過谷歌API的發揮,這將需要實施ILocationListener
, IGoogleApiClientConnectionCallbacks
和IGoogleApiClientOnConnectionFailedListener
。 希望有幫助!
編輯評論:
如果問題LocationService是最新的,我沒有看到你正在實施IGoogleApiClientConnectionCallbacks
或ILocationListener
。 可能是因為地圖頁使用的是gps, GetLastKnownLocation
起作用了,因為最近已經獲得了位置。
GPS位置請求是一個異步操作IGoogleApiClientConnectionCallbacks
的方法IGoogleApiClientConnectionCallbacks
是OnConnected,您應在其中調用類似以下內容的方法:
LocationServices.FusedLocationApi.RequestLocationUpdates(googleApiClient, locationRequest, this);
這將主動請求位置更新,然后在返回位置更新時觸發OnLocationChanged(Location location)
。 這些都是異步的,因此您可能需要在LocationService中公開這些事件並進行訂閱。
您可以嘗試TinyIoC,而不是Xamarin Dependency Service。 它適用於特定於平台的實現,例如定位服務。
也許Xamarin Dependency Service可以用於這些類型的事情,但是到目前為止,我僅將其用於自定義渲染器。 對於更多基於服務的內容,我使用TinyIoC。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.