所以我正在使用一个应用程序,它使我获得当前的纬度,经度,当前速度等,使用GPS数据,该应用程序不会给我任何错误和成功部署,我正在使用我的手机(索尼Xperia D 2005 )部署它。 无论如何,它说"等待位置更新"但这是永远的... 很抱歉我将要粘贴的所有代码行,但我不知道问题出在哪里。 这是我的MainActivity.cs代码:(我是xamarin的新手,我不知道这部分代码是否对这个问题负责,所以如果你需要任何额外的代码,请告诉我......)
using System;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Util;
using Android.Locations;
using Location.Droid.Services;
using Android.Content.PM;
namespace Location.Droid
{
[Activity (Label = "LocationDroid", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize |
ConfigChanges.Orientation | ConfigChanges.ScreenLayout)]
public class MainActivity : Activity
{
readonly string logTag = "MainActivity";
// make our labels
TextView latText;
TextView longText;
TextView altText;
TextView speedText;
TextView bearText;
TextView accText;
#region Lifecycle
//Lifecycle stages
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
Log.Debug (logTag, "OnCreate: Location app is becoming active");
SetContentView (Resource.Layout.Main);
// This event fires when the ServiceConnection lets the client (our App class) know that
// the Service is connected. We use this event to start updating the UI with location
// updates from the Service
App.Current.LocationServiceConnected += (object sender, ServiceConnectedEventArgs e) => {
Log.Debug (logTag, "ServiceConnected Event Raised");
// notifies us of location changes from the system
App.Current.LocationService.LocationChanged += HandleLocationChanged;
//notifies us of user changes to the location provider (ie the user disables or enables GPS)
App.Current.LocationService.ProviderDisabled += HandleProviderDisabled;
App.Current.LocationService.ProviderEnabled += HandleProviderEnabled;
// notifies us of the changing status of a provider (ie GPS no longer available)
App.Current.LocationService.StatusChanged += HandleStatusChanged;
};
latText = FindViewById<TextView> (Resource.Id.lat);
longText = FindViewById<TextView> (Resource.Id.longx);
altText = FindViewById<TextView> (Resource.Id.alt);
speedText = FindViewById<TextView> (Resource.Id.speed);
bearText = FindViewById<TextView> (Resource.Id.bear);
accText = FindViewById<TextView> (Resource.Id.acc);
altText.Text = "altitude";
speedText.Text = "speed";
bearText.Text = "bearing";
accText.Text = "accuracy";
// Start the location service:
App.StartLocationService();
}
protected override void OnPause()
{
Log.Debug (logTag, "OnPause: Location app is moving to background");
base.OnPause();
}
protected override void OnResume()
{
Log.Debug (logTag, "OnResume: Location app is moving into foreground");
base.OnResume();
}
protected override void OnDestroy ()
{
Log.Debug (logTag, "OnDestroy: Location app is becoming inactive");
base.OnDestroy ();
// Stop the location service:
App.StopLocationService();
}
#endregion
#region Android Location Service methods
///<summary>
/// Updates UI with location data
/// </summary>
public void HandleLocationChanged(object sender, LocationChangedEventArgs e)
{
Android.Locations.Location location = e.Location;
Log.Debug (logTag, "Foreground updating");
// these events are on a background thread, need to update on the UI thread
RunOnUiThread (() => {
latText.Text = String.Format ("Latitude: {0}", location.Latitude);
longText.Text = String.Format ("Longitude: {0}", location.Longitude);
altText.Text = String.Format ("Altitude: {0}", location.Altitude);
speedText.Text = String.Format ("Speed: {0}", location.Speed);
accText.Text = String.Format ("Accuracy: {0}", location.Accuracy);
bearText.Text = String.Format ("Bearing: {0}", location.Bearing);
});
}
public void HandleProviderDisabled(object sender, ProviderDisabledEventArgs e)
{
Log.Debug (logTag, "Location provider disabled event raised");
}
public void HandleProviderEnabled(object sender, ProviderEnabledEventArgs e)
{
Log.Debug (logTag, "Location provider enabled event raised");
}
public void HandleStatusChanged(object sender, StatusChangedEventArgs e)
{
Log.Debug (logTag, "Location status changed, event raised");
}
#endregion
}
}
答案 0 :(得分:0)
在这种情况下,您应该注册提供商GPS和NETWORK。
LocMgr.RequestLocationUpdates(LocationManager.GpsProvider, 2000, 0, this);
LocMgr.RequestLocationUpdates(LocationManager.NetworkProvider, 2000, 0, this);
GPS提供商在外面工作,NETWORK使用手机信号塔和无线网络。
此article可以帮助您优化代码。