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

tej*_*jas 6 android google-maps map geolocation userlocation

我使用以下代码来获取我当前的位置.但我面临的问题是,纬度和经度总是返回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);


              }
       } ........ }
Run Code Online (Sandbox Code Playgroud)

你们中的任何人都知道为什么吗?

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

Pra*_*eep 7

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


编辑1

  1. 打开Goog​​le地图应用并点击地图上的按钮以获取当前位置.

  2. 如果您允许使用Google地图权限,则只有其他应用权限才有效.(我觉得这是个大错误)

  3. 现在转到您的应用程序并尝试获取当前位置,它将工作.

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


MKJ*_*ekh 4

试试这个方法

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);
Run Code Online (Sandbox Code Playgroud)

在 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) {
    }
}
Run Code Online (Sandbox Code Playgroud)