gpt4 book ai didi

c# - 将 FusedLocationApi 与 Xamarin 3 结合使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:16 24 4
gpt4 key购买 nike

当我尝试从我的 Xamarin Activity 中使用 FusedLocationApi 时遇到了很多问题。此处列出的代码使用的方法 Location Xamarin已被标记为过时,因此无法编译。我的实现如下。我的问题是,如果这是这样做的方式,或者我是否忽略了一些更容易的事情? LocationHandler 由我的 Activity 使用,例如OnCreate、OnResume、OnPause 调用连接和断开连接方法。 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);
}
}


}

}

最佳答案

我猜不是。我将关闭问题,但不会删除问题,因为人们随后可以找到有效的 LocationServices 示例。

关于c# - 将 FusedLocationApi 与 Xamarin 3 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24872452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com