簡體   English   中英

位置對象為空

[英]Location object is null

我是安卓編程的新手。 我想開發一個使用位置 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 所需的版本。

您可以從

onLocationChanged(Location location)

您是否已 獲得 Google Maps API 密鑰並在google api 控制台中設置了所有內容?

 best = LocationManager.NETWORK_PROVIDER;
 loc = (LocationManager) getSystemService(LOCATION_SERVICE);
 googleMap.setMyLocationEnabled(true);
 loc.requestLocationUpdates(best,1000,0,this);
 location=loc.getLastKnownLocation(best);

嘗試這個。 您的代碼應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM