Skip to content

Commit ef87a60

Browse files
committed
Use map center for nearby API
1 parent 7ef99c2 commit ef87a60

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "org.nitri.opentopo"
88
minSdkVersion 19
99
targetSdkVersion 28
10-
versionCode 17
11-
versionName "1.7.2"
10+
versionCode 18
11+
versionName "1.8"
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {

app/src/main/java/org/nitri/opentopo/MainActivity.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@
44
import android.content.ContentResolver;
55
import android.content.Intent;
66
import android.content.pm.PackageManager;
7-
import android.location.Location;
87
import android.net.Uri;
98
import android.os.Bundle;
10-
import androidx.annotation.NonNull;
11-
import androidx.core.app.ActivityCompat;
12-
import androidx.appcompat.app.AppCompatActivity;
139
import android.text.TextUtils;
1410
import android.util.Log;
1511
import android.view.MenuItem;
1612
import android.widget.Toast;
1713

14+
import androidx.annotation.NonNull;
15+
import androidx.appcompat.app.AppCompatActivity;
16+
import androidx.core.app.ActivityCompat;
17+
1818
import net.danlew.android.joda.JodaTimeAndroid;
1919

2020
import org.nitri.opentopo.nearby.entity.NearbyItem;
21+
import org.osmdroid.util.GeoPoint;
2122
import org.xmlpull.v1.XmlPullParserException;
2223

2324
import java.io.IOException;
@@ -127,8 +128,8 @@ public void addGpxDetailFragment() {
127128
}
128129

129130
@Override
130-
public void addNearbyFragment(Location location) {
131-
NearbyFragment gpxDetailFragment = NearbyFragment.newInstance(location.getLatitude(), location.getLongitude());
131+
public void addNearbyFragment(GeoPoint nearbyCenterPoint) {
132+
NearbyFragment gpxDetailFragment = NearbyFragment.newInstance(nearbyCenterPoint.getLatitude(), nearbyCenterPoint.getLongitude());
132133
getSupportFragmentManager().beginTransaction().addToBackStack(null)
133134
.replace(R.id.map_container, gpxDetailFragment, NEARBY_FRAGMENT_TAG)
134135
.commit();
@@ -268,7 +269,13 @@ public void showNearbyPlace(NearbyItem nearbyItem) {
268269
}
269270
}
270271

272+
@Override
271273
public NearbyItem getSelectedNearbyPlace() {
272274
return mSelectedNearbyPlace;
273275
}
276+
277+
@Override
278+
public void clearSelectedNearbyPlace() {
279+
mSelectedNearbyPlace = null;
280+
}
274281
}

app/src/main/java/org/nitri/opentopo/MapFragment.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public boolean onZoom(ZoomEvent event) {
125125
private TextView mCopyRightView;
126126
private int mOverlay = OverlayHelper.OVERLAY_NONE;
127127
private GeoPoint mMapCenterState;
128+
private int mLastNearbyAnimateToId;
128129

129130
public MapFragment() {
130131
// Required empty public constructor
@@ -458,7 +459,11 @@ public void setNearbyPlace() {
458459

459460
private void showNearbyPlace(NearbyItem nearbyPlace) {
460461
mOverlayHelper.setNearby(nearbyPlace);
461-
animateToLatLon(nearbyPlace.getLat(), nearbyPlace.getLon());
462+
if (nearbyPlace.getId() != mLastNearbyAnimateToId) {
463+
// Animate only once
464+
animateToLatLon(nearbyPlace.getLat(), nearbyPlace.getLon());
465+
mLastNearbyAnimateToId = nearbyPlace.getId();
466+
}
462467
}
463468

464469
@Override
@@ -514,12 +519,21 @@ public boolean onOptionsItemSelected(MenuItem item) {
514519
mListener.addGpxDetailFragment();
515520
return true;
516521
case R.id.action_nearby:
517-
if (mListener != null)
518-
if (mCurrentLocation != null) {
519-
mListener.addNearbyFragment(mCurrentLocation);
520-
} else {
522+
if (mListener != null) {
523+
mListener.clearSelectedNearbyPlace();
524+
GeoPoint nearbyCenter = null;
525+
if (mMapView != null) {
526+
nearbyCenter = (GeoPoint) mMapView.getMapCenter();
527+
mListener.addNearbyFragment(nearbyCenter);
528+
}
529+
if (nearbyCenter == null && mCurrentLocation != null) {
530+
nearbyCenter = new GeoPoint(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
531+
mListener.addNearbyFragment(nearbyCenter);
532+
}
533+
if (nearbyCenter == null) {
521534
Toast.makeText(getActivity(), R.string.location_unknown, Toast.LENGTH_SHORT).show();
522535
}
536+
}
523537
return true;
524538
case R.id.action_gpx_zoom:
525539
disableFollow();
@@ -682,7 +696,7 @@ public interface OnFragmentInteractionListener {
682696
/**
683697
* Present nearby items
684698
*/
685-
void addNearbyFragment(Location location);
699+
void addNearbyFragment(GeoPoint nearbyCenterPoint);
686700

687701
/**
688702
* Set up navigation arrow
@@ -695,6 +709,11 @@ public interface OnFragmentInteractionListener {
695709
* @return NearbyItem
696710
*/
697711
NearbyItem getSelectedNearbyPlace();
712+
713+
/**
714+
* Clear selected nearby place
715+
*/
716+
void clearSelectedNearbyPlace();
698717
}
699718

700719
}

app/src/main/java/org/nitri/opentopo/NearbyFragment.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package org.nitri.opentopo;
22

3-
import androidx.lifecycle.Observer;
4-
import androidx.lifecycle.ViewModelProviders;
53
import android.content.Context;
64
import android.content.Intent;
75
import android.net.Uri;
86
import android.os.Bundle;
9-
import androidx.annotation.NonNull;
10-
import androidx.fragment.app.Fragment;
11-
import androidx.recyclerview.widget.LinearLayoutManager;
12-
import androidx.recyclerview.widget.RecyclerView;
137
import android.view.LayoutInflater;
148
import android.view.Menu;
159
import android.view.MenuInflater;
1610
import android.view.View;
1711
import android.view.ViewGroup;
1812

13+
import androidx.annotation.NonNull;
14+
import androidx.fragment.app.Fragment;
15+
import androidx.lifecycle.Observer;
16+
import androidx.lifecycle.ViewModelProvider;
17+
import androidx.recyclerview.widget.LinearLayoutManager;
18+
import androidx.recyclerview.widget.RecyclerView;
19+
1920
import com.google.gson.Gson;
2021
import com.google.gson.GsonBuilder;
2122

@@ -93,7 +94,7 @@ public void onCreate(Bundle savedInstanceState) {
9394
mNearbyAdapter = new NearbyAdapter(mNearbyItems, this);
9495
mNearbyAdapter.setHasStableIds(true);
9596

96-
NearbyViewModel nearbyViewModel = ViewModelProviders.of(requireActivity()).get(NearbyViewModel.class);
97+
NearbyViewModel nearbyViewModel = new ViewModelProvider(requireActivity()).get(NearbyViewModel.class);
9798

9899
MediaWikiApi api = retrofit.create(MediaWikiApi.class);
99100
NearbyDao dao = NearbyDatabase.getDatabase(getActivity()).nearbyDao();

0 commit comments

Comments
 (0)