mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
Fix map fragment
This commit is contained in:
parent
0901e67853
commit
fc2557d47f
3 changed files with 73 additions and 161 deletions
|
|
@ -21,7 +21,6 @@ import fr.free.nrw.commons.R;
|
||||||
import fr.free.nrw.commons.location.LatLng;
|
import fr.free.nrw.commons.location.LatLng;
|
||||||
import fr.free.nrw.commons.location.LocationServiceManager;
|
import fr.free.nrw.commons.location.LocationServiceManager;
|
||||||
import fr.free.nrw.commons.theme.BaseActivity;
|
import fr.free.nrw.commons.theme.BaseActivity;
|
||||||
import fr.free.nrw.commons.utils.UriDeserializer;
|
|
||||||
import fr.free.nrw.commons.utils.UriSerializer;
|
import fr.free.nrw.commons.utils.UriSerializer;
|
||||||
|
|
||||||
public class NearbyActivity extends BaseActivity {
|
public class NearbyActivity extends BaseActivity {
|
||||||
|
|
@ -46,6 +45,7 @@ public class NearbyActivity extends BaseActivity {
|
||||||
if (getSupportActionBar() != null) {
|
if (getSupportActionBar() != null) {
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
}
|
}
|
||||||
|
bundle = new Bundle();
|
||||||
locationManager = new LocationServiceManager(this);
|
locationManager = new LocationServiceManager(this);
|
||||||
locationManager.registerLocationManager();
|
locationManager.registerLocationManager();
|
||||||
curLatLang = locationManager.getLatestLocation();
|
curLatLang = locationManager.getLatestLocation();
|
||||||
|
|
@ -78,15 +78,7 @@ public class NearbyActivity extends BaseActivity {
|
||||||
|
|
||||||
private void showMapView() {
|
private void showMapView() {
|
||||||
if (!isMapViewActive) {
|
if (!isMapViewActive) {
|
||||||
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
|
setMapFragment();
|
||||||
NearbyMapFragment fragment = new NearbyMapFragment();
|
|
||||||
fragment.setArguments(bundle);
|
|
||||||
ft.add(R.id.container, fragment);
|
|
||||||
ft.commit();
|
|
||||||
|
|
||||||
//NearbyController.loadAttractionsFromLocationToBaseMarkerOptions(curLatLang, placeList);
|
|
||||||
/*getSupportFragmentManager().beginTransaction()
|
|
||||||
.replace(R.id.container, new NearbyMapFragment()).commit();*/
|
|
||||||
isMapViewActive = true;
|
isMapViewActive = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -97,15 +89,8 @@ public class NearbyActivity extends BaseActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void refreshView() {
|
protected void refreshView() {
|
||||||
//placeList = NearbyController.loadAttractionsFromLocation(curLatLang, this);
|
nearbyAsyncTask = new NearbyAsyncTask();
|
||||||
nearbyAsyncTask.execute();
|
nearbyAsyncTask.execute();
|
||||||
if (isMapViewActive) {
|
|
||||||
getSupportFragmentManager().beginTransaction()
|
|
||||||
.replace(R.id.container, new NearbyMapFragment()).commit();
|
|
||||||
} else {
|
|
||||||
getSupportFragmentManager().beginTransaction()
|
|
||||||
.replace(R.id.container, new NearbyListFragment()).commit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocationServiceManager getLocationManager() {
|
public LocationServiceManager getLocationManager() {
|
||||||
|
|
@ -144,27 +129,44 @@ public class NearbyActivity extends BaseActivity {
|
||||||
if (isCancelled()) {
|
if (isCancelled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//placeList = NearbyController.loadAttractionsFromLocation(curLatLang, getApplicationContext());
|
|
||||||
Gson gson = new GsonBuilder()
|
gson = new GsonBuilder()
|
||||||
.registerTypeAdapter(Uri.class, new UriSerializer())
|
.registerTypeAdapter(Uri.class, new UriSerializer())
|
||||||
.create();
|
.create();
|
||||||
gsonPlaceList = gson.toJson(placeList);
|
gsonPlaceList = gson.toJson(placeList);
|
||||||
gsonCurLatLng = gson.toJson(curLatLang);
|
gsonCurLatLng = gson.toJson(curLatLang);
|
||||||
|
|
||||||
bundle = new Bundle();
|
bundle.clear();
|
||||||
bundle.putString("PlaceList", gsonPlaceList);
|
bundle.putString("PlaceList", gsonPlaceList);
|
||||||
bundle.putString("CurLatLng", gsonCurLatLng);
|
bundle.putString("CurLatLng", gsonCurLatLng);
|
||||||
|
|
||||||
// Begin the transaction
|
// Begin the transaction
|
||||||
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
|
if (isMapViewActive) {
|
||||||
NearbyListFragment fragment = new NearbyListFragment();
|
setMapFragment();
|
||||||
fragment.setArguments(bundle);
|
} else {
|
||||||
ft.add(R.id.container, fragment);
|
setListFragment();
|
||||||
ft.commit();
|
}
|
||||||
|
|
||||||
if (progressBar != null) {
|
if (progressBar != null) {
|
||||||
progressBar.setVisibility(View.GONE);
|
progressBar.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setMapFragment() {
|
||||||
|
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
|
||||||
|
NearbyMapFragment fragment = new NearbyMapFragment();
|
||||||
|
fragment.setArguments(bundle);
|
||||||
|
ft.add(R.id.container, fragment);
|
||||||
|
ft.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListFragment() {
|
||||||
|
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
|
||||||
|
NearbyListFragment fragment = new NearbyListFragment();
|
||||||
|
fragment.setArguments(bundle);
|
||||||
|
ft.add(R.id.container, fragment);
|
||||||
|
ft.commit();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,21 +2,18 @@ package fr.free.nrw.commons.nearby;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
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;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.ProgressBar;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
|
|
@ -45,19 +42,6 @@ public class NearbyListFragment extends ListFragment {
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
Bundle bundle = this.getArguments();
|
|
||||||
gson = new GsonBuilder()
|
|
||||||
.registerTypeAdapter(Uri.class, new UriDeserializer())
|
|
||||||
.create();
|
|
||||||
if (bundle != null){
|
|
||||||
String gsonPlaceList = bundle.getString("PlaceList");
|
|
||||||
String gsonLatLng = bundle.getString("CurLatLng");
|
|
||||||
Type listType = new TypeToken<List<Place>>() {}.getType();
|
|
||||||
placeList = gson.fromJson(gsonPlaceList, listType);
|
|
||||||
Type curLatLngType = new TypeToken<LatLng>() {}.getType();
|
|
||||||
curLatLng = gson.fromJson(gsonLatLng, curLatLngType);
|
|
||||||
NearbyController.loadAttractionsFromLocationToPlaces(curLatLng, placeList);
|
|
||||||
}
|
|
||||||
setRetainInstance(true);
|
setRetainInstance(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,20 +62,22 @@ public class NearbyListFragment extends ListFragment {
|
||||||
|
|
||||||
// Check that this is the first time view is created,
|
// Check that this is the first time view is created,
|
||||||
// to avoid double list when screen orientation changed
|
// to avoid double list when screen orientation changed
|
||||||
|
Bundle bundle = this.getArguments();
|
||||||
|
gson = new GsonBuilder()
|
||||||
|
.registerTypeAdapter(Uri.class, new UriDeserializer())
|
||||||
|
.create();
|
||||||
|
if (bundle != null){
|
||||||
|
String gsonPlaceList = bundle.getString("PlaceList");
|
||||||
|
String gsonLatLng = bundle.getString("CurLatLng");
|
||||||
|
Type listType = new TypeToken<List<Place>>() {}.getType();
|
||||||
|
placeList = gson.fromJson(gsonPlaceList, listType);
|
||||||
|
Type curLatLngType = new TypeToken<LatLng>() {}.getType();
|
||||||
|
curLatLng = gson.fromJson(gsonLatLng, curLatLngType);
|
||||||
|
placeList = NearbyController.loadAttractionsFromLocationToPlaces(curLatLng, placeList);
|
||||||
|
}
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
adapter.clear();
|
adapter.clear();
|
||||||
//nearbyAsyncTask = new NearbyAsyncTask();
|
|
||||||
//nearbyAsyncTask.execute();
|
|
||||||
//progressBar.setVisibility(View.VISIBLE);
|
|
||||||
Timber.d("Saved instance state is null, populating ListView");
|
Timber.d("Saved instance state is null, populating ListView");
|
||||||
} else {
|
|
||||||
//progressBar.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we are returning here from a screen orientation and the AsyncTask is still working,
|
|
||||||
// re-create and display the progress dialog.
|
|
||||||
if (isTaskRunning()) {
|
|
||||||
//progressBar.setVisibility(View.VISIBLE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
adapter.clear();
|
adapter.clear();
|
||||||
|
|
@ -99,72 +85,6 @@ public class NearbyListFragment extends ListFragment {
|
||||||
adapter.notifyDataSetChanged();
|
adapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isTaskRunning() {
|
|
||||||
//return nearbyAsyncTask != null && nearbyAsyncTask.getStatus() != AsyncTask.Status.FINISHED;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDetach() {
|
|
||||||
// All dialogs should be closed before leaving the activity in order to avoid
|
|
||||||
// the: Activity has leaked window com.android.internal.policy... exception
|
|
||||||
/* if (progressBar != null && progressBar.isShown()) {
|
|
||||||
progressBar.setVisibility(View.GONE);
|
|
||||||
}*/
|
|
||||||
super.onDetach();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
|
|
||||||
// See http://stackoverflow.com/questions/18264408/incomplete-asynctask-crashes-my-app
|
|
||||||
if (isTaskRunning()) {
|
|
||||||
//nearbyAsyncTask.cancel(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<Place>> {
|
|
||||||
|
|
||||||
@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<Place> doInBackground(Void... params) {
|
|
||||||
return NearbyController.loadAttractionsFromLocationToPlaces(
|
|
||||||
((NearbyActivity)getActivity())
|
|
||||||
.getLocationManager()
|
|
||||||
.getLatestLocation(), getActivity()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostExecute(List<Place> places) {
|
|
||||||
super.onPostExecute(places);
|
|
||||||
|
|
||||||
if (isCancelled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (progressBar != null) {
|
|
||||||
progressBar.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
adapter.clear();
|
|
||||||
adapter.addAll(places);
|
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
@OnItemClick(R.id.listView)
|
@OnItemClick(R.id.listView)
|
||||||
void onItemClicked(int position) {
|
void onItemClicked(int position) {
|
||||||
Place place = (Place) listview.getItemAtPosition(position);
|
Place place = (Place) listview.getItemAtPosition(position);
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
package fr.free.nrw.commons.nearby;
|
package fr.free.nrw.commons.nearby;
|
||||||
|
|
||||||
import android.os.AsyncTask;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.Nullable;
|
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 com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
import com.mapbox.mapboxsdk.Mapbox;
|
import com.mapbox.mapboxsdk.Mapbox;
|
||||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||||
|
|
@ -18,14 +21,19 @@ 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.lang.reflect.Type;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
|
import fr.free.nrw.commons.utils.UriDeserializer;
|
||||||
|
|
||||||
public class NearbyMapFragment extends android.support.v4.app.Fragment {
|
public class NearbyMapFragment extends android.support.v4.app.Fragment {
|
||||||
//private NearbyAsyncTask nearbyAsyncTask;
|
//private NearbyAsyncTask nearbyAsyncTask;
|
||||||
private fr.free.nrw.commons.location.LatLng currentLocation;
|
|
||||||
private MapView mapView;
|
private MapView mapView;
|
||||||
|
private Gson gson;
|
||||||
|
private List<Place> placeList;
|
||||||
|
private List<BaseMarkerOptions> baseMarkerOptionses;
|
||||||
|
private fr.free.nrw.commons.location.LatLng curLatLng;
|
||||||
|
|
||||||
public NearbyMapFragment() {
|
public NearbyMapFragment() {
|
||||||
|
|
||||||
|
|
@ -34,7 +42,21 @@ 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();
|
Bundle bundle = this.getArguments();
|
||||||
|
gson = new GsonBuilder()
|
||||||
|
.registerTypeAdapter(Uri.class, new UriDeserializer())
|
||||||
|
.create();
|
||||||
|
if (bundle != null){
|
||||||
|
String gsonPlaceList = bundle.getString("PlaceList");
|
||||||
|
String gsonLatLng = bundle.getString("CurLatLng");
|
||||||
|
Type listType = new TypeToken<List<Place>>() {}.getType();
|
||||||
|
placeList = gson.fromJson(gsonPlaceList, listType);
|
||||||
|
Type curLatLngType = new TypeToken<fr.free.nrw.commons.location.LatLng>() {}.getType();
|
||||||
|
curLatLng = gson.fromJson(gsonLatLng, curLatLngType);
|
||||||
|
baseMarkerOptionses = NearbyController
|
||||||
|
.loadAttractionsFromLocationToBaseMarkerOptions(curLatLng, placeList);
|
||||||
|
|
||||||
|
}
|
||||||
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);
|
||||||
|
|
@ -46,13 +68,19 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
|
||||||
MapboxMapOptions options = new MapboxMapOptions()
|
MapboxMapOptions options = new MapboxMapOptions()
|
||||||
.styleUrl(Style.OUTDOORS)
|
.styleUrl(Style.OUTDOORS)
|
||||||
.camera(new CameraPosition.Builder()
|
.camera(new CameraPosition.Builder()
|
||||||
.target(new LatLng(currentLocation.latitude, currentLocation.longitude))
|
.target(new LatLng(curLatLng.latitude, curLatLng.longitude))
|
||||||
.zoom(11)
|
.zoom(11)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
// create map
|
// create map
|
||||||
mapView = new MapView(getActivity(), options);
|
mapView = new MapView(getActivity(), options);
|
||||||
mapView.onCreate(savedInstanceState);
|
mapView.onCreate(savedInstanceState);
|
||||||
|
mapView.getMapAsync(new OnMapReadyCallback() {
|
||||||
|
@Override
|
||||||
|
public void onMapReady(MapboxMap mapboxMap) {
|
||||||
|
mapboxMap.addMarkers(baseMarkerOptionses);
|
||||||
|
}
|
||||||
|
});
|
||||||
setHasOptionsMenu(false);
|
setHasOptionsMenu(false);
|
||||||
return mapView;
|
return mapView;
|
||||||
}
|
}
|
||||||
|
|
@ -60,8 +88,6 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
|
||||||
@Override
|
@Override
|
||||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
//nearbyAsyncTask = new NearbyAsyncTask();
|
|
||||||
//nearbyAsyncTask.execute();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -93,40 +119,4 @@ 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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue