working Markers with mixed adapter view and fragment layout

This commit is contained in:
Domagalski 2018-10-20 18:09:28 +02:00
parent 2b4b83a0d2
commit 091b714b18
3 changed files with 205 additions and 16 deletions

View File

@ -45,7 +45,8 @@
android:label="@string/title_activity_sharing" />
<activity
android:name=".activity.MainActivity"
android:label="@string/title_activity_sharing" />
android:label="@string/title_activity_sharing"
android:launchMode="singleTop" />
<service
android:name=".service.BackgroundLocalizationService"

View File

@ -1,45 +1,68 @@
package com.uam.wmi.findmytutor.activity;
import android.animation.TypeEvaluator;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.widget.DrawerLayout;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Toast;
import android.support.v7.widget.Toolbar;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.annotations.MarkerViewOptions;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import android.util.Log;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.uam.wmi.findmytutor.ListViewAdapter;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.network.RetrofitClientInstance;
import com.uam.wmi.findmytutor.service.CoordinateService;
public class MainActivity extends AppCompatActivity {
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity implements
OnMapReadyCallback {
private BottomNavigationView mMainNav;
private FrameLayout mMainFrame;
@ -55,6 +78,16 @@ public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private MapView mapView;
private MapboxMap mapboxMap;
private int mInterval = 10000;
private Handler mHandler;
private Map<String,Coordinate> coordsMap = new HashMap<>();
private CoordinateService coordinateService;
private CompositeDisposable disposable = new CompositeDisposable();
// 4 search
private ListView listView;
@ -65,16 +98,60 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences sharedPref = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
final String authToken = sharedPref.getString("authToken",null);
coordinateService = RetrofitClientInstance.createService(CoordinateService.class,"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiI2YjhmNzFiMS00NDM2LTQxZGQtYjg3MC1mNzZlNjdkNDM4NDMiLCJzdWIiOiJzdHJpbmciLCJqdGkiOiJiZGRjZTAwMC0xN2U4LTQwNDUtYWZiNS1kY2RkOWNhNDFiNmQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJUdXRvciIsImV4cCI6MTU0MTcxNzk2MywiaXNzIjoiaHR0cDovL2ZpbmRteXR1dG9yLmNvbSIsImF1ZCI6Imh0dHA6Ly9maW5kbXl0dXRvci5jb20ifQ.JJVNeMAwwla6DJk6X8qZUgPFKJp-Epx55W9V_fIwpgg");
mHandler = new Handler();
Bundle extras = getIntent().getExtras();
Mapbox.getInstance(this, getString(R.string.access_token));
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
configureconfigureNavigationDrawer();
configureBottomNavigationView();
configureLogoutButton();
}
@Override
public void onMapReady(MapboxMap map) {
mapboxMap = map;
final Marker marker = mapboxMap.addMarker(new MarkerViewOptions()
.position(new LatLng(52.466782,16.927549)));
mStatusChecker.run();
mapboxMap.addOnMapClickListener(new MapboxMap.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng point) {
// When the user clicks on the map, we want to animate the marker to that
// location.
ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position",
new LatLngEvaluator(), marker.getPosition(), point);
markerAnimator.setDuration(2000);
markerAnimator.start();
}
});
}
Runnable mStatusChecker = new Runnable() {
@Override
public void run() {
try{
fetchTopCoords();
} finally {
mHandler.postDelayed(mStatusChecker, mInterval);
}
}
};
private void configureconfigureNavigationDrawer() {
listView = findViewById(R.id.list_item);
Toolbar toolbar = findViewById(R.id.toolbar_main);
@ -282,18 +359,116 @@ public class MainActivity extends AppCompatActivity {
}
}
@Override
protected void onResume() {
super.onResume();
private static class LatLngEvaluator implements TypeEvaluator<LatLng> {
// Method is used to interpolate the marker animation.
private LatLng latLng = new LatLng();
@Override
public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
latLng.setLatitude(startValue.getLatitude()
+ ((endValue.getLatitude() - startValue.getLatitude()) * fraction));
latLng.setLongitude(startValue.getLongitude()
+ ((endValue.getLongitude() - startValue.getLongitude()) * fraction));
return latLng;
}
}
private void fetchTopCoords() {
disposable.add(
// coordinateService.getTopCoordinates()
coordinateService.getOnlineCoordinates()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<List<Coordinate>>() {
@Override
public void onSuccess(List<Coordinate> coordsList) {
for (Coordinate element : coordsList) {
String id = element.getUserId();
Coordinate cord = coordsMap.get(id);
Log.d("mapper", "a " + mapboxMap.getMarkerViewManager());
Log.d("mapper", "b " + coordsMap.size());
if (cord != null) {
if (!cord.getLongitude().equals(element.getLongitude())
) {
Log.d("mapper", " cos sie zienilo ");
Marker marker = mapboxMap.addMarker(new MarkerViewOptions()
.title(cord.getUserId())
.position(new LatLng(cord.getLatitude(),cord.getLongitude())));
ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position",
new LatLngEvaluator(), marker.getPosition(), new LatLng(element.getLatitude(),element.getLongitude()));
markerAnimator.setDuration(2000);
markerAnimator.start();
coordsMap.replace(id,element);
} else if (!cord.getTimeStamp().equals(element.getTimeStamp())){
Log.d("mapper", "update");
Log.d("mapper", " "+cord.getTimeStamp());
Log.d("mapper", " "+element.getTimeStamp());
coordsMap.replace(id,element);
}
} else {
coordsMap.put(id,element);
mapboxMap.addMarker(new MarkerOptions().position(new LatLng(element.getLatitude(), element.getLongitude())));
}
}
}
@Override
public void onError(Throwable e) {
Log.e("Error",e.toString());
}
})
);
}
@Override
protected void onPause() {
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
mHandler.removeCallbacks(mStatusChecker);
disposable.dispose();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
@ -10,6 +11,17 @@
android:visibility="visible"
tools:openDrawer="start">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
mapbox:mapbox_cameraTargetLat="52.466799"
mapbox:mapbox_cameraTargetLng="16.927002"
mapbox:mapbox_cameraZoom="17"
mapbox:mapbox_styleUrl="mapbox://styles/domagalsky/cjiyzrqjp05l72rmj6ntvv2n8">
</com.mapbox.mapboxsdk.maps.MapView>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
@ -26,6 +38,7 @@
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">