0

此代码基于以下链接:
http ://docs.xamarin.com/guides/android/platform_features/maps_and_location/part_3_-_location_services

我无法获得任何纬度/经度(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)

4

1 回答 1

0

这个错误:The "Aapt" task failed unexpectedly.最近才开始发生 - 我认为这与来自 Android/Google 的最新 sdk 有关 - 我认为他们已经移动了一个文件位置。

检查其他问题,例如Xamarin Studio 2 - latest stable update - Error execution task Aapt: The source sequence is empty

然后检查 Xamarin 支持 - 他们将发布更新来解决此问题。

于 2013-05-19T16:44:17.263 回答