2016-06-28 5 views
3

공유 코드 라이브러리에서 다음 코드를 사용하여 사용자의 현재 위치를 가져 오려고했습니다. 프로젝트의Xamarin Plugin.Geolocator가 작동하지 않습니다.

소스 Dropbox link

using System; 
using System.Diagnostics; 
using Plugin.Geolocator; 
using Plugin.Geolocator.Abstractions; 

namespace SampleApp 
{ 
    public class LocationManager 
    { 
     private DateTimeOffset timestamp; 

     private double latitude; 

     private double longitude; 

     IGeolocator locator; 

     public LocationManager() 
     { 
      asyncInitLocation(); 
     } 

     private async void asyncInitLocation() 
     { 
      try 
      { 
       locator = CrossGeolocator.Current; 
       locator.DesiredAccuracy = 50; 

       if (locator.IsGeolocationEnabled) 
       { 
        var position = await locator.GetPositionAsync(timeoutMilliseconds: 20000); 
        timestamp = position.Timestamp; 
        latitude = position.Latitude; 
        longitude = position.Longitude; 
       } 
       else 
       { 
        Debug.WriteLine("Geolocation is disabled!"); 
       } 
      } 
      catch (Exception ex) 
      { 
       Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex); 
      } 
     } 

     public DateTimeOffset getTimestamp() 
     { 
      return this.timestamp; 
     } 

     public double getLatitude() 
     { 
      return this.latitude; 
     } 

     public double getLongitude() 
     { 
      return this.longitude; 
     } 

     public void refresh() 
     { 
      asyncInitLocation(); 
     } 
    } 
} 

나는 아래의 오류가 발생하고 여기 사용할 수 있습니다. Google은 나를 도왔다!

Unable to get location, may need to increase timeout: System.Threading.Tasks.TaskCanceledException: A task was canceled. 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0002d] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:179 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
    at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult() [0x00000] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535 
    at Plugin.Geolocator.GeolocatorImplementation+<GetPositionAsync>d__27.MoveNext() [0x00597] in C:\projects\xamarin-plugins\Geolocator\Geolocator\Geolocator.Plugin.Android\GeolocatorImplementation.cs:175 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [0x0000c] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00027] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:176 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
    at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult() [0x00000] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 
    at BugsAndDrugs.LocationManager+<asyncInitLocation>c__async0.MoveNext() [0x000d4] in /Users/myname/Perforce/depot/test_sandbox/Playground/SampleApp/SampleApp/Manager/LocationManager.cs:32 

내가 뭔가를 놓치고 있습니까 :

Unable to get location, may need to increase timeout: System.NotImplementedException: This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation. 
    at Plugin.Geolocator.CrossGeolocator.get_Current() <0x52c7bb60 + 0x0003f> in <filename unknown>:0 
    at SampleApp.LocationManager+<asyncInitLocation>c__async0.MoveNext() <0x52c7ae60 + 0x000bf> in <filename unknown>:0 

업데이트 :이

<?xml version="1.0" encoding="utf-8"?> 
<packages> 
    <package id="Microsoft.Bcl" version="1.1.10" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarintvos10+xamarinwatchos10+xamarinios10" /> 
    <package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarintvos10+xamarinwatchos10+xamarinios10" /> 
    <package id="Microsoft.Net.Http" version="2.2.29" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarintvos10+xamarinwatchos10+xamarinios10" /> 
    <package id="modernhttpclient" version="2.4.2" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarintvos10+xamarinwatchos10+xamarinios10" /> 
    <package id="Newtonsoft.Json" version="8.0.3" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarintvos10+xamarinwatchos10+xamarinios10" /> 
    <package id="Xam.Plugin.Geolocator" version="3.0.4" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarintvos10+xamarinwatchos10+xamarinios10" /> 
</packages> 

가 지금은 아래의 오류 메시지가 같은 packages.config 파일이 보인다? 나는 프로젝트에 nuget을 사용하여 플러그인을 추가했다.

+0

@jzeferino 님이 질문을 업데이트했습니다. – brainless

+0

@jzeferino 네이티브 – brainless

+0

@jzef erino 위치에 액세스하는 데 필요한 권한이 플러그인 자체에 추가 될 것이라고 설명서에 나와 있습니다! – brainless

답변

2

문제를 다시 테스트 한 후 을 새로 작성한 후 Geolocator.PluginAndroid Project에 추가했습니다.

참고 :

플러그인 설명서를 말한다 :

ACCESS_COARSE_LOCATION & ACCESS_FINE_LOCATION 권한이 필요 하지만 라이브러리가 자동으로이 추가됩니다. 또한 사용자가 Marshmallow를 실행하는 경우 플러그인은 에 자동으로 런타임 권한을 요청합니다.

하지만 Android에서이 권한을 수동으로 추가해야합니다. MainActivity가

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="pt.sushiathome.shared_geolocator"> 
    <uses-sdk android:minSdkVersion="15" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <application android:label="shared.geolocator"> 
    </application> 
</manifest> 

:

Button button = FindViewById<Button>(Resource.Id.myButton); 

button.Click += async delegate 
{ 
    try 
    { 
     var locator = CrossGeolocator.Current; 
     locator.DesiredAccuracy = 50; 

     if (locator.IsGeolocationEnabled) 
     { 
      var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000); 
      position.ToString(); 
     } 
     else 
     { 
      System.Diagnostics.Debug.WriteLine("Geolocation is disabled!"); 
     } 
    } 
    catch (Exception ex) 
    { 
     System.Diagnostics.Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex); 
    } 
}; 

그냥 position.ToString();에 중단 점을 배치하고 내가 위치를 가지고 같은

는 매니페스트 보인다. (에뮬레이터와 실제 장치에서 테스트.)

NOTE2에게 : 에뮬레이터에서 GPS에 전환

시도 : enter image description here

3

내가 같은 오류가 발생했다를, 나는 클린업으로 해결 솔루션을 다시 빌드하고 모든 프로젝트에 종속성이 추가되었는지 확인하십시오.

관련 문제