Java 在Android中获取纬度和经度而不显示地图

Java 在Android中获取纬度和经度而不显示地图,java,android,google-maps,android-activity,google-maps-api-2,Java,Android,Google Maps,Android Activity,Google Maps Api 2,有没有不显示地图就获取信息的方法? 我想在工作和其他活动中得到我所在位置的坐标。 tnx很多。通过使用,您可以获得lat和lang: LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); double longitude = locati

有没有不显示地图就获取信息的方法? 我想在工作和其他活动中得到我所在位置的坐标。 tnx很多。

通过使用,您可以获得lat和lang:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
getLastKnownLocation()
的调用没有阻塞-这意味着如果当前没有可用的位置,它将返回
null
,因此您可能需要考虑将a传递给,这将为您提供位置的异步更新

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,您需要为您的应用程序提供

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

您可能还希望在GPS不可用时添加,并使用选择您的位置提供商

使用此代码可以获得纬度和经度值并 确定在使用应用程序之前,您已从清单获得访问位置的权限

if(isGPS){
Geocoder gc=新的地理编码器(getApplicationContext(),Locale.getDefault());
尝试
{
双纬度=location.getLatitude();
double lng=location.getLongitude();
Toast.makeText(getApplicationContext(),lat+”和“+lng,Toast.LENGTH_LONG).show();
}
捕获(例外e)
{
e、 printStackTrace();
}
}
其他的
{
显示(“GPS未启用”);
}
明示许可
如果你想在没有互联网的情况下获得lat-lon,请使用此选项


是的,你不必使用地图来获取经度和纬度。上面提到的WSER链接给出了最佳答案。根据您的要求,只需添加。你必须选择多少分钟一次,你需要的位置信息。原因是监听器会消耗更多的设备电池。因此,选择一个合适的场景。
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" />
 LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        latitude = location.getLongitude();
        longitude = location.getLatitude();