package de.tudarmstadt.informatik.hostage.ui2.fragment; import android.app.AlertDialog; import android.app.Dialog; import android.app.Fragment; import android.content.Context; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.view.InflateException; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.model.CircleOptions; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import java.util.ArrayList; import de.tudarmstadt.informatik.hostage.R; import de.tudarmstadt.informatik.hostage.logging.Record; import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper; import static com.google.android.gms.common.GooglePlayServicesUtil.*; /** * Created by fabio on 10.02.14. */ public class ThreatMapFragment extends Fragment { private GoogleMap map = null; private static View view = null; private UglyDbHelper dbh; /** * if google play services aren't available an error notification will be displayed * * @return true if the google play services are available */ private boolean isGooglePlay() { int status = isGooglePlayServicesAvailable(getActivity()); boolean result = status == ConnectionResult.SUCCESS; if (result == false) { getErrorDialog(status, getActivity(), 10).show(); } return result; } private void loadMapFragment() { // Get a handle to the Map Fragment MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.threatmapfragment); map = mapFragment.getMap(); dbh = new UglyDbHelper(getActivity()); ArrayList records = dbh.getAllRecords(); CircleOptions circleOptions = new CircleOptions().radius(200.0).fillColor(Color.argb(127, 240, 80, 60)).strokeWidth(0.0f); for (Record record : records) { LatLng location = new LatLng(record.getLatitude(), record.getLongitude()); map.addCircle(circleOptions.center(location)); } LatLng tudarmstadt = new LatLng(49.86923, 8.6632768); //LatLng mapCenter = new LatLng(41.889, -87.622); map.moveCamera(CameraUpdateFactory.newLatLngZoom(tudarmstadt, 13)); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); if (view != null) { ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) parent.removeView(view); } try { view = inflater.inflate(R.layout.fragment_threatmap, container, false); } catch (InflateException e) { /* map is already there, just return view as it is */ } if (isGooglePlay()) { loadMapFragment(); } return view; } }