diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java index ec410ca696..53e01af86a 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java @@ -55,9 +55,11 @@ public boolean onOptionsItemSelected(MenuItem item) { } private void showMapView() { - isMapViewActive = true; - getSupportFragmentManager().beginTransaction() - .replace(R.id.container, new NearbyMapFragment()).commit(); + if (!isMapViewActive) { + getSupportFragmentManager().beginTransaction() + .replace(R.id.container, new NearbyMapFragment()).commit(); + isMapViewActive = true; + } } @Override diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyAdapter.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyAdapter.java index 96a42e0084..d2ad0b9786 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyAdapter.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyAdapter.java @@ -7,7 +7,6 @@ import android.widget.ArrayAdapter; import fr.free.nrw.commons.R; - import timber.log.Timber; public class NearbyAdapter extends ArrayAdapter { diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java new file mode 100644 index 0000000000..0d0712e1b0 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java @@ -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 loadAttractionsFromLocation(LatLng curLatLng, Context context) { + Timber.d("Loading attractions near %s", curLatLng); + if (curLatLng == null) { + return Collections.emptyList(); + } + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + List 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 distances = new HashMap<>(); + for (Place place: places) { + distances.put(place, computeDistanceBetween(place.location, curLatLng)); + } + Collections.sort(places, + new Comparator() { + @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 loadAttractionsFromLocationToPlaces(LatLng curLatLng, + Context context) { + + List 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 loadAttractionsFromLocationToBaseMarkerOptions( + LatLng curLatLng, + Context context) { + List baseMarkerOptionses = new ArrayList<>(); + List 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; + } +} diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java index 6dae3bc901..5921c0e305 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java @@ -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.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,10 @@ protected void onProgressUpdate(Integer... values) { @Override protected List doInBackground(Void... params) { - return loadAttractionsFromLocation( - ((NearbyActivity)getActivity()).getLocationManager().getLatestLocation() + return NearbyController.loadAttractionsFromLocationToPlaces( + ((NearbyActivity)getActivity()) + .getLocationManager() + .getLatestLocation(), getActivity() ); } @@ -164,40 +154,4 @@ void onItemClicked(int position) { startActivity(mapIntent); } } - - private List loadAttractionsFromLocation(LatLng curLatLng) { - Timber.d("Loading attractions near %s", curLatLng); - if (curLatLng == null) { - return Collections.emptyList(); - } - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); - List 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 distances = new HashMap<>(); - for (Place place: places) { - distances.put(place, computeDistanceBetween(place.location, curLatLng)); - } - Collections.sort(places, - new Comparator() { - @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; - } } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java index 367ecc0392..7b627ecdfb 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java @@ -1,23 +1,31 @@ package fr.free.nrw.commons.nearby; +import android.os.AsyncTask; import android.os.Bundle; +import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import butterknife.BindView; -import butterknife.ButterKnife; - 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.MapboxMap; +import com.mapbox.mapboxsdk.maps.MapboxMapOptions; 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 { - @BindView(R.id.mapview) MapView mapView; + private NearbyAsyncTask nearbyAsyncTask; + private fr.free.nrw.commons.location.LatLng currentLocation; + private MapView mapView; public NearbyMapFragment() { @@ -26,7 +34,7 @@ public NearbyMapFragment() { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - + currentLocation = ((NearbyActivity)getActivity()).getLocationManager().getLatestLocation(); Mapbox.getInstance(getActivity(), getString(R.string.mapbox_commons_app_token)); MapboxTelemetry.getInstance().setTelemetryEnabled(false); @@ -35,21 +43,25 @@ public void onCreate(Bundle savedInstanceState) { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.fragment_nearby_map, container, false); - ButterKnife.bind(this, view); - + MapboxMapOptions options = new MapboxMapOptions() + .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); - 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 @@ -81,4 +93,39 @@ public void onDestroyView() { mapView.onDestroy(); super.onDestroyView(); } + + private class NearbyAsyncTask extends AsyncTask> { + + @Override + protected void onPreExecute() { + super.onPreExecute(); + } + + @Override + protected void onProgressUpdate(Integer... values) { + super.onProgressUpdate(values); + } + + @Override + protected List doInBackground(Void... params) { + return NearbyController + .loadAttractionsFromLocationToBaseMarkerOptions(currentLocation, getActivity() + ); + } + + @Override + protected void onPostExecute(final List baseMarkerOptionses) { + super.onPostExecute(baseMarkerOptionses); + + if (isCancelled()) { + return; + } + mapView.getMapAsync(new OnMapReadyCallback() { + @Override + public void onMapReady(MapboxMap mapboxMap) { + mapboxMap.addMarkers(baseMarkerOptionses); + } + }); + } + } } diff --git a/app/src/main/res/layout/fragment_nearby_map.xml b/app/src/main/res/layout/fragment_nearby_map.xml deleted file mode 100644 index ff4ba4d542..0000000000 --- a/app/src/main/res/layout/fragment_nearby_map.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - \ No newline at end of file