【问题标题】:get latitude and longitude in Android without show the map [duplicate]在不显示地图的情况下在Android中获取纬度和经度[重复]
【发布时间】:2013-12-15 23:07:30
【问题描述】:

有没有办法在不显示地图的情况下获取信息? 我想在与其他活动一起工作期间获得我的位置的坐标。 tnx 很多。

【问题讨论】:

  • 是的,您不必使用地图来获取经度和纬度
  • 与anwser stackoverflow.com/questions/2227292/…的问题相同
  • 上面提到的链接给出了最好的答案。只需根据您的要求添加即可。您必须选择多少分钟一次,您需要位置信息。原因是听众消耗设备电池更多。因此,选择一个适当的方案。 span>

标签: java android google-maps android-activity google-maps-api-2


【解决方案1】:

通过使用LocationManager,您可以获得纬度和纬度:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

getLastKnownLocation() 的调用不会阻塞——这意味着如果当前没有可用的位置,它将返回null——所以你可能想看看将LocationListener 传递给requestLocationUpdates() method,而不是,这将为您提供位置的异步更新。

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    }
}

lm.requestLocationUpdates(LocationManager.GPS, 2000, 10, locationListener);

如果您想使用 GPS,您需要将ACCESS_FINE_LOCATION permission 提供给您的应用程序。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

您可能还想在 GPS 不可用时添加ACCESS_COARSE_LOCATION permission,并使用getBestProvider() method 选择您的位置提供商。

【讨论】:

    【解决方案2】:

    使用此代码能够获取纬度和经度值并制作 确保在使用应用程序之前,您已从清单中获得访问位置的权限

    if(isGPS) {
        Geocoder gc = new Geocoder(getApplicationContext(), Locale.getDefault());
            try 
                  {
                    double lat=location.getLatitude();
                double lng=location.getLongitude(); 
                            Toast.makeText(getApplicationContext(),lat+"and"+lng, Toast.LENGTH_LONG).show();  
                  }
                  catch (Exception e)
                  {
                      e.printStackTrace();
                  }
            }
                    else
                    {
                display("GPS not Enabled");
            }
    Manifest permission
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

    【讨论】:

      【解决方案3】:

      如果你想在没有互联网的情况下获得纬度,请使用它

       LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
              Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
              latitude = location.getLongitude();
              longitude = location.getLatitude();
      

      【讨论】:

        猜你喜欢
        • 2011-11-27
        • 2017-03-28
        • 1970-01-01
        • 1970-01-01
        • 2012-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        相关资源
        最近更新 更多