我正在Google地图上绘制心形折线,根据半径(1-100米)确定大小。一旦绘制完成,用户需要沿着心形边界行走,并从起点到终点结束(从底部开始行走,然后向左,向右,最后再次到达底部)。
我已经能够绘制心形并获取360个点(latlng)。以下是我的代码,它将绘制心形和图像。
private void initPath() {
path = new PolylineOptions();
path.color(ContextCompat.getColor(mActivity,R.color.heart_green));
path.width(25);
// offset to bottom
double offsetY = getY(Math.toRadians(180));
for (int angle = 0; angle <= 360; angle++) {
double t = Math.toRadians(angle);
double x = getX(t);
double y = getY(t) - offsetY;
//Log.d(TAG, "angle = " + angle + "(x = " + x + ",y= " + y + ")");
//convert x,y to lat lng
LatLng latLng = getLatLng(x, y, center);
path.add(latLng);
heart360Points.add(latLng);
}
}
private double getX(double t) {
return radius * 16 * Math.sin(t) * Math.sin(t) * Math.sin(t) / HEART_RATIO;
}
private double getY(double t) {
return radius * (13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t)) / HEART_RATIO;
}
private LatLng getLatLng(double dx, double dy, LatLng centerHeart) {
return new LatLng(centerHeart.latitude + Math.toDegrees(dy / EARTH_RADIUS),
centerHeart.longitude + Math.toDegrees(dx / EARTH_RADIUS) / Math.cos(Math.toRadians(centerHeart.latitude)));
}
但是每当我试图走在心形边缘时,GPS位置会出现大量波动,所以我从未能够完成在心形上的行走。我目前每秒请求一次位置。
以下是我的位置代码。
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 2000 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.love_lead_perform);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mApiClient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mApiClient.connect();
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mSettingsClient = LocationServices.getSettingsClient(this);
createLocationCallback();
createLocationRequest();
createLocationSettingsRequest();
}
private void createLocationSettingsRequest() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();
}
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
我不明白为什么GPS定位会波动太大。即使我站在原地,也会得到不同的GPS定位。
如何获取准确的GPS定位?