Procházet zdrojové kódy

determine location on map using locationclient

Fabio Arnold před 10 roky
rodič
revize
7c79690b7c

+ 0 - 1
src/de/tudarmstadt/informatik/hostage/ui2/fragment/RecordOverviewFragment.java

@@ -714,7 +714,6 @@ public class RecordOverviewFragment extends Fragment implements ChecklistDialog.
 				record.setLongitude(ssidLocation.longitude - bssidRadius + 2.0 * bssidRadius * Math.random());
 
 				dbh.addRecord(record);
-				Log.i("SSID", record.getSsid() + " " + record.getBssid());
 			}
 		}
 

+ 88 - 16
src/de/tudarmstadt/informatik/hostage/ui2/fragment/ThreatMapFragment.java

@@ -4,13 +4,16 @@ import android.app.Fragment;
 import android.graphics.Color;
 import android.location.Location;
 import android.os.Bundle;
-import android.util.Log;
 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.common.GooglePlayServicesClient;
+import com.google.android.gms.location.LocationClient;
+import com.google.android.gms.location.LocationListener;
+import com.google.android.gms.location.LocationRequest;
 import com.google.android.gms.maps.CameraUpdateFactory;
 import com.google.android.gms.maps.GoogleMap;
 import com.google.android.gms.maps.MapFragment;
@@ -35,12 +38,22 @@ import static com.google.android.gms.common.GooglePlayServicesUtil.*;
  *
  * Created by Fabio Arnold on 10.02.14.
  */
-public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindowClickListener {
-	private GoogleMap mMap = null;
+public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindowClickListener,
+		GooglePlayServicesClient.ConnectionCallbacks,
+		GooglePlayServicesClient.OnConnectionFailedListener,
+		LocationListener {
+	private static GoogleMap sMap = null;
 	private static View sView = null;
 
 	private HashMap<String, String> mMarkerIDToSSID = new HashMap<String, String>();
 
+	private LocationClient mLocationClient;
+	private static final LocationRequest REQUEST = LocationRequest.create()
+			.setExpirationDuration(5000) // 5 seconds
+			.setInterval(5000)           // 5 seconds
+			.setFastestInterval(16)      // 16ms = 60fps
+			.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
+
 	/**
 	 * if google play services aren't available an error notification will be displayed
 	 *
@@ -71,6 +84,34 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindo
 		}
 	}
 
+	/**
+	 * callbacks from LocationClient
+	 */
+	@Override
+	public void onConnected(Bundle bundle) {
+		mLocationClient.requestLocationUpdates(REQUEST, this);
+	}
+
+	@Override
+	public void onDisconnected() {
+
+	}
+
+	@Override
+	public void onConnectionFailed(ConnectionResult connectionResult) {
+
+	}
+
+	@Override
+	public void onLocationChanged(Location location) {
+		sMap.animateCamera(CameraUpdateFactory.newLatLng(
+				new LatLng(location.getLatitude(), location.getLongitude())));
+	}
+
+	/**
+	 * helper class
+	 * easier to use than LatLng
+	 */
 	private class Point {
 		public double x, y;
 		public Point(double sx, double sy) {
@@ -79,6 +120,11 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindo
 		}
 	}
 
+	/**
+	 * helper class
+	 * contains heuristic to split SSIDs by location
+	 * see MAX_DISTANCE
+	 */
 	private class SSIDArea {
 		private Point mMinimum, mMaximum;
 		public int numPoints;
@@ -128,6 +174,9 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindo
 		}
 	}
 
+	/**
+	 * fills the map with markers and circle representing SSIDs
+	 */
 	private void populateMap() {
 		UglyDbHelper dbh = new UglyDbHelper(getActivity());
 		ArrayList<Record> records = dbh.getAllRecords();
@@ -135,8 +184,6 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindo
 		HashMap<String, ArrayList<SSIDArea>> threadAreas = new HashMap<String, ArrayList<SSIDArea>>();
 
 		for (Record record : records) {
-			Log.i("SSID", record.getSsid() + " bssid " + record.getBssid());
-
 			LatLng location = new LatLng(record.getLatitude(), record.getLongitude());
 			ArrayList<SSIDArea> areas;
 			if (threadAreas.containsKey(record.getSsid())) {
@@ -169,8 +216,8 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindo
 				LatLng center = area.calculateCenterLocation();
 				float radius = area.calculateRadius();
 
-				mMap.addCircle(circleOptions.center(center).radius(100.0 + radius).fillColor(color));
-				Marker marker = mMap.addMarker(new MarkerOptions()
+				sMap.addCircle(circleOptions.center(center).radius(100.0 + radius).fillColor(color));
+				Marker marker = sMap.addMarker(new MarkerOptions()
 						.title(ssid + ": " + area.numPoints + (area.numPoints == 1 ? " attack"
 								: " attacks")).position(
 								center));
@@ -179,16 +226,24 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindo
 			}
 		}
 
-		mMap.setMyLocationEnabled(true);
-		mMap.setOnInfoWindowClickListener(this);
-
-		LatLng tudarmstadt = new LatLng(49.86923, 8.6632768);
-		//LatLng mapCenter = new LatLng(41.889, -87.622);
+		sMap.setMyLocationEnabled(true);
+		sMap.setOnInfoWindowClickListener(this);
 
-		//Location myLocation = map.getMyLocation();
-		mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(tudarmstadt, 13));
+		LatLng tudarmstadt = new LatLng(49.86923, 8.6632768); // default location
+		sMap.moveCamera(CameraUpdateFactory.newLatLngZoom(tudarmstadt, 13));
 	}
 
+	/**
+	 * performs initialization
+	 * checks if google play services are supported
+	 * view must be removed if this object has been created once before
+	 * that is why view is static
+	 *
+	 * @param inflater
+	 * @param container
+	 * @param savedInstanceState
+	 * @return the view
+	 */
 	@Override
 	public View onCreateView(LayoutInflater inflater, ViewGroup container,
 	                         Bundle savedInstanceState) {
@@ -203,15 +258,32 @@ public class ThreatMapFragment extends Fragment implements GoogleMap.OnInfoWindo
 		try {
 			sView = inflater.inflate(R.layout.fragment_threatmap, container, false);
 			if (isGooglePlay()) {
-				mMap = ((MapFragment) getFragmentManager()
+				sMap = ((MapFragment) getFragmentManager()
 						.findFragmentById(R.id.threatmapfragment)).getMap();
 				populateMap();
 			}
 		} catch (InflateException e) {
         	// map already exists
-			e.printStackTrace();
+			//e.printStackTrace();
 		}
 
 		return sView;
 	}
+
+	@Override
+	public void onResume() {
+		super.onResume();
+		if (mLocationClient == null) {
+			mLocationClient = new LocationClient(MainActivity.getInstance().getApplicationContext(), this, this);
+		}
+		mLocationClient.connect();
+	}
+
+	@Override
+	public void onPause() {
+		super.onPause();
+		if (mLocationClient != null) {
+			mLocationClient.disconnect();
+		}
+	}
 }