我希望在用户导航的基础上根据他的方向/标题和他当前的片段(当前位置到下一个)旋转地图。我有在地图上绘制的路线,我正在使用方法current.bearingTo(destination)
计算两点之间的方位。我已关注this链接,但结果并非如我所料。我想要实现的是,当用户启动路线导航应用程序应该采取当前段(起点和终点)并计算两点之间的方位,也从传感器获得航向/方向并旋转地图。然后,当用户轮流(左或右)时,在段完成时,再次计算新轴承(下一段的开始/结束点)和前进方向并相应地旋转地图。这是我的onSensorChanged方法;
@Override
public void onSensorChanged( SensorEvent event ) {
Location LocationObj = LocationService.mLocation;
// If we don't have a Location, we break out
if ( LocationObj == null ) return;
float azimuth = event.values[0];
float baseAzimuth = azimuth;
GeomagneticField geoField = new GeomagneticField( Double
.valueOf( LocationObj.getLatitude() ).floatValue(), Double
.valueOf( LocationObj.getLongitude() ).floatValue(),
Double.valueOf( LocationObj.getAltitude() ).floatValue(),
System.currentTimeMillis() );
azimuth -= geoField.getDeclination(); // converts magnetic north into true north
Location destinationObj = new Location("");
destinationObj.setLongitude(33.521398);
destinationObj.setLatitude(73.090826);
// Store the bearingTo in the bearTo variable
float bearTo = LocationObj.bearingTo( destinationObj );
// If the bearTo is smaller than 0, add 360 to get the rotation clockwise.
if (bearTo < 0) {
bearTo = bearTo + 360;
}
//This is where we choose to point it
float direction = bearTo - azimuth;
// If the direction is smaller than 0, add 360 to get the rotation clockwise.
if (direction < 0) {
direction = direction + 360;
}
//Log.i(TAG, "rotation sensor angle : " + direction);
//mapController.setMapRotation((float) Math.toRadians(direction));
//rotateImageView( arrow, R.drawable.arrow, direction );
//Set the field
String bearingText = "N";
if ( (360 >= baseAzimuth && baseAzimuth >= 337.5) || (0 <= baseAzimuth && baseAzimuth <= 22.5) ) bearingText = "N";
else if (baseAzimuth > 22.5 && baseAzimuth < 67.5) bearingText = "NE";
else if (baseAzimuth >= 67.5 && baseAzimuth <= 112.5) bearingText = "E";
else if (baseAzimuth > 112.5 && baseAzimuth < 157.5) bearingText = "SE";
else if (baseAzimuth >= 157.5 && baseAzimuth <= 202.5) bearingText = "S";
else if (baseAzimuth > 202.5 && baseAzimuth < 247.5) bearingText = "SW";
else if (baseAzimuth >= 247.5 && baseAzimuth <= 292.5) bearingText = "W";
else if (baseAzimuth > 292.5 && baseAzimuth < 337.5) bearingText = "NW";
else bearingText = "?";
//fieldBearing.setText(bearingText);
Log.i(TAG, "rotation sensor azimuth : " + bearingText);
}