Skip to content

Commit

Permalink
feat(history): optimize the logic of storing history
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCShou committed Mar 30, 2024
1 parent a9a1c50 commit 50cc3d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
5 changes: 3 additions & 2 deletions app/src/main/java/com/zcshou/gogogo/HistoryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,15 @@ private void initRecordListView() {
mRecordListView.setOnItemClickListener((adapterView, view, i, l) -> {
String bd09Longitude;
String bd09Latitude;
//bd09坐标
String name;
name = (String) ((TextView) view.findViewById(R.id.LocationText)).getText();
String bd09LatLng = (String) ((TextView) view.findViewById(R.id.BDLatLngText)).getText();
bd09LatLng = bd09LatLng.substring(bd09LatLng.indexOf('[') + 1, bd09LatLng.indexOf(']'));
String[] latLngStr = bd09LatLng.split(" ");
bd09Longitude = latLngStr[0].substring(latLngStr[0].indexOf(':') + 1);
bd09Latitude = latLngStr[1].substring(latLngStr[1].indexOf(':') + 1);

if (!MainActivity.showLocation(bd09Longitude, bd09Latitude)) {
if (!MainActivity.showLocation(name, bd09Longitude, bd09Latitude)) {
GoUtils.DisplayToast(this, getResources().getString(R.string.history_error_location));
}
this.finish();
Expand Down
26 changes: 14 additions & 12 deletions app/src/main/java/com/zcshou/gogogo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public class MainActivity extends BaseActivity implements SensorEventListener {
private MapView mMapView;
private static BaiduMap mBaiduMap = null;
private static LatLng mMarkLatLngMap = new LatLng(36.547743718042415, 117.07018449827267); // 当前标记的地图点
private static String mMarkName = null;
private GeoCoder mGeoCoder;
private SensorManager mSensorManager;
private Sensor mSensorAccelerometer;
Expand Down Expand Up @@ -695,6 +696,7 @@ public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult)
if (reverseGeoCodeResult == null || reverseGeoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
XLog.i("逆地理位置失败!");
} else {
mMarkName = String.valueOf(reverseGeoCodeResult.getAddress());
poiLatitude.setText(String.valueOf(reverseGeoCodeResult.getLocation().latitude));
poiLongitude.setText(String.valueOf(reverseGeoCodeResult.getLocation().longitude));
poiAddress.setText(reverseGeoCodeResult.getAddress());
Expand Down Expand Up @@ -872,6 +874,7 @@ private void initMapButton() {
double[] bdLonLat = MapUtils.wgs2bd09(dialog_lat_double, dialog_lng_double);
mMarkLatLngMap = new LatLng(bdLonLat[1], bdLonLat[0]);
}
mMarkName = "手动输入的坐标";

markMap();

Expand Down Expand Up @@ -983,11 +986,12 @@ private void resetMap() {
// }

// 在地图上显示位置
public static boolean showLocation(String bd09Longitude, String bd09Latitude) {
public static boolean showLocation(String name, String bd09Longitude, String bd09Latitude) {
boolean ret = true;

try {
if (!bd09Longitude.isEmpty() && !bd09Latitude.isEmpty()) {
mMarkName = name;
mMarkLatLngMap = new LatLng(Double.parseDouble(bd09Latitude), Double.parseDouble(bd09Longitude));
MarkerOptions ooA = new MarkerOptions().position(mMarkLatLngMap).icon(mMapIndicator);
mBaiduMap.clear();
Expand Down Expand Up @@ -1159,7 +1163,7 @@ public void onFailure(@NonNull Call call, @NonNull IOException e) {
XLog.e("HTTP: HTTP GET FAILED");
//插表参数
ContentValues contentValues = new ContentValues();
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LOCATION, getResources().getString(R.string.history_location_default_name));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LOCATION, mMarkName);
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LONGITUDE_WGS84, String.valueOf(latLng[0]));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LATITUDE_WGS84, String.valueOf(latLng[1]));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_TIMESTAMP, System.currentTimeMillis() / 1000);
Expand Down Expand Up @@ -1189,31 +1193,29 @@ public void onResponse(@NonNull Call call, @NonNull Response response) throws IO
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_TIMESTAMP, System.currentTimeMillis() / 1000);
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LONGITUDE_CUSTOM, Double.toString(lng));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LATITUDE_CUSTOM, Double.toString(lat));

DataBaseHistoryLocation.saveHistoryLocation(mLocationHistoryDB, contentValues);
} else { //位置获取失败
//插表参数
} else {
ContentValues contentValues = new ContentValues();
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LOCATION, getResources().getString(R.string.history_location_default_name));
// contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LOCATION, getResources().getString(R.string.history_location_default_name));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LOCATION, mMarkName);
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LONGITUDE_WGS84, String.valueOf(latLng[0]));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LATITUDE_WGS84, String.valueOf(latLng[1]));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_TIMESTAMP, System.currentTimeMillis() / 1000);
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LONGITUDE_CUSTOM, Double.toString(lng));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LATITUDE_CUSTOM, Double.toString(lat));

DataBaseHistoryLocation.saveHistoryLocation(mLocationHistoryDB, contentValues);
}
} catch (JSONException e) {
XLog.e("JSON: resolve json error");
//插表参数
ContentValues contentValues = new ContentValues();
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LOCATION, getResources().getString(R.string.history_location_default_name));
// contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LOCATION, getResources().getString(R.string.history_location_default_name));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LOCATION, mMarkName);
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LONGITUDE_WGS84, String.valueOf(latLng[0]));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LATITUDE_WGS84, String.valueOf(latLng[1]));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_TIMESTAMP, System.currentTimeMillis() / 1000);
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LONGITUDE_CUSTOM, Double.toString(lng));
contentValues.put(DataBaseHistoryLocation.DB_COLUMN_LATITUDE_CUSTOM, Double.toString(lat));

DataBaseHistoryLocation.saveHistoryLocation(mLocationHistoryDB, contentValues);
}
}
Expand All @@ -1230,9 +1232,9 @@ private void initSearchView() {
mSearchList.setOnItemClickListener((parent, view, position, id) -> {
String lng = ((TextView) view.findViewById(R.id.poi_longitude)).getText().toString();
String lat = ((TextView) view.findViewById(R.id.poi_latitude)).getText().toString();
mMarkName = ((TextView) view.findViewById(R.id.poi_name)).getText().toString();
mMarkLatLngMap = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
MapStatusUpdate mapstatusupdate = MapStatusUpdateFactory.newLatLng(mMarkLatLngMap);
//对地图的中心点进行更新,
mBaiduMap.setMapStatus(mapstatusupdate);

markMap();
Expand All @@ -1243,7 +1245,7 @@ private void initSearchView() {
// mSearchList.setVisibility(View.GONE);
//搜索历史 插表参数
ContentValues contentValues = new ContentValues();
contentValues.put(DataBaseHistorySearch.DB_COLUMN_KEY, ((TextView) view.findViewById(R.id.poi_name)).getText().toString());
contentValues.put(DataBaseHistorySearch.DB_COLUMN_KEY, mMarkName);
contentValues.put(DataBaseHistorySearch.DB_COLUMN_DESCRIPTION, ((TextView) view.findViewById(R.id.poi_address)).getText().toString());
contentValues.put(DataBaseHistorySearch.DB_COLUMN_IS_LOCATION, DataBaseHistorySearch.DB_SEARCH_TYPE_RESULT);
contentValues.put(DataBaseHistorySearch.DB_COLUMN_LONGITUDE_CUSTOM, lng);
Expand All @@ -1267,7 +1269,7 @@ private void initSearchView() {
if (searchIsLoc.equals("1")) {
String lng = ((TextView) view.findViewById(R.id.search_longitude)).getText().toString();
String lat = ((TextView) view.findViewById(R.id.search_latitude)).getText().toString();
//对地图的中心点进行更新
mMarkName = ((TextView) view.findViewById(R.id.poi_name)).getText().toString();
mMarkLatLngMap = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
MapStatusUpdate mapstatusupdate = MapStatusUpdateFactory.newLatLng(mMarkLatLngMap);
mBaiduMap.setMapStatus(mapstatusupdate);
Expand Down

0 comments on commit 50cc3d2

Please sign in to comment.