gpt4 book ai didi

android - 不调用 OnLocationChanged

转载 作者:行者123 更新时间:2023-11-29 14:16:10 26 4
gpt4 key购买 nike

OnLocationChanged() 未被调用,但添加了 requestLocationUpdates,因此 map 未显示其当前位置。
map 始终显示其默认的模拟位置。请建议克服这个问题。我已经在 list 文件中添加了所有必要的权限。
注意:我不想使用 getLastKnownLocation 获取最后位置,而是想要当前位置。我等待设备修复,有时 GPS 需要比平时更多的时间,但仍然没有改善。我也带着这个设备走了将近 10 公里。 :-).
这是我使用的代码。

public class TestingmapActivity extends MapActivity implements LocationListener {

private MapController myMapController;
private MapView mapView;
private LocationManager myLocationManager;
private double m_dLati = 12.913469, m_dLongi = 77.609761;
String provider;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mapView = (MapView) findViewById(R.id.mapview);
myMapController = mapView.getController();
provider = LocationManager.GPS_PROVIDER;
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
myMapController.setZoom(14); // Zoon 1 is world view
myLocationManager.requestLocationUpdates(provider, 0, 0,this);
setMockLocation(m_dLati, m_dLongi, 500);
}

@Override
protected void onResume() {
startListening();
super.onResume();
}

@Override
protected void onPause() {
stopListening();
super.onResume();
}

@Override
protected void onStop() {
stopListening();
super.onStop();
}

private void startListening() {
if (myLocationManager != null) {
myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, this);
}
}

private void stopListening() {
if (myLocationManager != null)
myLocationManager.removeUpdates(this);
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

private void setMockLocation(double latitude, double longitude,
float accuracy) {
Log.v("", "provider name= " + provider);
myLocationManager.addTestProvider(provider, "requiresNetwork" == "",
"requiresSatellite" == "", "requiresCell" == "",
"hasMonetaryCost" == "", "supportsAltitude" == "",
"supportsSpeed" == "", "supportsBearing" == "",
android.location.Criteria.POWER_LOW,
android.location.Criteria.ACCURACY_FINE);
Location newLocation = new Location(provider);
newLocation.setLatitude(latitude);
newLocation.setLongitude(longitude);
newLocation.setAccuracy(accuracy);
Toast.makeText(
getApplicationContext(),
"Mock: New Latitude = " + newLocation.getLatitude()
+ " New Longitude = " + newLocation.getLongitude(),
Toast.LENGTH_LONG).show();
myLocationManager.setTestProviderEnabled(provider, true);
myLocationManager.setTestProviderStatus(provider,
LocationProvider.AVAILABLE, null, System.currentTimeMillis());
myLocationManager.setTestProviderLocation(provider, newLocation);
}

@Override
public void onLocationChanged(Location location) {
Log.v("LocationListener", "onLocationChanged " + location.getLatitude()
+ " , " + location.getLongitude());
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
myMapController.animateTo(point);
}

@Override
public void onProviderDisabled(String provider) {}

@Override
public void onProviderEnabled(String provider) {}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}

list 权限:

<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.LOCATION" />

谢谢,双联实验室

最佳答案

您需要在 setMockLocation 方法中覆盖提供者。请再次检查该方法。

关于android - 不调用 OnLocationChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135532/

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