当我尝试使用Xamarin活动中的 FusedLocationApi 时,我遇到了很多问题。此处列出的代码使用的方法Location Xamarin已标记为已过时,因此无法编译。我的实现如下。我的问题是,如果这是方法,或者我更容易忽视一些事情?我的活动使用LocationHandler,例如OnCreate,OnResume,OnPause调用connect和disconnect方法。 OnChangedLocation方法当然应该做一些更聪明的事情。
using System;
using Android.Gms.Common;
using Android.Gms.Common.Apis;
using Android.Gms.Location;
using Android.Locations;
using Android.Util;
using Android.OS;
using Android.Content;
namespace WithKidsAndroid
{
public class LocationHandler : Java.Lang.Object, IGoogleApiClientConnectionCallbacks, IGoogleApiClientOnConnectionFailedListener, Android.Gms.Location.ILocationListener
{
private IGoogleApiClient _googleAPI;
private Context _context;
public LocationHandler(Context context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
else
{
_context = context;
}
initializeGoogleAPI();
LocRequest = new LocationRequest();
}
public LocationHandler(Context context, LocationRequest request)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
else
{
_context = context;
}
initializeGoogleAPI();
LocRequest = request;
}
public LocationRequest LocRequest
{
get;
set;
}
public void connectGoogleAPI()
{
System.Diagnostics.Debug.Assert(_googleAPI != null);
if (!_googleAPI.IsConnectionCallbacksRegistered(this))
{
_googleAPI.RegisterConnectionCallbacks(this);
}
if (!_googleAPI.IsConnectionFailedListenerRegistered(this))
{
_googleAPI.RegisterConnectionFailedListener(this);
}
if (!_googleAPI.IsConnected || !_googleAPI.IsConnecting)
{
_googleAPI.Connect();
}
}
public void disconnectGoogleAPI()
{
if (_googleAPI != null && _googleAPI.IsConnected)
{
if (_googleAPI.IsConnectionCallbacksRegistered(this))
{
_googleAPI.UnregisterConnectionCallbacks(this);
}
if (_googleAPI.IsConnectionFailedListenerRegistered(this))
{
_googleAPI.UnregisterConnectionFailedListener(this);
}
_googleAPI.Disconnect();
}
}
public void OnConnected(Bundle connectionHint)
{
Log.Debug("LocationHandler", "logged connected", connectionHint);
if (LocRequest == null)
{
throw new Exception("Unknown location request. Set this first by using property LocRequest or constructor.");
}
LocationServices.FusedLocationApi.RequestLocationUpdates(_googleAPI, LocRequest, this);
}
public void OnConnectionSuspended(int cause)
{
Log.Debug("LocationHandler", "logged OnConnectionSuspended", cause);
}
public void OnConnectionFailed(ConnectionResult result)
{
Log.Debug("LocationHandler", "logged OnConnectionFailed", result);
}
public void OnLocationChanged(Location location)
{
Log.Debug("LocationHandler", "logged location changed: " + location.ToString());
}
private void initializeGoogleAPI()
{
int queryResult = GooglePlayServicesUtil.IsGooglePlayServicesAvailable(_context);
if (queryResult == ConnectionResult.Success)
{
_googleAPI = new GoogleApiClientBuilder(_context).AddApi(LocationServices.Api).AddConnectionCallbacks(this).AddOnConnectionFailedListener(this).Build();
}
else
{
var errorString = String.Format("There is a problem with Google Play Services on this device: {0} - {1}", queryResult, GooglePlayServicesUtil.GetErrorString(queryResult));
Log.Error("WithKidsAndroid.LocationHandler", errorString);
throw new Exception(errorString);
}
}
}
}
答案 0 :(得分:2)
我猜不是。我将关闭这个问题,但不会删除问题,因为人们可以找到一个有效的LocationServices示例。