gpt4 book ai didi

android - 无法在 Android 中获取新的 GPS 位置

转载 作者:行者123 更新时间:2023-11-30 03:40:18 25 4
gpt4 key购买 nike

我正在开发一款应用程序,该应用程序使用带 GPS 的 Google Map V2。该代码显示了获取用户位置的纬度和经度的部分。

map.setMyLocationEnabled(true); //Locate the user's current location

LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria crit = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
LatLng locationOfUser = new LatLng(location.getLatitude(),location.getLongitude());

该应用基本上使用 locationOfUser 中的值,并且可以绘制一条线到用户选择的标记。然而,当我从不同的位置尝试应用程序时,这有效,指示用户当前位置的蓝色标记发生变化,但 locationOfUser 中的值没有变化,并且从最后一个位置而不是当前位置绘制线。

谢谢。

最佳答案

您需要requestLocationUpdate

service.requestLocationUpdates(provider, updateInterval, updateDistance, 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)
{
LatLng locationOfUser = new LatLng(location.getLatitude(),location.getLongitude());
}
});

其中 updateInterval 是期望的最小时间间隔,updateDistance 是期望的最小距离间隔。例如,您可以设置 updateInterval = 1000 和 updateDistance = 1。

关于android - 无法在 Android 中获取新的 GPS 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15851810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com