ThreatMapFragment.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.util.Log;
  10. import android.view.InflateException;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import com.google.android.gms.common.ConnectionResult;
  15. import com.google.android.gms.maps.CameraUpdateFactory;
  16. import com.google.android.gms.maps.GoogleMap;
  17. import com.google.android.gms.maps.MapFragment;
  18. import com.google.android.gms.maps.model.CircleOptions;
  19. import com.google.android.gms.maps.model.LatLng;
  20. import com.google.android.gms.maps.model.Marker;
  21. import com.google.android.gms.maps.model.MarkerOptions;
  22. import java.util.ArrayList;
  23. import de.tudarmstadt.informatik.hostage.R;
  24. import de.tudarmstadt.informatik.hostage.logging.Record;
  25. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  26. import static com.google.android.gms.common.GooglePlayServicesUtil.*;
  27. /**
  28. * Created by fabio on 10.02.14.
  29. */
  30. public class ThreatMapFragment extends Fragment implements GoogleMap.OnMarkerClickListener {
  31. private static GoogleMap map = null;
  32. private static View view = null;
  33. /**
  34. * if google play services aren't available an error notification will be displayed
  35. *
  36. * @return true if the google play services are available
  37. */
  38. private boolean isGooglePlay() {
  39. int status = isGooglePlayServicesAvailable(getActivity());
  40. boolean result = status == ConnectionResult.SUCCESS;
  41. if (result == false) {
  42. getErrorDialog(status, getActivity(), 10).show();
  43. }
  44. return result;
  45. }
  46. @Override
  47. public boolean onMarkerClick(Marker marker) {
  48. Log.i("MARKER", ""+marker.getId());
  49. return false;
  50. }
  51. private void populateMap() {
  52. UglyDbHelper dbh = new UglyDbHelper(getActivity());
  53. ArrayList<Record> records = dbh.getAllRecords();
  54. CircleOptions circleOptions = new CircleOptions().radius(200.0).fillColor(Color.argb(127, 240, 80, 60)).strokeWidth(0.0f);
  55. for (Record record : records) {
  56. LatLng location = new LatLng(record.getLatitude(), record.getLongitude());
  57. map.addCircle(circleOptions.center(location));
  58. map.addMarker(new MarkerOptions().title(record.getSsid()).position(location));
  59. }
  60. map.setMyLocationEnabled(true);
  61. map.setOnMarkerClickListener(this);
  62. LatLng tudarmstadt = new LatLng(49.86923, 8.6632768);
  63. //LatLng mapCenter = new LatLng(41.889, -87.622);
  64. map.moveCamera(CameraUpdateFactory.newLatLngZoom(tudarmstadt, 13));
  65. }
  66. @Override
  67. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  68. Bundle savedInstanceState) {
  69. super.onCreateView(inflater, container, savedInstanceState);
  70. if (view != null) {
  71. ViewGroup parent = (ViewGroup) view.getParent();
  72. if (parent != null)
  73. parent.removeView(view);
  74. }
  75. try {
  76. view = inflater.inflate(R.layout.fragment_threatmap, container, false);
  77. if (isGooglePlay()) {
  78. map = ((MapFragment) getFragmentManager()
  79. .findFragmentById(R.id.threatmapfragment)).getMap();
  80. populateMap();
  81. }
  82. } catch (InflateException e) {
  83. // map already exists
  84. }
  85. return view;
  86. }
  87. }