我正在使用xamarin来构建一个android应用程序,它列出了很多商店,并告诉你它们离我们有多远。我已经让它在android模拟器中工作了。但当我在我的物理设备(三星galaxy s5/lineageos 15.1/android 8.1.0)上调试时,我似乎没有收到位置更新。其他利用位置数据的应用程序运行良好,比如谷歌地图。
应用程序请求许可,我已确保以下功能,
StartRequestingLocationData()
确实是在被召唤。
然而,
OnLocationChanged(Location location)
应用程序在物理设备上运行时从不被调用。在模拟器中,一切工作正常,它在启动时立即给出位置,并在新的位置数据进入时不断更新。
我很想提供一个错误信息或更彻底的方式,但我没有看到任何线索,可能是什么原因。例如,我在输出中没有看到错误或警告。
这是我的第一个android应用程序(尽管我在很多其他平台上都很有经验),所以我很可能遗漏了一些别人显而易见的东西。我的问题是,我找不到在哪里寻找线索,网上搜索还没有解决这个问题的覆盖面。
这是相关代码。提前谢谢。
public void StartRequestingLocationData()
{
this.location_manager = (LocationManager)GetSystemService(Context.LocationService);
Criteria crit = new Criteria();
crit.Accuracy = Accuracy.Fine;
crit.AltitudeRequired = false;
crit.BearingRequired = false;
crit.SpeedRequired = false;
// Both the emulator and physical device report this as "gps".
string provider = this.location_manager.GetBestProvider(crit, true);
this.location_manager.RequestLocationUpdates(provider, 1, 1, this);
Location last_location = this.location_manager.GetLastKnownLocation(provider);
if (last_location != null)
{
this.OnLocationChanged(last_location);
}
}
public void OnLocationChanged(Location location)
{
foreach (Replenisher store in this.stores)
{
if (store.DistanceView != null)
{
LatLng store_loc = new LatLng(double.Parse(store.GPSN), double.Parse(store.GPSW));
LatLng our_loc = new LatLng(location.Latitude, location.Longitude);
double distance = Meters.ComputeDistanceBetween(our_loc, store_loc);
store.DistanceView.Text = GetString(
Resource.String.kilometers_away,
distance / 1000
);
store.DistanceView.Visibility = ViewStates.Visible;
}
}
}