ThreatMapFragment.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.app.AlertDialog;
  3. import android.app.Dialog;
  4. import android.app.Fragment;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.graphics.Color;
  8. import android.os.Bundle;
  9. import android.view.InflateException;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import com.google.android.gms.common.ConnectionResult;
  14. import com.google.android.gms.maps.CameraUpdateFactory;
  15. import com.google.android.gms.maps.GoogleMap;
  16. import com.google.android.gms.maps.MapFragment;
  17. import com.google.android.gms.maps.model.CircleOptions;
  18. import com.google.android.gms.maps.model.LatLng;
  19. import com.google.android.gms.maps.model.MarkerOptions;
  20. import java.util.ArrayList;
  21. import de.tudarmstadt.informatik.hostage.R;
  22. import de.tudarmstadt.informatik.hostage.logging.Record;
  23. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  24. import static com.google.android.gms.common.GooglePlayServicesUtil.*;
  25. /**
  26. * Created by fabio on 10.02.14.
  27. */
  28. public class ThreatMapFragment extends Fragment {
  29. private GoogleMap map = null;
  30. private static View view = null;
  31. private UglyDbHelper dbh;
  32. /**
  33. * if google play services aren't available an error notification will be displayed
  34. *
  35. * @return true if the google play services are available
  36. */
  37. private boolean isGooglePlay() {
  38. int status = isGooglePlayServicesAvailable(getActivity());
  39. boolean result = status == ConnectionResult.SUCCESS;
  40. if (result == false) {
  41. getErrorDialog(status, getActivity(), 10).show();
  42. }
  43. return result;
  44. }
  45. private void loadMapFragment() {
  46. // Get a handle to the Map Fragment
  47. MapFragment mapFragment = (MapFragment) getFragmentManager()
  48. .findFragmentById(R.id.threatmapfragment);
  49. map = mapFragment.getMap();
  50. dbh = new UglyDbHelper(getActivity());
  51. ArrayList<Record> records = dbh.getAllRecords();
  52. CircleOptions circleOptions = new CircleOptions().radius(200.0).fillColor(Color.argb(127, 240, 80, 60)).strokeWidth(0.0f);
  53. for (Record record : records) {
  54. LatLng location = new LatLng(record.getLatitude(), record.getLongitude());
  55. map.addCircle(circleOptions.center(location));
  56. }
  57. LatLng tudarmstadt = new LatLng(49.86923, 8.6632768);
  58. //LatLng mapCenter = new LatLng(41.889, -87.622);
  59. map.moveCamera(CameraUpdateFactory.newLatLngZoom(tudarmstadt, 13));
  60. }
  61. @Override
  62. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  63. Bundle savedInstanceState) {
  64. super.onCreateView(inflater, container, savedInstanceState);
  65. if (view != null) {
  66. ViewGroup parent = (ViewGroup) view.getParent();
  67. if (parent != null)
  68. parent.removeView(view);
  69. }
  70. try {
  71. view = inflater.inflate(R.layout.fragment_threatmap, container, false);
  72. } catch (InflateException e) {
  73. /* map is already there, just return view as it is */
  74. }
  75. if (isGooglePlay()) {
  76. loadMapFragment();
  77. }
  78. return view;
  79. }
  80. }