[Android-Location]无法从LocationManager获取经度和纬度 [英] [Android-Location]Can not get Longitude and Latitude from LocationManager

查看:396
本文介绍了[Android-Location]无法从LocationManager获取经度和纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用LocationManager通过经度"和纬度"获取用户地址,但是当我调用location.getLongitude()和location.getLatitude()时,它返回0给我.我设法使用Google,但没人遇到我的问题.

I used LocationManager to get user address by "Longitude" and "Latitude" but when i call location.getLongitude() and location.getLatitude(), it return 0 back to me. I manage to Google but have nobody meet my issue.

杰里米·爱德华兹先生向我建议:

Mr.Jeremy Edwards suggest me that:

确保在手机的设置中启用了基于网络的位置.您可以检测到这种情况,并在确实需要时提示用户启用它. 我相信这是启动此活动的代码行.

Make sure that network based location is enabled in the phone's settings. You can detect this situation and prompt the user to enable it if you really need it. I believe this is the line of code to launch this activity.

我这样做,但是可以帮上忙.有时候给我经度和纬度,有时候却不给我.

I do that but it can help. Sometime that give me Longitude and Latitude, some time not.

这是我的代码:

String locationProvider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager
            .getLastKnownLocation(locationProvider);

AndroidMainfest.xml中的权限:

The permission in the AndroidMainfest.xml:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

有人可以帮助我吗?预先谢谢你!

Can anyone help me? Thank you in advance!

推荐答案

您正试图仅从网络提供商那里获取信息.尝试使用下面的代码.它将帮助您从GPS提供商获取数据,如果该值不可用,那么它将帮助您从网络提供商获取坐标

You are trying to get only from network provider. Try using below code. It will help you get from GPS Provider and if value not available, then it will help you get coordinates from network provider

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute



        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no GPS Provider and no network provider is enabled
        } 
        else 
        {   // Either GPS provider or network provider is enabled

            // First get location from Network Provider
            if (isNetworkEnabled) 
            {
                locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) 
                {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) 
                    {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        this.canGetLocation = true;
                    }
                }
            }// End of IF network enabled

            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) 
            {
                locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) 
                {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) 
                    {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        this.canGetLocation = true;
                    }
                }

            }// End of if GPS Enabled
        }// End of Either GPS provider or network provider is enabled

这篇关于[Android-Location]无法从LocationManager获取经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆