Nearby places displayed on the map

This commit is contained in:
Neslihan 2017-05-12 17:13:06 +03:00
parent d5f50e8626
commit c2e018ae9e
4 changed files with 146 additions and 60 deletions

View file

@ -0,0 +1,80 @@
package fr.free.nrw.commons.nearby;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import fr.free.nrw.commons.location.LatLng;
import timber.log.Timber;
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
public class NearbyController {
private static final int MAX_RESULTS = 1000;
private static List<Place> loadAttractionsFromLocation(LatLng curLatLng, Context context) {
Timber.d("Loading attractions near %s", curLatLng);
if (curLatLng == null) {
return Collections.emptyList();
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
List<Place> places = prefs.getBoolean("useWikidata", true)
? NearbyPlaces.getInstance().getFromWikidataQuery(
curLatLng, Locale.getDefault().getLanguage())
: NearbyPlaces.getInstance().getFromWikiNeedsPictures();
if (curLatLng != null) {
Timber.d("Sorting places by distance...");
final Map<Place, Double> distances = new HashMap<>();
for (Place place: places) {
distances.put(place, computeDistanceBetween(place.location, curLatLng));
}
Collections.sort(places,
new Comparator<Place>() {
@Override
public int compare(Place lhs, Place rhs) {
double lhsDistance = distances.get(lhs);
double rhsDistance = distances.get(rhs);
return (int) (lhsDistance - rhsDistance);
}
}
);
}
return places;
}
public static List<Place> loadAttractionsFromLocationToPlaces(LatLng curLatLng, Context context){
List<Place> places = loadAttractionsFromLocation(curLatLng,context);
places = places.subList(0, Math.min(places.size(), MAX_RESULTS));
for (Place place: places) {
String distance = formatDistanceBetween(curLatLng, place.location);
place.setDistance(distance);
}
return places;
}
public static List<BaseMarkerOptions> loadAttractionsFromLocationToBaseMarkerOptions(LatLng curLatLng, Context context){
List<BaseMarkerOptions> baseMarkerOptionses = new ArrayList<>();
List<Place> places = loadAttractionsFromLocation(curLatLng,context);
places = places.subList(0, Math.min(places.size(), MAX_RESULTS));
for (Place place: places) {
String distance = formatDistanceBetween(curLatLng, place.location);
place.setDistance(distance);
baseMarkerOptionses.add(new MarkerOptions()
.position(new com.mapbox.mapboxsdk.geometry.LatLng(place.location.latitude,place.location.longitude))
.title(place.name)
.snippet(place.description));
}
return baseMarkerOptionses;
}
}

View file

@ -1,14 +1,9 @@
package fr.free.nrw.commons.nearby;
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
@ -16,24 +11,17 @@ import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemClick;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import timber.log.Timber;
public class NearbyListFragment extends ListFragment {
private static final int MAX_RESULTS = 1000;
private NearbyAsyncTask nearbyAsyncTask;
@BindView(R.id.listView) ListView listview;
@ -124,8 +112,8 @@ public class NearbyListFragment extends ListFragment {
@Override
protected List<Place> doInBackground(Void... params) {
return loadAttractionsFromLocation(
((NearbyActivity)getActivity()).getLocationManager().getLatestLocation()
return NearbyController.loadAttractionsFromLocationToPlaces(
((NearbyActivity)getActivity()).getLocationManager().getLatestLocation(), getActivity()
);
}
@ -164,40 +152,4 @@ public class NearbyListFragment extends ListFragment {
startActivity(mapIntent);
}
}
private List<Place> loadAttractionsFromLocation(LatLng curLatLng) {
Timber.d("Loading attractions near %s", curLatLng);
if (curLatLng == null) {
return Collections.emptyList();
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
List<Place> places = prefs.getBoolean("useWikidata", true)
? NearbyPlaces.getInstance().getFromWikidataQuery(
curLatLng, Locale.getDefault().getLanguage())
: NearbyPlaces.getInstance().getFromWikiNeedsPictures();
if (curLatLng != null) {
Timber.d("Sorting places by distance...");
final Map<Place, Double> distances = new HashMap<>();
for (Place place: places) {
distances.put(place, computeDistanceBetween(place.location, curLatLng));
}
Collections.sort(places,
new Comparator<Place>() {
@Override
public int compare(Place lhs, Place rhs) {
double lhsDistance = distances.get(lhs);
double rhsDistance = distances.get(rhs);
return (int) (lhsDistance - rhsDistance);
}
}
);
}
places = places.subList(0, Math.min(places.size(), MAX_RESULTS));
for (Place place: places) {
String distance = formatDistanceBetween(curLatLng, place.location);
place.setDistance(distance);
}
return places;
}
}

View file

@ -1,23 +1,34 @@
package fr.free.nrw.commons.nearby;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.services.android.telemetry.MapboxTelemetry;
import java.util.List;
import fr.free.nrw.commons.R;
public class NearbyMapFragment extends android.support.v4.app.Fragment {
private NearbyAsyncTask nearbyAsyncTask;
@BindView(R.id.mapview) MapView mapView;
@BindView(R.id.progressBar)
ProgressBar progressBar;
public NearbyMapFragment() {
@ -41,17 +52,16 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
mapView.onCreate(savedInstanceState);
setHasOptionsMenu(false);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
}
});
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
nearbyAsyncTask = new NearbyAsyncTask();
nearbyAsyncTask.execute();
}
@Override
public void onStart() {
mapView.onStart();
@ -81,4 +91,41 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
mapView.onDestroy();
super.onDestroyView();
}
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<BaseMarkerOptions>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
}
@Override
protected List<BaseMarkerOptions> doInBackground(Void... params) {
return NearbyController.loadAttractionsFromLocationToBaseMarkerOptions(
((NearbyActivity)getActivity()).getLocationManager().getLatestLocation(), getActivity()
);
}
@Override
protected void onPostExecute(final List<BaseMarkerOptions> baseMarkerOptionses) {
super.onPostExecute(baseMarkerOptionses);
if (isCancelled()) {
return;
}
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
mapboxMap.addMarkers(baseMarkerOptionses);
}
});
}
}
}

View file

@ -13,4 +13,11 @@
mapbox:mapbox_cameraTargetLat="50.0755"
mapbox:mapbox_cameraTargetLng="14.4378"
mapbox:mapbox_styleUrl="@string/style_mapbox_streets" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</LinearLayout>