mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
commit
676583b6ec
6 changed files with 175 additions and 88 deletions
|
|
@ -55,9 +55,11 @@ public class NearbyActivity extends BaseActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showMapView() {
|
private void showMapView() {
|
||||||
isMapViewActive = true;
|
if (!isMapViewActive) {
|
||||||
getSupportFragmentManager().beginTransaction()
|
getSupportFragmentManager().beginTransaction()
|
||||||
.replace(R.id.container, new NearbyMapFragment()).commit();
|
.replace(R.id.container, new NearbyMapFragment()).commit();
|
||||||
|
isMapViewActive = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import android.view.ViewGroup;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
|
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
|
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
public class NearbyAdapter extends ArrayAdapter<Place> {
|
public class NearbyAdapter extends ArrayAdapter<Place> {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,101 @@
|
||||||
|
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.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
|
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||||
|
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.location.LatLng;
|
||||||
|
|
||||||
|
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 timber.log.Timber;
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads attractions from location for list view, we need to return Place data type.
|
||||||
|
* @param curLatLng users current location
|
||||||
|
* @param context current activity
|
||||||
|
* @return Place list that holds nearby 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*Loads attractions from location for map view, we need to return BaseMarkerOption data type.
|
||||||
|
* @param curLatLng users current location
|
||||||
|
* @param context the activity
|
||||||
|
* @return BaseMarkerOprions list that holds nearby 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,14 +1,9 @@
|
||||||
package fr.free.nrw.commons.nearby;
|
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.Intent;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
|
||||||
import android.support.v4.app.ListFragment;
|
import android.support.v4.app.ListFragment;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
@ -16,24 +11,17 @@ import android.view.ViewGroup;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
import butterknife.OnItemClick;
|
import butterknife.OnItemClick;
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
import fr.free.nrw.commons.location.LatLng;
|
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;
|
import timber.log.Timber;
|
||||||
|
|
||||||
public class NearbyListFragment extends ListFragment {
|
public class NearbyListFragment extends ListFragment {
|
||||||
|
|
||||||
private static final int MAX_RESULTS = 1000;
|
|
||||||
private NearbyAsyncTask nearbyAsyncTask;
|
private NearbyAsyncTask nearbyAsyncTask;
|
||||||
|
|
||||||
@BindView(R.id.listView) ListView listview;
|
@BindView(R.id.listView) ListView listview;
|
||||||
|
|
@ -124,8 +112,10 @@ public class NearbyListFragment extends ListFragment {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<Place> doInBackground(Void... params) {
|
protected List<Place> doInBackground(Void... params) {
|
||||||
return loadAttractionsFromLocation(
|
return NearbyController.loadAttractionsFromLocationToPlaces(
|
||||||
((NearbyActivity)getActivity()).getLocationManager().getLatestLocation()
|
((NearbyActivity)getActivity())
|
||||||
|
.getLocationManager()
|
||||||
|
.getLatestLocation(), getActivity()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -164,40 +154,4 @@ public class NearbyListFragment extends ListFragment {
|
||||||
startActivity(mapIntent);
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,31 @@
|
||||||
package fr.free.nrw.commons.nearby;
|
package fr.free.nrw.commons.nearby;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
import butterknife.BindView;
|
|
||||||
import butterknife.ButterKnife;
|
|
||||||
|
|
||||||
import com.mapbox.mapboxsdk.Mapbox;
|
import com.mapbox.mapboxsdk.Mapbox;
|
||||||
|
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||||
|
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||||
|
import com.mapbox.mapboxsdk.constants.Style;
|
||||||
|
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||||
import com.mapbox.mapboxsdk.maps.MapView;
|
import com.mapbox.mapboxsdk.maps.MapView;
|
||||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||||
|
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
|
||||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
||||||
import com.mapbox.services.android.telemetry.MapboxTelemetry;
|
import com.mapbox.services.android.telemetry.MapboxTelemetry;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
|
|
||||||
public class NearbyMapFragment extends android.support.v4.app.Fragment {
|
public class NearbyMapFragment extends android.support.v4.app.Fragment {
|
||||||
@BindView(R.id.mapview) MapView mapView;
|
private NearbyAsyncTask nearbyAsyncTask;
|
||||||
|
private fr.free.nrw.commons.location.LatLng currentLocation;
|
||||||
|
private MapView mapView;
|
||||||
|
|
||||||
public NearbyMapFragment() {
|
public NearbyMapFragment() {
|
||||||
|
|
||||||
|
|
@ -26,7 +34,7 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
currentLocation = ((NearbyActivity)getActivity()).getLocationManager().getLatestLocation();
|
||||||
Mapbox.getInstance(getActivity(),
|
Mapbox.getInstance(getActivity(),
|
||||||
getString(R.string.mapbox_commons_app_token));
|
getString(R.string.mapbox_commons_app_token));
|
||||||
MapboxTelemetry.getInstance().setTelemetryEnabled(false);
|
MapboxTelemetry.getInstance().setTelemetryEnabled(false);
|
||||||
|
|
@ -35,21 +43,25 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
View view = inflater.inflate(R.layout.fragment_nearby_map, container, false);
|
MapboxMapOptions options = new MapboxMapOptions()
|
||||||
ButterKnife.bind(this, view);
|
.styleUrl(Style.OUTDOORS)
|
||||||
|
.camera(new CameraPosition.Builder()
|
||||||
|
.target(new LatLng(currentLocation.latitude, currentLocation.longitude))
|
||||||
|
.zoom(11)
|
||||||
|
.build());
|
||||||
|
|
||||||
|
// create map
|
||||||
|
mapView = new MapView(getActivity(), options);
|
||||||
mapView.onCreate(savedInstanceState);
|
mapView.onCreate(savedInstanceState);
|
||||||
|
|
||||||
setHasOptionsMenu(false);
|
setHasOptionsMenu(false);
|
||||||
|
return mapView;
|
||||||
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
|
@Override
|
||||||
|
|
@ -81,4 +93,39 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
|
||||||
mapView.onDestroy();
|
mapView.onDestroy();
|
||||||
super.onDestroyView();
|
super.onDestroyView();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<BaseMarkerOptions>> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPreExecute() {
|
||||||
|
super.onPreExecute();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onProgressUpdate(Integer... values) {
|
||||||
|
super.onProgressUpdate(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<BaseMarkerOptions> doInBackground(Void... params) {
|
||||||
|
return NearbyController
|
||||||
|
.loadAttractionsFromLocationToBaseMarkerOptions(currentLocation, 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:mapbox="http://schemas.android.com/tools"
|
|
||||||
android:orientation="vertical"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent">
|
|
||||||
|
|
||||||
<com.mapbox.mapboxsdk.maps.MapView
|
|
||||||
android:id="@+id/mapview"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
mapbox:mapbox_cameraZoom="11"
|
|
||||||
mapbox:mapbox_cameraTargetLat="50.0755"
|
|
||||||
mapbox:mapbox_cameraTargetLng="14.4378"
|
|
||||||
mapbox:mapbox_styleUrl="@string/style_mapbox_streets" />
|
|
||||||
</LinearLayout>
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue