Skip to content

Commit

Permalink
feat(main): add settings for altitude
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCShou committed May 27, 2024
1 parent c2805c2 commit 004c078
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
16 changes: 16 additions & 0 deletions app/src/main/java/com/zcshou/gogogo/FragmentSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
});
}

EditTextPreference pfAltitude = findPreference("setting_altitude");
if (pfAltitude != null) {
pfAltitude.setSummaryProvider((Preference.SummaryProvider<EditTextPreference>) preference -> getResources().getString(R.string.setting_current_value) + preference.getText());
pfAltitude.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
Selection.setSelection(editText.getText(), editText.length());
});
pfAltitude.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.toString().trim().length() == 0) {
GoUtils.DisplayToast(this.getContext(),getResources().getString(R.string.app_error_input_null));
return false;
}
return true;
});
}

SwitchPreferenceCompat pLog = findPreference("setting_log_off");
if (pLog != null) {
pLog.setOnPreferenceChangeListener((preference, newValue) -> {
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/zcshou/gogogo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public class MainActivity extends BaseActivity implements SensorEventListener {
/* 对外 */
public static final String LAT_MSG_ID = "LAT_VALUE";
public static final String LNG_MSG_ID = "LNG_VALUE";
public static final String ALT_MSG_ID = "ALT_VALUE";

public static final String POI_NAME = "POI_NAME";
public static final String POI_ADDRESS = "POI_ADDRESS";
Expand Down Expand Up @@ -1018,6 +1019,8 @@ private void startGoLocation() {
double[] latLng = MapUtils.bd2wgs(mMarkLatLngMap.longitude, mMarkLatLngMap.latitude);
serviceGoIntent.putExtra(LNG_MSG_ID, latLng[0]);
serviceGoIntent.putExtra(LAT_MSG_ID, latLng[1]);
double alt = Double.parseDouble(sharedPreferences.getString("setting_altitude", "55.0"));
serviceGoIntent.putExtra(ALT_MSG_ID, alt);

startForegroundService(serviceGoIntent);
XLog.d("startForegroundService: ServiceGo");
Expand Down Expand Up @@ -1057,7 +1060,8 @@ private void doGoLocation(View v) {
mButtonStart.setImageResource(R.drawable.ic_position);
} else {
double[] latLng = MapUtils.bd2wgs(mMarkLatLngMap.longitude, mMarkLatLngMap.latitude);
mServiceBinder.setPosition(latLng[0], latLng[1]);
double alt = Double.parseDouble(sharedPreferences.getString("setting_altitude", "55.0"));
mServiceBinder.setPosition(latLng[0], latLng[1], alt);
Snackbar.make(v, "已传送到新位置", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();

Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/zcshou/joystick/JoyStick.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class JoyStick extends View {
private GoUtils.TimeCount mTimer;
private boolean isMove;
private double mSpeed = 1.2; /* 默认的速度,单位 m/s */
private double mAltitude = 55.0;
private double mAngle = 0;
private double mR = 0;
private double disLng = 0;
Expand Down Expand Up @@ -149,9 +150,10 @@ public JoyStick(Context context, AttributeSet attrs) {
}
}

public void setCurrentPosition(double lng, double lat) {
public void setCurrentPosition(double lng, double lat, double alt) {
double[] lngLat = MapUtils.wgs2bd09(lng, lat);
mCurMapLngLat = new LatLng(lngLat[1], lngLat[0]);
mAltitude = alt;

resetBaiduMap();
}
Expand Down Expand Up @@ -425,7 +427,7 @@ public boolean onTouch(View view, MotionEvent event) {

public interface JoyStickClickListener {
void onMoveInfo(double speed, double disLng, double disLat, double angle);
void onPositionInfo(double lng, double lat);
void onPositionInfo(double lng, double lat, double alt);
}


Expand Down Expand Up @@ -542,7 +544,7 @@ public boolean onQueryTextChange(String newText) {
mMarkMapLngLat = null;

double[] lngLat = MapUtils.bd2wgs(mCurMapLngLat.longitude, mCurMapLngLat.latitude);
mListener.onPositionInfo(lngLat[0], lngLat[1]);
mListener.onPositionInfo(lngLat[0], lngLat[1], mAltitude);

resetBaiduMap();

Expand Down Expand Up @@ -727,7 +729,7 @@ public boolean onQueryTextChange(String newText) {// 当搜索内容改变时触
String wgs84Longitude = wgs84latLngStr[0].substring(wgs84latLngStr[0].indexOf(':') + 1);
String wgs84Latitude = wgs84latLngStr[1].substring(wgs84latLngStr[1].indexOf(':') + 1);

mListener.onPositionInfo(Double.parseDouble(wgs84Longitude), Double.parseDouble(wgs84Latitude));
mListener.onPositionInfo(Double.parseDouble(wgs84Longitude), Double.parseDouble(wgs84Latitude), mAltitude);

// 注意这里在选择位置之后需要刷新地图
String bdLatLng = (String) ((TextView) view.findViewById(R.id.BDLatLngText)).getText();
Expand Down
17 changes: 11 additions & 6 deletions app/src/main/java/com/zcshou/service/ServiceGo.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ public class ServiceGo extends Service {
// 定位相关变量
public static final double DEFAULT_LAT = 36.667662;
public static final double DEFAULT_LNG = 117.027707;
public static final double DEFAULT_ALT = 55.0D;
public static final float DEFAULT_BEA = 0.0F;
private double mCurLat = DEFAULT_LAT;
private double mCurLng = DEFAULT_LNG;
private double mCurAlt = DEFAULT_ALT;
private float mCurBea = DEFAULT_BEA;
private double mSpeed = 1.2; /* 默认的速度,单位 m/s */
private static final int HANDLER_MSG_ID = 0;
Expand Down Expand Up @@ -86,8 +88,9 @@ public void onCreate() {
public int onStartCommand(Intent intent, int flags, int startId) {
mCurLng = intent.getDoubleExtra(MainActivity.LNG_MSG_ID, DEFAULT_LNG);
mCurLat = intent.getDoubleExtra(MainActivity.LAT_MSG_ID, DEFAULT_LAT);
mCurAlt = intent.getDoubleExtra(MainActivity.ALT_MSG_ID, DEFAULT_ALT);

mJoyStick.setCurrentPosition(mCurLng, mCurLat);
mJoyStick.setCurrentPosition(mCurLng, mCurLat, mCurAlt);

return super.onStartCommand(intent, flags, startId);
}
Expand Down Expand Up @@ -160,9 +163,10 @@ public void onMoveInfo(double speed, double disLng, double disLat, double angle)
}

@Override
public void onPositionInfo(double lng, double lat) {
public void onPositionInfo(double lng, double lat, double alt) {
mCurLng = lng;
mCurLat = lat;
mCurAlt = alt;
}
});
mJoyStick.show();
Expand Down Expand Up @@ -233,7 +237,7 @@ private void setLocationGPS() {
// 尽可能模拟真实的 GPS 数据
Location loc = new Location(LocationManager.GPS_PROVIDER);
loc.setAccuracy(Criteria.ACCURACY_FINE); // 设定此位置的估计水平精度,以米为单位。
loc.setAltitude(55.0D); // 设置高度,在 WGS 84 参考坐标系中的米
loc.setAltitude(mCurAlt); // 设置高度,在 WGS 84 参考坐标系中的米
loc.setBearing(mCurBea); // 方向(度)
loc.setLatitude(mCurLat); // 纬度(度)
loc.setLongitude(mCurLng); // 经度(度)
Expand Down Expand Up @@ -288,7 +292,7 @@ private void setLocationNetwork() {
// 尽可能模拟真实的 NETWORK 数据
Location loc = new Location(LocationManager.NETWORK_PROVIDER);
loc.setAccuracy(Criteria.ACCURACY_COARSE); // 设定此位置的估计水平精度,以米为单位。
loc.setAltitude(55.0D); // 设置高度,在 WGS 84 参考坐标系中的米
loc.setAltitude(mCurAlt); // 设置高度,在 WGS 84 参考坐标系中的米
loc.setBearing(mCurBea); // 方向(度)
loc.setLatitude(mCurLat); // 纬度(度)
loc.setLongitude(mCurLng); // 经度(度)
Expand Down Expand Up @@ -319,12 +323,13 @@ public void onReceive(Context context, Intent intent) {
}

public class ServiceGoBinder extends Binder {
public void setPosition(double lng, double lat) {
public void setPosition(double lng, double lat, double alt) {
mLocHandler.removeMessages(HANDLER_MSG_ID);
mCurLng = lng;
mCurLat = lat;
mCurAlt = alt;
mLocHandler.sendEmptyMessage(HANDLER_MSG_ID);
mJoyStick.setCurrentPosition(mCurLng, mCurLat);
mJoyStick.setCurrentPosition(mCurLng, mCurLat, mCurAlt);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
<string name="setting_run_default">3.6</string>
<string name="setting_bike">驾驶速度(米/秒)</string>
<string name="setting_bike_default">10.0</string>
<string name="setting_altitude">海拔高度(米)</string>
<string name="setting_altitude_default">55.0</string>
<string name="setting_group_log">记录</string>
<string name="setting_log_off">关闭日志</string>
<string name="setting_pos_history">历史记录有效期(天)</string>
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/preferences_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
app:defaultValue="@string/setting_bike_default"
app:summary="@string/setting_bike_default"
app:iconSpaceReserved="false"/>

<EditTextPreference
app:key="setting_altitude"
app:title="@string/setting_altitude"
app:defaultValue="@string/setting_altitude_default"
app:summary="@string/setting_altitude_default"
app:iconSpaceReserved="false"/>

</PreferenceCategory>

<PreferenceCategory
Expand Down

0 comments on commit 004c078

Please sign in to comment.