Skip to content

Commit 676583b

Browse files
author
Vivek Maskara
authored
Merge pull request #558 from neslihanturan/nearbyMap
Nearby map
2 parents 516a583 + c906d6d commit 676583b

File tree

6 files changed

+176
-89
lines changed

6 files changed

+176
-89
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
5555
}
5656

5757
private void showMapView() {
58-
isMapViewActive = true;
59-
getSupportFragmentManager().beginTransaction()
60-
.replace(R.id.container, new NearbyMapFragment()).commit();
58+
if (!isMapViewActive) {
59+
getSupportFragmentManager().beginTransaction()
60+
.replace(R.id.container, new NearbyMapFragment()).commit();
61+
isMapViewActive = true;
62+
}
6163
}
6264

6365
@Override

app/src/main/java/fr/free/nrw/commons/nearby/NearbyAdapter.java

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import android.widget.ArrayAdapter;
88

99
import fr.free.nrw.commons.R;
10-
1110
import timber.log.Timber;
1211

1312
public class NearbyAdapter extends ArrayAdapter<Place> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package fr.free.nrw.commons.nearby;
2+
3+
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
4+
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
5+
6+
import android.content.Context;
7+
import android.content.SharedPreferences;
8+
import android.preference.PreferenceManager;
9+
10+
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
11+
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
12+
13+
import fr.free.nrw.commons.location.LatLng;
14+
15+
import java.util.ArrayList;
16+
import java.util.Collections;
17+
import java.util.Comparator;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Locale;
21+
import java.util.Map;
22+
23+
import timber.log.Timber;
24+
25+
26+
public class NearbyController {
27+
private static final int MAX_RESULTS = 1000;
28+
29+
private static List<Place> loadAttractionsFromLocation(LatLng curLatLng, Context context) {
30+
Timber.d("Loading attractions near %s", curLatLng);
31+
if (curLatLng == null) {
32+
return Collections.emptyList();
33+
}
34+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
35+
List<Place> places = prefs.getBoolean("useWikidata", true)
36+
? NearbyPlaces.getInstance().getFromWikidataQuery(
37+
curLatLng, Locale.getDefault().getLanguage())
38+
: NearbyPlaces.getInstance().getFromWikiNeedsPictures();
39+
if (curLatLng != null) {
40+
Timber.d("Sorting places by distance...");
41+
final Map<Place, Double> distances = new HashMap<>();
42+
for (Place place: places) {
43+
distances.put(place, computeDistanceBetween(place.location, curLatLng));
44+
}
45+
Collections.sort(places,
46+
new Comparator<Place>() {
47+
@Override
48+
public int compare(Place lhs, Place rhs) {
49+
double lhsDistance = distances.get(lhs);
50+
double rhsDistance = distances.get(rhs);
51+
return (int) (lhsDistance - rhsDistance);
52+
}
53+
}
54+
);
55+
}
56+
return places;
57+
}
58+
59+
/**
60+
* Loads attractions from location for list view, we need to return Place data type.
61+
* @param curLatLng users current location
62+
* @param context current activity
63+
* @return Place list that holds nearby places
64+
*/
65+
66+
public static List<Place> loadAttractionsFromLocationToPlaces(LatLng curLatLng,
67+
Context context) {
68+
69+
List<Place> places = loadAttractionsFromLocation(curLatLng,context);
70+
places = places.subList(0, Math.min(places.size(), MAX_RESULTS));
71+
for (Place place: places) {
72+
String distance = formatDistanceBetween(curLatLng, place.location);
73+
place.setDistance(distance);
74+
}
75+
return places;
76+
}
77+
78+
/**
79+
*Loads attractions from location for map view, we need to return BaseMarkerOption data type.
80+
* @param curLatLng users current location
81+
* @param context the activity
82+
* @return BaseMarkerOprions list that holds nearby places
83+
*/
84+
public static List<BaseMarkerOptions> loadAttractionsFromLocationToBaseMarkerOptions(
85+
LatLng curLatLng,
86+
Context context) {
87+
List<BaseMarkerOptions> baseMarkerOptionses = new ArrayList<>();
88+
List<Place> places = loadAttractionsFromLocation(curLatLng,context);
89+
places = places.subList(0, Math.min(places.size(), MAX_RESULTS));
90+
for (Place place: places) {
91+
String distance = formatDistanceBetween(curLatLng, place.location);
92+
place.setDistance(distance);
93+
baseMarkerOptionses.add(new MarkerOptions()
94+
.position(new com.mapbox.mapboxsdk.geometry
95+
.LatLng(place.location.latitude,place.location.longitude))
96+
.title(place.name)
97+
.snippet(place.description));
98+
}
99+
return baseMarkerOptionses;
100+
}
101+
}

app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java

+6-52
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,27 @@
11
package fr.free.nrw.commons.nearby;
22

3-
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
4-
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
5-
63
import android.content.Intent;
7-
import android.content.SharedPreferences;
84
import android.net.Uri;
95
import android.os.AsyncTask;
106
import android.os.Bundle;
11-
import android.preference.PreferenceManager;
127
import android.support.v4.app.ListFragment;
138
import android.view.LayoutInflater;
149
import android.view.View;
1510
import android.view.ViewGroup;
1611
import android.widget.ListView;
1712
import android.widget.ProgressBar;
1813

14+
import java.util.List;
15+
1916
import butterknife.BindView;
2017
import butterknife.ButterKnife;
2118
import butterknife.OnItemClick;
2219
import fr.free.nrw.commons.R;
2320
import fr.free.nrw.commons.location.LatLng;
24-
25-
import java.util.Collections;
26-
import java.util.Comparator;
27-
import java.util.HashMap;
28-
import java.util.List;
29-
import java.util.Locale;
30-
import java.util.Map;
31-
3221
import timber.log.Timber;
3322

3423
public class NearbyListFragment extends ListFragment {
3524

36-
private static final int MAX_RESULTS = 1000;
3725
private NearbyAsyncTask nearbyAsyncTask;
3826

3927
@BindView(R.id.listView) ListView listview;
@@ -124,8 +112,10 @@ protected void onProgressUpdate(Integer... values) {
124112

125113
@Override
126114
protected List<Place> doInBackground(Void... params) {
127-
return loadAttractionsFromLocation(
128-
((NearbyActivity)getActivity()).getLocationManager().getLatestLocation()
115+
return NearbyController.loadAttractionsFromLocationToPlaces(
116+
((NearbyActivity)getActivity())
117+
.getLocationManager()
118+
.getLatestLocation(), getActivity()
129119
);
130120
}
131121

@@ -164,40 +154,4 @@ void onItemClicked(int position) {
164154
startActivity(mapIntent);
165155
}
166156
}
167-
168-
private List<Place> loadAttractionsFromLocation(LatLng curLatLng) {
169-
Timber.d("Loading attractions near %s", curLatLng);
170-
if (curLatLng == null) {
171-
return Collections.emptyList();
172-
}
173-
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
174-
List<Place> places = prefs.getBoolean("useWikidata", true)
175-
? NearbyPlaces.getInstance().getFromWikidataQuery(
176-
curLatLng, Locale.getDefault().getLanguage())
177-
: NearbyPlaces.getInstance().getFromWikiNeedsPictures();
178-
if (curLatLng != null) {
179-
Timber.d("Sorting places by distance...");
180-
final Map<Place, Double> distances = new HashMap<>();
181-
for (Place place: places) {
182-
distances.put(place, computeDistanceBetween(place.location, curLatLng));
183-
}
184-
Collections.sort(places,
185-
new Comparator<Place>() {
186-
@Override
187-
public int compare(Place lhs, Place rhs) {
188-
double lhsDistance = distances.get(lhs);
189-
double rhsDistance = distances.get(rhs);
190-
return (int) (lhsDistance - rhsDistance);
191-
}
192-
}
193-
);
194-
}
195-
196-
places = places.subList(0, Math.min(places.size(), MAX_RESULTS));
197-
for (Place place: places) {
198-
String distance = formatDistanceBetween(curLatLng, place.location);
199-
place.setDistance(distance);
200-
}
201-
return places;
202-
}
203157
}

app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java

+64-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
package fr.free.nrw.commons.nearby;
22

3+
import android.os.AsyncTask;
34
import android.os.Bundle;
5+
import android.support.annotation.Nullable;
46
import android.view.LayoutInflater;
57
import android.view.View;
68
import android.view.ViewGroup;
79

8-
import butterknife.BindView;
9-
import butterknife.ButterKnife;
10-
1110
import com.mapbox.mapboxsdk.Mapbox;
11+
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
12+
import com.mapbox.mapboxsdk.camera.CameraPosition;
13+
import com.mapbox.mapboxsdk.constants.Style;
14+
import com.mapbox.mapboxsdk.geometry.LatLng;
1215
import com.mapbox.mapboxsdk.maps.MapView;
1316
import com.mapbox.mapboxsdk.maps.MapboxMap;
17+
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
1418
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
1519
import com.mapbox.services.android.telemetry.MapboxTelemetry;
1620

21+
import java.util.List;
22+
1723
import fr.free.nrw.commons.R;
1824

1925
public class NearbyMapFragment extends android.support.v4.app.Fragment {
20-
@BindView(R.id.mapview) MapView mapView;
26+
private NearbyAsyncTask nearbyAsyncTask;
27+
private fr.free.nrw.commons.location.LatLng currentLocation;
28+
private MapView mapView;
2129

2230
public NearbyMapFragment() {
2331

@@ -26,7 +34,7 @@ public NearbyMapFragment() {
2634
@Override
2735
public void onCreate(Bundle savedInstanceState) {
2836
super.onCreate(savedInstanceState);
29-
37+
currentLocation = ((NearbyActivity)getActivity()).getLocationManager().getLatestLocation();
3038
Mapbox.getInstance(getActivity(),
3139
getString(R.string.mapbox_commons_app_token));
3240
MapboxTelemetry.getInstance().setTelemetryEnabled(false);
@@ -35,21 +43,25 @@ public void onCreate(Bundle savedInstanceState) {
3543
@Override
3644
public View onCreateView(LayoutInflater inflater, ViewGroup container,
3745
Bundle savedInstanceState) {
38-
View view = inflater.inflate(R.layout.fragment_nearby_map, container, false);
39-
ButterKnife.bind(this, view);
40-
46+
MapboxMapOptions options = new MapboxMapOptions()
47+
.styleUrl(Style.OUTDOORS)
48+
.camera(new CameraPosition.Builder()
49+
.target(new LatLng(currentLocation.latitude, currentLocation.longitude))
50+
.zoom(11)
51+
.build());
52+
53+
// create map
54+
mapView = new MapView(getActivity(), options);
4155
mapView.onCreate(savedInstanceState);
42-
4356
setHasOptionsMenu(false);
57+
return mapView;
58+
}
4459

45-
mapView.getMapAsync(new OnMapReadyCallback() {
46-
@Override
47-
public void onMapReady(MapboxMap mapboxMap) {
48-
49-
}
50-
});
51-
52-
return view;
60+
@Override
61+
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
62+
super.onViewCreated(view, savedInstanceState);
63+
nearbyAsyncTask = new NearbyAsyncTask();
64+
nearbyAsyncTask.execute();
5365
}
5466

5567
@Override
@@ -81,4 +93,39 @@ public void onDestroyView() {
8193
mapView.onDestroy();
8294
super.onDestroyView();
8395
}
96+
97+
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<BaseMarkerOptions>> {
98+
99+
@Override
100+
protected void onPreExecute() {
101+
super.onPreExecute();
102+
}
103+
104+
@Override
105+
protected void onProgressUpdate(Integer... values) {
106+
super.onProgressUpdate(values);
107+
}
108+
109+
@Override
110+
protected List<BaseMarkerOptions> doInBackground(Void... params) {
111+
return NearbyController
112+
.loadAttractionsFromLocationToBaseMarkerOptions(currentLocation, getActivity()
113+
);
114+
}
115+
116+
@Override
117+
protected void onPostExecute(final List<BaseMarkerOptions> baseMarkerOptionses) {
118+
super.onPostExecute(baseMarkerOptionses);
119+
120+
if (isCancelled()) {
121+
return;
122+
}
123+
mapView.getMapAsync(new OnMapReadyCallback() {
124+
@Override
125+
public void onMapReady(MapboxMap mapboxMap) {
126+
mapboxMap.addMarkers(baseMarkerOptionses);
127+
}
128+
});
129+
}
130+
}
84131
}

app/src/main/res/layout/fragment_nearby_map.xml

-16
This file was deleted.

0 commit comments

Comments
 (0)