【问题标题】:Location object is null位置对象为空
【发布时间】:2015-07-02 18:28:15
【问题描述】:

我是安卓编程新手。 我想开发一个使用 Location API 的应用程序。 我为此目的使用位置管理器。 代码:

        public Location getLocation() {
            try {
                locationManager = (LocationManager) mContext
                        .getSystemService(LOCATION_SERVICE);

                isGPSEnabled = locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);

                isNetworkEnabled = locationManager
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                if (!isGPSEnabled && !isNetworkEnabled) {
                } else {
                    this.canGetLocation = true;
                    if (isNetworkEnabled) {
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("clickindia", "Network");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            Log.d("message","location"+location);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }

                    if (isGPSEnabled) {
                        if (location == null) {
                            locationManager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER,
                                    MIN_TIME_BW_UPDATES,
                                    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                            Log.d("message", "GPS Enabled");
                            if (locationManager != null) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                }
                            }
                        }
                    }
                }

清单文件中已经提到了所有必要的权限。 我正在手机上运行代码,似乎getLastKnownLocation() 没有做任何事情。请帮助我卡住了

我无法使用新的 FusedLocationApi,因为我的手机的 google play 服务版本低于支持 googleApiClient 和 LocationServices 所需的版本。

【问题讨论】:

  • 请说明投反对票的原因..

标签: java android


【解决方案1】:

您有got Google Maps API key 并在google api console 中设置所有内容吗?

【讨论】:

  • 是的,我设置了一切
【解决方案2】:
 best = LocationManager.NETWORK_PROVIDER;
 loc = (LocationManager) getSystemService(LOCATION_SERVICE);
 googleMap.setMyLocationEnabled(true);
 loc.requestLocationUpdates(best,1000,0,this);
 location=loc.getLastKnownLocation(best);

试试这个。 您的代码应该可以工作。

【讨论】:

  • 你的代码在这里太复杂了。使用库和所有实现发布您的完整代码。此外,如果您可以上传清单会更好
  • 试试这个link
【解决方案3】:

您可以从

获取位置
onLocationChanged(Location location)

【讨论】:

  • 那么,您的设备已开启 GPS,数据网络已开启,Manifest 中的权限设置正常,位置管理器中的提供商使用 GPS 和网络,但您仍然没有收到关于LocationChanged 的​​任何数据?
  • 我认为您的情况还缺少其他内容...如果您发送更多代码,我们可能会找到解决该问题的方法
猜你喜欢
  • 2017-08-19
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多