获取0.0的纬度和经度,同时在地图中显示当前位置



我使用以下代码来获取我的当前位置。但我面临的问题是,对于纬度和经度,它总是返回0.0。我已经打开了手机的GPS设置,并设置了所有权限。但我还是要面对这个。

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
customLocationListener = new CustomLocationListener();
locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER,
        0,
        0,
        customLocationListener);
class CustomLocationListener implements LocationListener{ ............
      public void onLocationChanged(Location argLocation) { 
         if(location != null) {     
        int latitude=(int)(argLocation.getLatitude()*1E6);   
        int longitude=(int)(argLocation.getLongitude()*1E6);

              }
       } ........ }

你们有人知道为什么吗?

注意:我的活动扩展MapActivity而不是Location Listener

也检查这是原因之一。去地图默认应用程序,允许用户允许纬度和经度选项。在我们的应用程序中,当GPS信号闪烁和停止时,我们将获得纬度和经度。


编辑1

  1. 打开谷歌地图应用程序,点击地图上的按钮获取当前位置

  2. 当你允许谷歌地图的权限,那么只有其他应用程序的权限将工作。(我觉得这是大臭虫)

  3. 现在打开你的应用程序,试着获取当前位置,它会工作的

这是丑陋的,但我们已经按照这些步骤获得当前位置在我们的应用程序。

试试这个

locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);

        Criteria locationCritera = new Criteria();
        String providerName = locationManager.getBestProvider(locationCritera,
                true);
        if(providerName!=null)
            location = locationManager.getLastKnownLocation(providerName);
        locationListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, locationListener);

在mylocationlistener设置你的位置对象

private class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location loc) {
        if (loc != null) {
            location = loc; 
        }
    }
    public void onProviderDisabled(String provider) {
    }
    public void onProviderEnabled(String provider) {
    }
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

这是你想要的…!

public void getCurrentLocation() {
        LocationManager locationManager;
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) getSystemService(context);
        Criteria crta = new Criteria();
        crta.setAccuracy(Criteria.ACCURACY_FINE);
        crta.setAltitudeRequired(false);
        crta.setBearingRequired(false);
        crta.setCostAllowed(true);
        crta.setPowerRequirement(Criteria.POWER_LOW);
        String provider = locationManager.getBestProvider(crta, true);
        locationManager.requestLocationUpdates(provider, 1000, 0,
                new LocationListener() {
                    @Override
                    public void onStatusChanged(String provider, int status,
                            Bundle extras) {
                    }
                    @Override
                    public void onProviderEnabled(String provider) {
                    }
                    @Override
                    public void onProviderDisabled(String provider) {
                    }
                    @Override
                    public void onLocationChanged(Location location) {
                        if (location != null) {
                            double lat = location.getLatitude();
                            double lng = location.getLongitude();
                            if (lat != 0.0 && lng != 0.0) {
                                System.out.println("WE GOT THE LOCATION");
                                System.out.println(lat);
                                System.out.println(lng);                                                         
                            }
                        }
                    }
                });
    }

CustomLocationManager.Java

import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class CustomLocationManager {
    private LocationManager mLocationManager;
    private LocationValue locationValue;
    private Location networkLocation = null;
    private Location gpsLocation = null;
    private Timer mTimer;
    private boolean isGpsEnabled = false;
    private boolean isNetworkEnabled = false;
    private static CustomLocationManager _instance;
    private CustomLocationManager() {}
    public static CustomLocationManager getCustomLocationManager() {
        if (_instance == null) {
            _instance = new CustomLocationManager();
        }
        return _instance;
    }
    public LocationManager getLocationManager(Context context) {
        if (mLocationManager == null)
            mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        return mLocationManager;
    }
    public boolean getCurrentLocation(Context context, LocationValue result) {
        locationValue = result;
        if (mLocationManager == null)
            mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        try {
            isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {}
        try {
            isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {}
        if (!isGpsEnabled && !isNetworkEnabled)
            return false;
        if (isGpsEnabled)
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);
        if (isNetworkEnabled)
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkLocationListener);
        mTimer = new Timer();
        mTimer.schedule(new GetLastKnownLocation(), 20000);
        return true;
    }
    LocationListener gpsLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            mTimer.cancel();
            locationValue.getCurrentLocation(location);
            mLocationManager.removeUpdates(this);
            mLocationManager.removeUpdates(networkLocationListener);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };
    private LocationListener networkLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            mTimer.cancel();
            locationValue.getCurrentLocation(location);
            mLocationManager.removeUpdates(this);
            mLocationManager.removeUpdates(gpsLocationListener);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };
    private class GetLastKnownLocation extends TimerTask {
        CurrentLocationHandler handler;
        GetLastKnownLocation() {
            handler = new CurrentLocationHandler();
        }
        @Override
        public void run() {
            mLocationManager.removeUpdates(gpsLocationListener);
            mLocationManager.removeUpdates(networkLocationListener);
            if (isGpsEnabled)
                gpsLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (isNetworkEnabled)
                networkLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            handler.sendEmptyMessage(0);
        }
    }
    private class CurrentLocationHandler extends Handler {
        @Override
        public final void handleMessage(Message msg) {
            if (gpsLocation != null && networkLocation != null) {
                if (gpsLocation.getTime() > networkLocation.getTime())
                    locationValue.getCurrentLocation(gpsLocation);
                else
                    locationValue.getCurrentLocation(networkLocation);
                return;
            }
            if (gpsLocation != null) {
                locationValue.getCurrentLocation(gpsLocation);
                return;
            }
            if (networkLocation != null) {
                locationValue.getCurrentLocation(networkLocation);
                return;
            }
            locationValue.getCurrentLocation(null);
        }
    }
}


LocationValue.Java

import android.location.Location;
public abstract class LocationValue {
    public abstract void getCurrentLocation(Location location);
}

YourActivity.Java
private void getCurrentLocation() {
        CustomLocationManager.getCustomLocationManager().getCurrentLocation(this, locationValue);
    }
    public LocationValue locationValue = new LocationValue() {
        @Override
        public void getCurrentLocation(Location location) {
            // You will get location here if the GPS is enabled
            if(location != null) {
                Log.d("LOCATION", location.getLatitude() + ", " + location.getLongitude());
            }
        }
    };

在这里,当我们给予一个定位权限,然后等待几秒钟后,GPS将需要时间来稳定定位,之后我们将得到实际值lat和long。

Setting -> GPS Enable

后长值出现0.0的原因

相关内容

  • 没有找到相关文章

最新更新