C# Xamarin android在位置更改之前获取位置 使用Android.App; 使用Android.Widget; 使用Android.OS; 使用Android.Locations; 使用Android.Content; [活动(Label=“Getlocation”,MainLauncher=true)] 公共类主活动:活动,ILocationListener { 按钮bttnGo; TextView-txtLoc; 职位经理职位经理; 位置当前位置; 字符串提供者; 创建时受保护的覆盖无效(捆绑包) { base.OnCreate(bundle); //从“主”布局资源设置视图 SetContentView(Resource.Layout.Main); bttnGo=findviewbyd(Resource.Id.get\u address\u按钮); txtLoc=findviewbyd(Resource.Id.location\u text); 初始化ElocationManager(); bttnGo.Click+=bttnGo\u Click; } 异步void BttnGo\u单击(对象发送方,事件参数e) { 如果(currentLocation==null) { txtLoc.Text=“未找到位置,请尝试四处移动”; } 其他的 { txtLoc.Text=currentLocation.ToString(); } } private void InitializeLocationManager() { locMgr=(LocationManager)GetSystemService(LocationService); criteriaForLocationService=新标准 { 准确度=准确度。很好 }; IList acceptableLocationProviders=locMgr.GetProviders(criteriaForLocationService,true); if(acceptableLocationProviders.Any()) { provider=acceptableLocationProviders.First(); } 其他的 { provider=string.Empty; } } 受保护的覆盖void OnResume() { base.OnResume(); Locamgr.RequestLocationUpdate(提供程序,2000,0,this); } 受保护的覆盖void OnPause() { base.OnPause(); Locamgr.RemoveUpdate(本); } 已更改位置上的公共无效(位置) { 当前位置=位置; 如果(currentLocation==null) { txtLoc.Text=“未检测到任何位置”; } 其他的 { txtLoc.Text=location.Latitude.ToString(); } } 公共无效OnProviderDisabled(字符串提供程序) { } 公共无效OnProviderEnabled(字符串提供程序) { } public void OnStatusChanged(字符串提供程序,[GeneratedEnum]可用性状态,Bundle extra) { } } }
我的编码知识很少,我只是按照教程检索当前位置 我的问题是,我只能在实际位置更改时检索位置。我知道这可能是因为在OnLocationChanged发生之前currentLocation没有获得值。如何解决这个问题 Xamarin android在位置更改之前获取位置 您可以使用方法来实现此功能。下面是一个简单的演示:C# Xamarin android在位置更改之前获取位置 使用Android.App; 使用Android.Widget; 使用Android.OS; 使用Android.Locations; 使用Android.Content; [活动(Label=“Getlocation”,MainLauncher=true)] 公共类主活动:活动,ILocationListener { 按钮bttnGo; TextView-txtLoc; 职位经理职位经理; 位置当前位置; 字符串提供者; 创建时受保护的覆盖无效(捆绑包) { base.OnCreate(bundle); //从“主”布局资源设置视图 SetContentView(Resource.Layout.Main); bttnGo=findviewbyd(Resource.Id.get\u address\u按钮); txtLoc=findviewbyd(Resource.Id.location\u text); 初始化ElocationManager(); bttnGo.Click+=bttnGo\u Click; } 异步void BttnGo\u单击(对象发送方,事件参数e) { 如果(currentLocation==null) { txtLoc.Text=“未找到位置,请尝试四处移动”; } 其他的 { txtLoc.Text=currentLocation.ToString(); } } private void InitializeLocationManager() { locMgr=(LocationManager)GetSystemService(LocationService); criteriaForLocationService=新标准 { 准确度=准确度。很好 }; IList acceptableLocationProviders=locMgr.GetProviders(criteriaForLocationService,true); if(acceptableLocationProviders.Any()) { provider=acceptableLocationProviders.First(); } 其他的 { provider=string.Empty; } } 受保护的覆盖void OnResume() { base.OnResume(); Locamgr.RequestLocationUpdate(提供程序,2000,0,this); } 受保护的覆盖void OnPause() { base.OnPause(); Locamgr.RemoveUpdate(本); } 已更改位置上的公共无效(位置) { 当前位置=位置; 如果(currentLocation==null) { txtLoc.Text=“未检测到任何位置”; } 其他的 { txtLoc.Text=location.Latitude.ToString(); } } 公共无效OnProviderDisabled(字符串提供程序) { } 公共无效OnProviderEnabled(字符串提供程序) { } public void OnStatusChanged(字符串提供程序,[GeneratedEnum]可用性状态,Bundle extra) { } } },c#,android,xamarin,C#,Android,Xamarin,我的编码知识很少,我只是按照教程检索当前位置 我的问题是,我只能在实际位置更改时检索位置。我知道这可能是因为在OnLocationChanged发生之前currentLocation没有获得值。如何解决这个问题 Xamarin android在位置更改之前获取位置 您可以使用方法来实现此功能。下面是一个简单的演示: using Android.App; using Android.Widget; using Android.OS; using Android.Locations; using A
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Locations;
using Android.Content;
[Activity(Label = "Getlocation", MainLauncher = true)]
public class MainActivity : Activity, ILocationListener
{
Button bttnGo;
TextView txtLoc;
LocationManager locMgr;
Location currentLocation;
string provider;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
bttnGo = FindViewById<Button>(Resource.Id.get_address_button);
txtLoc = FindViewById<TextView>(Resource.Id.location_text);
InitializeLocationManager();
bttnGo.Click += BttnGo_Click;
}
async void BttnGo_Click(object sender, EventArgs e)
{
if (currentLocation == null)
{
txtLoc.Text = "No location found, try moving around";
}
else
{
txtLoc.Text = currentLocation.ToString();
}
}
private void InitializeLocationManager()
{
locMgr = (LocationManager)GetSystemService(LocationService);
Criteria criteriaForLocationService = new Criteria
{
Accuracy = Accuracy.Fine
};
IList<string> acceptableLocationProviders = locMgr.GetProviders(criteriaForLocationService, true);
if(acceptableLocationProviders.Any())
{
provider = acceptableLocationProviders.First();
}
else
{
provider = string.Empty;
}
}
protected override void OnResume()
{
base.OnResume();
locMgr.RequestLocationUpdates(provider, 2000, 0, this);
}
protected override void OnPause()
{
base.OnPause();
locMgr.RemoveUpdates(this);
}
public void OnLocationChanged(Location location)
{
currentLocation = location;
if (currentLocation == null)
{
txtLoc.Text = "No location detected";
}
else
{
txtLoc.Text = location.Latitude.ToString();
}
}
public void OnProviderDisabled(string provider)
{
}
public void OnProviderEnabled(string provider)
{
}
public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
{
}
}
}
LocationManager LocationManager=(LocationManager)GetSystemService(Context.LocationService);
IList lp=locationManager.AllProviders;
foreach(lp中的字符串项)
{
Log.Debug(“标记”,“可用位置服务:”+项);
}
//指示用于选择位置提供程序的应用程序条件的类。
标准=新标准();
//指示是否允许提供程序产生金钱成本。
criteria.CostAllowed=false;
//设置所需的定位精度
标准。准确度=准确度。粗略;
//返回最符合给定条件的提供程序的名称
字符串providerName=locationManager.GetBestProvider(条件为true);
Log.Debug(“8023”,“----位置服务:”+providerName);
//直接选择位置提供程序
//字符串providerName=LocationManager.GpsProvider;
Location Location=locationManager.GetLastKnownLocation(providerName);
双纬度=位置纬度;
双lng=位置。经度;
txtLoc.Text=“纬度=“+lat+”,经度=“+lng;
Xamarin android在位置更改之前获取位置
您可以使用方法来实现此功能。下面是一个简单的演示:
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Locations;
using Android.Content;
[Activity(Label = "Getlocation", MainLauncher = true)]
public class MainActivity : Activity, ILocationListener
{
Button bttnGo;
TextView txtLoc;
LocationManager locMgr;
Location currentLocation;
string provider;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
bttnGo = FindViewById<Button>(Resource.Id.get_address_button);
txtLoc = FindViewById<TextView>(Resource.Id.location_text);
InitializeLocationManager();
bttnGo.Click += BttnGo_Click;
}
async void BttnGo_Click(object sender, EventArgs e)
{
if (currentLocation == null)
{
txtLoc.Text = "No location found, try moving around";
}
else
{
txtLoc.Text = currentLocation.ToString();
}
}
private void InitializeLocationManager()
{
locMgr = (LocationManager)GetSystemService(LocationService);
Criteria criteriaForLocationService = new Criteria
{
Accuracy = Accuracy.Fine
};
IList<string> acceptableLocationProviders = locMgr.GetProviders(criteriaForLocationService, true);
if(acceptableLocationProviders.Any())
{
provider = acceptableLocationProviders.First();
}
else
{
provider = string.Empty;
}
}
protected override void OnResume()
{
base.OnResume();
locMgr.RequestLocationUpdates(provider, 2000, 0, this);
}
protected override void OnPause()
{
base.OnPause();
locMgr.RemoveUpdates(this);
}
public void OnLocationChanged(Location location)
{
currentLocation = location;
if (currentLocation == null)
{
txtLoc.Text = "No location detected";
}
else
{
txtLoc.Text = location.Latitude.ToString();
}
}
public void OnProviderDisabled(string provider)
{
}
public void OnProviderEnabled(string provider)
{
}
public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
{
}
}
}
LocationManager LocationManager=(LocationManager)GetSystemService(Context.LocationService);
IList lp=locationManager.AllProviders;
foreach(lp中的字符串项)
{
Log.Debug(“标记”,“可用位置服务:”+项);
}
//指示用于选择位置提供程序的应用程序条件的类。
标准=新标准();
//指示是否允许提供程序产生金钱成本。
criteria.CostAllowed=false;
//设置所需的定位精度
标准。准确度=准确度。粗略;
//返回最符合给定条件的提供程序的名称
字符串providerName=locationManager.GetBestProvider(条件为true);
Log.Debug(“8023”,“----位置服务:”+providerName);
//直接选择位置提供程序
//字符串providerName=LocationManager.GpsProvider;
Location Location=locationManager.GetLastKnownLocation(providerName);
双纬度=位置纬度;
双lng=位置。经度;
txtLoc.Text=“纬度=“+lat+”,经度=“+lng;