Skip to content

Nearby map #558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.widget.ArrayAdapter;

import fr.free.nrw.commons.R;

import timber.log.Timber;

public class NearbyAdapter extends ArrayAdapter<Place> {
Expand Down
101 changes: 101 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
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;
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;
Expand Down Expand Up @@ -124,8 +112,10 @@ protected void onProgressUpdate(Integer... values) {

@Override
protected List<Place> doInBackground(Void... params) {
return loadAttractionsFromLocation(
((NearbyActivity)getActivity()).getLocationManager().getLatestLocation()
return NearbyController.loadAttractionsFromLocationToPlaces(
((NearbyActivity)getActivity())
.getLocationManager()
.getLatestLocation(), getActivity()
);
}

Expand Down Expand Up @@ -164,40 +154,4 @@ void onItemClicked(int position) {
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;
}
}
81 changes: 64 additions & 17 deletions app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java
Original file line number Diff line number Diff line change
@@ -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() {

Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -81,4 +93,39 @@ public void onDestroyView() {
mapView.onDestroy();
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);
}
});
}
}
}
16 changes: 0 additions & 16 deletions app/src/main/res/layout/fragment_nearby_map.xml

This file was deleted.