gpt4 book ai didi

java - 使用折线选项在 map 上发生位置更改故障

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:02 26 4
gpt4 key购买 nike

我的应用程序面临一些问题,特别是在我的谷歌地图上绘制路线图/绘制问题。我在我家周围进行了测试路线,发现 GPS 提供商不如 runtastic 或 endemondo 等类似应用程序准确。

有时,位置监听器会在我的 map 上做出难以理解的更改,然后折线会在 map 上靠近我的位置绘制任何线条,即使 GPS 信号完美。
有时,它就是不起作用。它不监听位置变化。

任何人都可以向我解释一下(比如我五)其他健身应用程序如何获取其当前位置和绘制路线到谷歌地图上?谢谢!

// map fragment

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.fragment1)).getMap();
map.setMyLocationEnabled(true);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);

//Permission gain

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSION_ACCESS_COURSE_LOCATION);
return;
}
isLocationEnabled(getApplicationContext());

Location myLocation = locationManager.getLastKnownLocation(provider);
if (myLocation != null) {
latitude = myLocation.getLatitude();
longitude = myLocation.getLongitude();
float zoom = (float) 17.0;
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), zoom));
Log.e("TAG", "GPS is on");
Toast.makeText(MainActivity.this, "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();
} else {
locationManager.requestLocationUpdates(provider, 5000, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, this);

}

public void onLocationChanged(Location mylocation) {

if (lastLocationloc == null) {
lastLocationloc = mylocation;
}
LatLng lastLatLng = locationToLatLng(lastLocationloc);
LatLng thisLatLng = locationToLatLng(mylocation);
map.addPolyline(new PolylineOptions().add(lastLatLng).add(thisLatLng).width(6).color(Color.RED));
lastLocationloc = mylocation;
Toast.makeText(MainActivity.this, "!!!!Location CHANGE!!!!", Toast.LENGTH_SHORT).show();

}

public static LatLng locationToLatLng(Location loc) {
if (loc != null)
return new LatLng(loc.getLatitude(), loc.getLongitude());
return null;
}


For example, I want to plot it like this:


But my application works its own way..

最佳答案

首先,您需要考虑接收到的位置的准确性。您可以使用 Location.getAccuracy() 方法 ( documentation ) 获取准确度。精度以米为单位,因此越低越好:

if (location.getAccuracy() < MINIMUM_ACCURACY) {
// Add the new location to your polyline
}

例如,您可以将 MINIMUM_ACCURACY 设置为 10 米。

另一方面,仅当新位置距上次添加位置的给定距离远时,您才可能需要向折线添加新位置。举个例子:

private static final float MINIMUM_ACCURACY = 10;
private static final float MINIMUM_DISTANCE_BETWEEN_POINTS = 20;
private Location lastLocationloc;

// ...

public void onLocationChanged(Location mylocation) {
if (mylocation.getAccuracy() < MINIMUM_ACCURACY) {
if (lastLocationloc == null || lastLocationloc.distanceTo(mylocation) > MINIMUM_DISTANCE_BETWEEN_POINTS) {
// Add the new location to your polyline

lastLocationloc = mylocation;
}
}
}

关于java - 使用折线选项在 map 上发生位置更改故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37225838/

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