Xamarin 形成地理位置错误的结果
Xamarin forms Geolocation wrong results
我正在开发 Xamarin 表单应用程序并尝试使用 Xamarin.Essentials 地理定位来确定我的 Android 设备位置。我按照 https://docs.microsoft.com/en-us/xamarin/essentials/geolocation?tabs=android 的步骤进行操作,但是结果完全不准确,因为我目前在中欧,但结果的纬度和经度是 37.4219983333333 和 -122.084,我猜这是 Google 总部的位置。
这是我的 AndroidManifest.xml 的样子
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.workingwithmaps">
<uses-sdk android:minSdkVersion="21" />
<application android:label="WorkingWithMaps.Android">
<meta-data android:name="com.google.android.geo.API_KEY" android:value="MY_API_KEY" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<!-- Necessary for apps that target Android 9.0 or higher -->
<uses-library android:name="org.apache.http.legacy" android:required="false" />
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-feature android:name="android.hardware.location.network" android:required="false" />
</manifest>
我的MainActivity.csclass:
[Activity(Label = "Resti", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Xamarin.FormsMaps.Init(this, savedInstanceState);
LoadApplication(new App());
}
}
然后我从 ContentPage 中这样调用它:
protected async override void OnAppearing()
{
Xamarin.Essentials.Location location = null;
var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
cts = new CancellationTokenSource();
location = await Geolocation.GetLocationAsync(request, cts.Token);
base.OnAppearing();
}
知道我做错了什么吗?
我正在模拟器上测试功能,感谢 Jason,我发现模拟器没有实际的 GPS 功能,它默认使用模拟位置。
我正在开发 Xamarin 表单应用程序并尝试使用 Xamarin.Essentials 地理定位来确定我的 Android 设备位置。我按照 https://docs.microsoft.com/en-us/xamarin/essentials/geolocation?tabs=android 的步骤进行操作,但是结果完全不准确,因为我目前在中欧,但结果的纬度和经度是 37.4219983333333 和 -122.084,我猜这是 Google 总部的位置。
这是我的 AndroidManifest.xml 的样子
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.workingwithmaps">
<uses-sdk android:minSdkVersion="21" />
<application android:label="WorkingWithMaps.Android">
<meta-data android:name="com.google.android.geo.API_KEY" android:value="MY_API_KEY" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<!-- Necessary for apps that target Android 9.0 or higher -->
<uses-library android:name="org.apache.http.legacy" android:required="false" />
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-feature android:name="android.hardware.location.network" android:required="false" />
</manifest>
我的MainActivity.csclass:
[Activity(Label = "Resti", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Xamarin.FormsMaps.Init(this, savedInstanceState);
LoadApplication(new App());
}
}
然后我从 ContentPage 中这样调用它:
protected async override void OnAppearing()
{
Xamarin.Essentials.Location location = null;
var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
cts = new CancellationTokenSource();
location = await Geolocation.GetLocationAsync(request, cts.Token);
base.OnAppearing();
}
知道我做错了什么吗?
我正在模拟器上测试功能,感谢 Jason,我发现模拟器没有实际的 GPS 功能,它默认使用模拟位置。