124 lines
3.5 KiB
Java
124 lines
3.5 KiB
Java
package at.fhj.airkoality.ui.fragment;
|
|
|
|
import android.Manifest;
|
|
import android.content.Intent;
|
|
import android.content.pm.PackageManager;
|
|
import android.os.Bundle;
|
|
import android.support.annotation.NonNull;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.v4.app.ActivityCompat;
|
|
import android.support.v4.app.Fragment;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
|
|
import com.google.android.gms.maps.GoogleMap;
|
|
import com.google.android.gms.maps.MapView;
|
|
import com.google.android.gms.maps.OnMapReadyCallback;
|
|
import com.google.android.gms.maps.model.LatLng;
|
|
import com.google.android.gms.maps.model.Marker;
|
|
import com.google.android.gms.maps.model.MarkerOptions;
|
|
|
|
import java.util.List;
|
|
|
|
import at.fhj.airkoality.R;
|
|
import at.fhj.airkoality.db.room.AirKoalityDB;
|
|
import at.fhj.airkoality.model.Location;
|
|
import at.fhj.airkoality.ui.activity.MeasurementActivity;
|
|
|
|
public class MapFragment extends Fragment implements OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {
|
|
|
|
private MapView mapView;
|
|
private GoogleMap map;
|
|
private AirKoalityDB database;
|
|
|
|
@Nullable
|
|
@Override
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
|
View view = inflater.inflate(R.layout.fragment_map, container, false);
|
|
|
|
mapView = view.findViewById(R.id.map);
|
|
mapView.onCreate(savedInstanceState);
|
|
mapView.getMapAsync(this);
|
|
|
|
database = AirKoalityDB.getDatabase(getContext());
|
|
|
|
return view;
|
|
}
|
|
|
|
private void enableMyLocation() {
|
|
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
|
if(map != null) {
|
|
map.setMyLocationEnabled(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void refresh(){
|
|
addMapMarkers();
|
|
}
|
|
|
|
private void addMapMarkers() {
|
|
if(map != null) {
|
|
new Thread(() -> {
|
|
List<Location> locations = database.locationDAO().getAll();
|
|
getActivity().runOnUiThread(() -> {
|
|
map.clear();
|
|
for (Location location : locations) {
|
|
MarkerOptions markerOptions = new MarkerOptions()
|
|
.title(location.getLocation())
|
|
.snippet(location.getCity() + ", " + location.getCountry())
|
|
.position(new LatLng(location.getLatitude(), location.getLongitude()));
|
|
|
|
map.addMarker(markerOptions);
|
|
}
|
|
});
|
|
}).start();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
public void onMapReady(GoogleMap googleMap) {
|
|
map = googleMap;
|
|
map.setOnInfoWindowClickListener(this);
|
|
enableMyLocation();
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onInfoWindowClick(Marker marker) {
|
|
Intent intent = new Intent(getContext(), MeasurementActivity.class);
|
|
intent.putExtra("location_name", marker.getTitle());
|
|
startActivity(intent);
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
mapView.onResume();
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
super.onPause();
|
|
mapView.onPause();
|
|
}
|
|
|
|
@Override
|
|
public void onLowMemory() {
|
|
super.onLowMemory();
|
|
mapView.onLowMemory();
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
mapView.onDestroy();
|
|
super.onDestroy();
|
|
|
|
}
|
|
}
|