- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我尝试从我的 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/
从谷歌商店下载并使用“Fake Gps”后,我想创建一个用于教育目的。 我正在使用 GoogleApiClient FusedLocationAPI 获取我的当前位置并创建模拟位置。我能够获取我当前的
我正在尝试在后台服务中使用 FusedLocation API。我在 :- 上遇到错误 LocationServices.FusedLocationApi.requestLocationUpdates
我制作了一个谷歌地图应用程序,但当我接受权限时它会关闭。除了 onConnected 方法之外,所有代码都可以。 这里是: @Override public void onConnected(
我正在编写一个简单的 Android 应用程序来捕获用户的位置。 我正在使用 FusedLocationAPI 来提供位置。 mLocationRequest = LocationRequest.
这个问题是关于我正在开发的 Android 应用程序。我正在使用 Android 版本 7 的真实手机上进行测试。 我希望 FusedLocationApi 每 15 分钟请求一次 GPS 位置。 当
你好,我正在尝试停止位置更新,我使用了这段代码:- LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
实际上,我正在使用 FusedLocationApi 来获取用户当前位置,然后我将在 map 上添加标记并且它工作正常..现在我在 MapFragment 上添加一个 EditText,editTex
我想在 GPS/Wifi 关闭时使用 google play services LocationServices.FusedLocationApi.getLastLocation(mGoogleApi
我有一个获取位置的 Android 应用: private LocationRequest createLocationRequest() { LocationRequest mLocatio
我想在我的 Android 项目中简单地检索设备位置,为此我使用播放服务方法: protected synchronized void buildGoogleApiClient() {
我正在编写一个 Android 应用程序,并且有一个与 FusedLocationApi.requestLocationUpdates() 相关的奇怪行为,我对此进行了搜索,并且其他相关问题都没有解决
我在 android 中使用谷歌的 fusedlocationapi 来获取用户当前的纬度和经度,现在根据我的用例,我只想在用户单击按钮时获取用户的当前位置,现在我认为而不是请求位置更新我可以在一段时
常规的 Android(非融合)LocationListener https://developer.android.com/reference/android/location/LocationLi
我需要将异步 FusedLocationApi.getLastLocation 调用转换为同步调用。我不是 Java 的大专家,我不确定我做对了。 我想连接到 GoogleApiClient,然后阻止
我没有使用 LocationManager,而是使用 LocationServices 方法来获取当前的 gps 位置和位置更新。 这是代码: public void onConnected(@Nul
我有定位服务的单例 public class LocationService implements GoogleApiClient.ConnectionCallbacks, Googl
我正在尝试使用 GoogleAPIClient 获取实时位置更新。虽然 GoogleAPIClient 已连接但位置对象为空,并且它提示 location.getProvider() 为空。有人可以帮
SO 上还有一些关于同一问题的其他问题,但我想答案有点陈旧,可能不适用于我的情况。在下面的代码中,永远不会调用方法 onLocationChanged。我还尝试从 android 模拟器发送新位置,但
我正在尝试使用 FusedLocationApi.getLastLocation 获取位置,并且我在 list 文件中获得了位置权限: 但是,当我从系统请求位置时,我得到 null。我只是在测试关
我不明白为什么 LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,
我是一名优秀的程序员,十分优秀!