GVKun编程网logo

Xamarin.Forms:Xamarin Essentials GetLocation() 在 Android 11 上运行时始终返回 null

16

如果您对Xamarin.Forms:XamarinEssentialsGetLocation()在Android11上运行时始终返回null感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Xam

如果您对Xamarin.Forms:Xamarin Essentials GetLocation() 在 Android 11 上运行时始终返回 null感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Xamarin.Forms:Xamarin Essentials GetLocation() 在 Android 11 上运行时始终返回 null的各种细节,此外还有关于Android Geocoder getFromLocationName始终返回null、android – MediaMetadataRetriever在尝试检索帧时始终返回null、android – Xamarin Forms Labs Geolocation示例、android – Xamarin.Auth得到disallowed_useragent错误[Xamarin.Forms]的实用技巧。

本文目录一览:

Xamarin.Forms:Xamarin Essentials GetLocation() 在 Android 11 上运行时始终返回 null

Xamarin.Forms:Xamarin Essentials GetLocation() 在 Android 11 上运行时始终返回 null

如何解决Xamarin.Forms:Xamarin Essentials GetLocation() 在 Android 11 上运行时始终返回 null?

我有一个 Xamarin Forms 应用程序,我正在尝试使用 Xamarin Essentials 获取手机的位置。应用面向 Android 版本 11 (API 30)。我已经在清单文件中提供了所有位置权限。它在 Android 11 以下运行良好,但在 Android 11 上有问题。

var location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Medium)); 的每个请求都返回 null。

以下是请求权限和位置的代码:

 private async Task locationPermission()
    {

        try
        {

            PermissionStatus whenInUsePermission = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
            if (whenInUsePermission != PermissionStatus.Granted)
            {
                whenInUsePermission = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
            }

            PermissionStatus locationAlwaysPermission = await Permissions.CheckStatusAsync<Permissions.LocationAlways>();
            if (locationAlwaysPermission != PermissionStatus.Granted)
            {
                locationAlwaysPermission = await Permissions.RequestAsync<Permissions.LocationAlways>();
            }

            if (whenInUsePermission == PermissionStatus.Granted || locationAlwaysPermission == PermissionStatus.Granted)
            {
                // do something
                await GetDeviceLocation();
            }
        }
        catch (Exception ex)
        {

        }
    }

    public async Task GetDeviceLocation()
    {
        try
        {
            PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
            if (status != PermissionStatus.Granted)
            {
                PermissionStatus results = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
            }

            if (status == PermissionStatus.Granted)
            {
                var location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Medium));

            }

        }
        catch (Exception ex)
        {


        }
    }

我已在清单文件中授予以下权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<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" />

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Android Geocoder getFromLocationName始终返回null

Android Geocoder getFromLocationName始终返回null

我一直在尝试对字符串进行地址解析以获取其坐标,但是我的程序总是崩溃,因为每次尝试使用getFromLocationName()它时,它都会返回null。我已经尝试修复了几个小时,但没有任何反应。这是我的代码

public class MainActivity extends Activity {    private GoogleMap mMap;    List<Address> addresses;    MarkerOptions miami;    String myLocation = "Miami,Florida";    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        if (mMap == null) {            mMap = ((MapFragment) getFragmentManager().findFragmentById(                    R.id.map)).getMap();        }        if (mMap != null) {            Geocoder geocoder = new Geocoder(this);            double latitude = 0;            double longitude = 0;            while(addresses==null){            try {                addresses = geocoder.getFromLocationName(myLocation, 1);            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            }            Address address = addresses.get(0);            if (addresses.size() > 0) {                latitude = address.getLatitude();                longitude = address.getLongitude();            }            LatLng City = new LatLng(latitude, longitude);            miami = new MarkerOptions().position(City).title("Miami");            mMap.addMarker(miami);            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15));        }}

答案1

小编典典

Geocoder并不总是返回值。您可以尝试在for循环中发送3次请求。我应该至少可以返回一次。如果不是这样,则可能是连接问题,也可能是其他问题,例如服务器不回复您的请求。尝试看看这些线程:

Geocoder并不总是返回值,而geocoder.getFromLocationName仅返回null

更新:

我也有一个while循环,但是我曾经尝试最多10次。有时,即使连接到互联网,它也从不返回任何内容。然后,我使用这种可靠得多的方法来每次获取地址:

public JSONObject getLocationInfo() {        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");        HttpClient client = new DefaultHttpClient();        HttpResponse response;        StringBuilder stringBuilder = new StringBuilder();        try {            response = client.execute(httpGet);            HttpEntity entity = response.getEntity();            InputStream stream = entity.getContent();            int b;            while ((b = stream.read()) != -1) {                stringBuilder.append((char) b);            }        } catch (ClientProtocolException e) {            } catch (IOException e) {        }        JSONObject jsonObject = new JSONObject();        try {            jsonObject = new JSONObject(stringBuilder.toString());        } catch (JSONException e) {            e.printStackTrace();        }        return jsonObject;    }

我这样称呼它:

JSONObject ret = getLocationInfo(); JSONObject location;String location_string;try {    location = ret.getJSONArray("results").getJSONObject(0);    location_string = location.getString("formatted_address");    Log.d("test", "formattted address:" + location_string);} catch (JSONException e1) {    e1.printStackTrace();}

希望这可以帮助。我也厌倦了依赖地理编码器。这对我有用。如果用经纬度坐标替换URL,并在Web浏览器中看到返回的JSON对象。您会看到刚刚发生的事情。

android – MediaMetadataRetriever在尝试检索帧时始终返回null

android – MediaMetadataRetriever在尝试检索帧时始终返回null

我正试图通过URL获取流媒体视频的第一帧. getFrameAtTime似乎总是返回null而没有Exception.

@Override
        protected Bitmap doInBackground(String... strings) {
            String url = strings[0];
            MediaMetadataRetriever retriever = new MediaMetadataRetriever(); //Is actually available in Api levels < 10 all the way to 5
            try {
                retriever.setDataSource(url,new HashMap<String,String>());
                return retriever.getFrameAtTime(1000,MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
            } catch (IllegalArgumentException ex) {
                ex.printstacktrace();
                Log.e(TAG,"problem retrieving frame " + ex);
            } catch (RuntimeException ex) {
                ex.printstacktrace();
                Log.e(TAG,"problem retrieving frame " + ex);
            } finally {
                try {
                    retriever.release();
                } catch (RuntimeException ex) {
                    Log.e(TAG,"problem retrieving frame " + ex);
                }
            }
            return null;
        }

解决方法

你试过 FFmpegMediaMetadataRetriever吗?:

FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
retriever.setDataSource(url,String>());
Bitmap b = retriever.getFrameAtTime(1000000,FFmpegMediaMetadataRetriever.OPTION_CLOSEST_SYNC); // get frame at one second
retriever.release();

android – Xamarin Forms Labs Geolocation示例

android – Xamarin Forms Labs Geolocation示例

我下载了Xamarin Forms Labs的源代码,并试图为android项目运行geolocator示例.单击“获取位置”按钮时,返回的位置消息为“已取消”.我尝试按照以下步骤在VS中创建一个新的xamarin表单项目:file>新项目> Xamarin PCL.当我在该项目中安装xamarin表单实验室并使用它时,我收到相同的消息.但是,我可以使用Windows Phone示例.我不确定安装Xamarin的Android模拟器是否可以连接到我的机器上的网络.这可以解释为什么它无法获得该位置

解决方法:

(这有点晚了,但只是提交,万一有人通过搜索找到了这个)

仔细检查您的平台权限.

对于Android:
您必须在Android项目中申请以下权限

> ACCESS_COARSE_LOCATION
> ACCESS_FINE_LOCATION

对于iOS:
在iOS 8中,您现在必须在位置管理器上调用RequestWhenInUseAuthorization或RequestAlwaysAuthorization.

此外,您需要将简要命名的NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription添加到Info.plist中.

对于Windows Phone:
您必须设置ID_CAP_LOCATION权限.

Xamarin表格
如果您正在开发Xamarin Forms应用程序,您可以尝试使用Geolocator(Xamarin.Plugin)示例,只需记住添加您的权限:)

样品:

var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;

var position = await locator.GetPositionAsync (timeout: 10000);

Console.WriteLine ("Position Status: {0}", position.Timestamp);
Console.WriteLine ("Position Latitude: {0}", position.Latitude);
Console.WriteLine ("Position Longitude: {0}", position.Longitude);

android – Xamarin.Auth得到disallowed_useragent错误[Xamarin.Forms]

android – Xamarin.Auth得到disallowed_useragent错误[Xamarin.Forms]

我试图用谷歌和Facebook实现登录.我已成功使用Xamarin.Auth实现Facebook登录,但在Google登录中我收到错误disallowed_useragent,我知道Google已经更改了他们的政策,并且不允许使用WebView登录,但是还有其他方法可以在Xamarin中使用Google登录.表格(便携式)?

enter image description here

谢谢.

解决方法:

这是Google的新限制,他们不再允许在嵌入式网页浏览中使用他们的登录界面.因此,您可以在“本地视图” – 外部浏览器(chrome标签,safari等)中为您的应用实施授权.他们会将结果返回给您的应用.

Google的工作示例如下:@H_301_11@Authenticating Users with an Identity Provider

今天关于Xamarin.Forms:Xamarin Essentials GetLocation() 在 Android 11 上运行时始终返回 null的讲解已经结束,谢谢您的阅读,如果想了解更多关于Android Geocoder getFromLocationName始终返回null、android – MediaMetadataRetriever在尝试检索帧时始终返回null、android – Xamarin Forms Labs Geolocation示例、android – Xamarin.Auth得到disallowed_useragent错误[Xamarin.Forms]的相关知识,请在本站搜索。

本文标签: