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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在地图中显示当前位置时获取纬度和经度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

答案

试试这种方式

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) {
    }
}
另一答案

还检查这是其中一个原因。转到地图默认应用程序并允许用户允许经度和经度选项。在我们的应用程序中当GPS信号闪烁时,当它停止时我们将获得经度和纬度。这对我有用


编辑1

  1. 打开Goog​​le地图应用并点击地图上的按钮以获取当前位置。
  2. 如果您允许使用Google地图权限,则只有其他应用权限才有效。 (我觉得这是个大错误)
  3. 现在转到您的应用程序并尝试获取当前位置,它将工作。

这很难看,但我们已按照这些步骤在我们的应用中获取当前位置。

另一答案

这就是你想要的......!

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);                                                         
                            }
                        }

                    }

                });
    }
另一答案

在这里,当我们给出位置许可然后等待几秒后,GPS将花费时间来稳定位置,之后我们将获得lat和long的实际值。

设置 - > GPS启用

Reason for lat long value comes 0.0

另一答案

custom location manager.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
                    

以上是关于在地图中显示当前位置时获取纬度和经度0.0的主要内容,如果未能解决你的问题,请参考以下文章

Android:如何获取经度和纬度的当前位置?

windows phone 8 - 如何在地图上显示经度和纬度坐标(当前位置)

使用gps从纬度和经度获取当前位置

在不显示提示信息的情况下,如何获取用户当前位置的经纬度

如何在android的地图中获取当前位置?

手动设置纬度和经度后谷歌地图不显示(缩放到)当前位置[重复]