我无法获得任何纬度/经度(Gps/Wifi)
代码如下:
我添加了: <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <:uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Android.Locations
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
//-- add these
using Android.Locations;
using System.Collections.Generic;
using System.Threading;
using System.Text;
using System.Linq;
using System.Xml;
namespace GPSWifi
{
[Activity (Label = "GPSWifi", MainLauncher = true)]
public class Activity1 : Activity
{
int count = 1;
private LocationManager _locMgr;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
InitializeLocationManager();
}
private void InitializeLocationManager()
{
_locMgr = (LocationManager) GetSystemService(LocationService);
var locationCriteria = new Criteria();
locationCriteria.Accuracy = Accuracy.NoRequirement;
locationCriteria.PowerRequirement = Power.NoRequirement;
string locationProvider = _locMgr.GetBestProvider(locationCriteria, true);
_locMgr.RequestLocationUpdates (locationProvider, 2000, 1, this);
}
protected override void OnResume ()
{
base.OnResume ();
_locMgr.RequestLocationUpdates (LocationManager.GpsProvider, 2000, 1, this);
}
protected override void OnPause ()
{
base.OnPause ();
_locMgr.RemoveUpdates (this);
}
public void OnProviderDisabled(string provider) {}
public void OnProviderEnabled(string provider) {}
public void OnLocationChanged (Location location)
{
var locationText = FindViewById<TextView>(Resource.Id.locationTextView);
locationText.Text = String.Format ("Latitude = {0}, Longitude = {1}",
location.Latitude, location.Longitude);
}
}
}
非常感谢您的帮助。
- 错误信息 :
C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(2,2):错误 MSB4018:“Aapt”任务意外失败。
System.InvalidOperationException:序列不包含任何元素
在 System.Linq.Enumerable.First[TSource](IEnumerable`1 源)
在 Xamarin.Android.Tasks.BuildToolsUtils.GetBuildToolsPath(字符串 androidSdkDirectory)
在 Xamarin.Android.Tasks.Aapt.GenerateFullPathToTool()
在 Microsoft.Build.Utilities.ToolTask.ComputePathToTool()
在 Microsoft.Build.Utilities.ToolTask.Execute()
在 Xamarin.Android.Tasks.Aapt.Execute()
在 Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
在 Microsoft.Build.BackEnd.TaskBuilder.d__20.MoveNext() (MSB4018) (GPSWifi)