Commits

littledot5566  committed f2cb6bb

Created HuntingMapActivity.java to display treasure location using Google Maps, updated Manifest, added Overlay Images and their layouts.

  • Participants
  • Parent commits 39c457d

Comments (0)

Files changed (8)

File AndroidManifest.xml

             android:screenOrientation="portrait" >
         </activity>
         <!--  -->
+        <activity
+            android:name=".HuntingMapActivity"
+            android:configChanges="keyboardHidden|orientation"
+            android:screenOrientation="portrait" >
+        </activity>
+        <!--  -->
         <activity android:name=".MainActivity" >
         </activity>
         <!--  -->

File res/drawable/androidmarker.png

Added
New image

File res/layout/hunting_act_layout.xml

         android:layout_height="wrap_content"
         android:orientation="vertical" >
 
-        <!--
-        <com.google.android.maps.MapView
-            android:id="@+id/mapview"
-            android:layout_width="fill_parent"
-            android:layout_height="wrap_content"
-            android:apiKey="0LByDM8JEn03L2EcIuW9j2KRDymM9vc6U9GRngA"
-            android:clickable="true" />
-        -->
-
         <TextView
             android:id="@+id/hunt_tv_cur_location"
             android:layout_width="wrap_content"
             android:id="@+id/hunt_tv_bearing"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" />
+
+        <Button
+            android:id="@+id/hunt_but_showmap"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:onClick="onClick" />
     </LinearLayout>
 
 </ScrollView>

File res/layout/map_act_layout.xml

+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical" >
+
+    <com.google.android.maps.MapView
+        android:id="@+id/mapview"
+        android:layout_width="fill_parent"
+        android:layout_height="fill_parent"
+        android:apiKey="0-0tVBYzo78GcruEETjNXCnXjt4rzTKIkzU5HAQ"
+        android:clickable="true" />
+
+</LinearLayout>

File src/nctuw/littledot/localtreasure/Geodesy.java

 		double radLat = Math.toRadians(curLoc.getLatitude());
 		double radBearing = Math.toRadians(bearing);
 
-		double earthRadius = 6371009; //meter
+		double earthRadius = 6371009; // meter
 		double angle = distance / earthRadius;
 
 		double destLat = Math.asin(Math.sin(radLat) * Math.cos(angle)
 	}
 
 	public static Location calculateVincentyDestination(Location curLoc,
-			double bearing, double distance) {//meter
+			double bearing, double distance) {// meter
 		// instantiate the calculator
 		GeodeticCalculator geoCalc = new GeodeticCalculator();
 

File src/nctuw/littledot/localtreasure/HuntingActivity.java

 package nctuw.littledot.localtreasure;
 
+import java.util.List;
+
 import nctuw.littledot.util.Echo;
 import nctuw.littledot.util.Leg;
 import android.app.Dialog;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.DialogInterface.OnCancelListener;
+import android.content.Intent;
+import android.graphics.drawable.Drawable;
 import android.hardware.Sensor;
 import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
 import android.text.format.Time;
 import android.view.LayoutInflater;
 import android.view.Menu;
+import android.view.View;
 import android.widget.TextView;
 
+import com.google.android.maps.GeoPoint;
 import com.google.android.maps.MapActivity;
 import com.google.android.maps.MapView;
+import com.google.android.maps.Overlay;
+import com.google.android.maps.OverlayItem;
 
 public class HuntingActivity extends MapActivity implements LocationListener,
 		SensorEventListener {
 	private LocationManager			mLM;
 	private SensorManager				mSM;
 
+	private TextView						tvCurLoc;
+	private TextView						tvDestLoc;
+	private TextView						tvDistance;
+	private TextView						tvBearing;
+
+	private ProgressDialog			pdLock;
+
 	private int									mDistance;
 	private double							mBearing;
 	private Location						mCurLocation;
 
 	private float								mAcceler[];
 	private float								mMagnetic[];
-
-	private TextView						tvCurLoc;
-	private TextView						tvDestLoc;
-	private TextView						tvDistance;
-	private TextView						tvBearing;
-
-	private ProgressDialog			pdLock;
-	private MapView							map;
+	private List<Overlay>				mapOverlays;
 
 	/*
 	 * Base class Activity
 		tvDestLoc = (TextView) findViewById(R.id.hunt_tv_dest_location);
 		tvDistance = (TextView) findViewById(R.id.hunt_tv_distance);
 		tvBearing = (TextView) findViewById(R.id.hunt_tv_bearing);
-		//map = (MapView) findViewById(R.id.mapview);
 		pdLock = new ProgressDialog(this);
 
-		//map.setBuiltInZoomControls(true);
+		Drawable mapMarker = this.getResources().getDrawable(
+				R.drawable.androidmarker);
+		MapOverlay itemizedoverlay = new MapOverlay(mapMarker, this);
+
 		mAcceler = new float[3];
 		mMagnetic = new float[3];
 
 			Bundle extras = getIntent().getExtras();
 			mDistance = extras.getInt(SelectDistanceActivity.BUNDLE_DISTANCE, 0);
 
-			//get a fresh start, only concerns are time & accuracy
+			// get a fresh start, only concerns are time & accuracy
 			Time time = new Time();
 			time.setToNow();
 			long now = time.toMillis(false);
 			Location location = mLM
 					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
 
-			//time freshness must < 5 minutes & accuracy must < 100m
+			// time freshness must < 5 minutes & accuracy must < 100m
 			if (location != null && now - location.getTime() < LOCATION_TIME_THRESH
 					&& location.getAccuracy() < LOCATION_ACC_THRESH) {
 				mCurLocation = location;
 				Echo.d(this, "gps=\n" + location);
 			}
 			location = mLM.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
+
 			if (location != null
 					&& ((mCurLocation != null
 							&& location.getTime() > mCurLocation.getTime() && location
 			}
 
 			location = mLM.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
+
 			if (location != null
 					&& ((mCurLocation != null
 							&& location.getTime() > mCurLocation.getTime() && location
 				Echo.d(this, "passive=\n" + location);
 			}
 
-			//randomize direction, calculate destination if current location is set
+			// randomize direction, calculate destination if current location is set
 			mBearing = Math.random() * 360;
 			if (mCurLocation != null) {
 				mDestLocation = Geodesy.calculateVincentyDestination(mCurLocation,
 				tvDistance.setText("===dist==="
 						+ mCurLocation.distanceTo(mDestLocation));
 			} else {
-				//else calibrate device
+				// else calibrate device
 				showDialog(LOCATION_LOCK_DIALOG);
 			}
 		}
 	}
 
+	public void onClick(View v) {
+		if (v.getId() == R.id.hunt_but_showmap) {
+			Intent showmap = new Intent(this, HuntingMapActivity.class);
+			startActivity(showmap);
+		}
+	}
+
 	@Override
 	protected void onResume() {
 		super.onResume();
-		//register location updates
+		// register location updates
 		for (String provider : mLM.getAllProviders()) {
 			Leg.d("provider=" + provider);
 			mLM.requestLocationUpdates(provider, 0, 0, this);
 		}
 
-		//register sensor updates
+		// register sensor updates
 		mSM.registerListener(this, mSM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
 				SensorManager.SENSOR_DELAY_UI);
 		mSM.registerListener(this,
 
 		tvCurLoc.setText("===curLoc===\n" + location.toString());
 
-		//always recalibrate on first gps lock
+		// always recalibrate on first gps lock
 		if (mDestLocation == null || mRecalibrate) {
 			mDestLocation = Geodesy.calculateVincentyDestination(location, mBearing,
 					mDistance);

File src/nctuw/littledot/localtreasure/HuntingMapActivity.java

+package nctuw.littledot.localtreasure;
+
+import java.util.List;
+
+import android.content.Context;
+import android.graphics.drawable.Drawable;
+import android.location.Location;
+import android.location.LocationManager;
+import android.os.Bundle;
+
+import com.google.android.maps.GeoPoint;
+import com.google.android.maps.MapActivity;
+import com.google.android.maps.MapView;
+import com.google.android.maps.Overlay;
+import com.google.android.maps.OverlayItem;
+
+public class HuntingMapActivity extends MapActivity {
+	private LocationManager	mLM;
+
+	private MapView					map;
+
+	private List<Overlay>		mapOverlays;
+
+	@Override
+	public void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.map_act_layout);
+
+		mLM = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
+
+		map = (MapView) findViewById(R.id.mapview);
+
+		map.setBuiltInZoomControls(true);
+		mapOverlays = map.getOverlays();
+		Drawable mapMarker = this.getResources().getDrawable(
+				R.drawable.androidmarker);
+		MapOverlay itemizedoverlay = new MapOverlay(mapMarker, this);
+
+		Bundle extras = getIntent().getExtras();
+
+		// get a fresh start, only concerns are time & accuracy
+		GeoPoint point;
+		OverlayItem overlayitem;
+		Location location;
+
+		location = mLM.getLastKnownLocation(LocationManager.GPS_PROVIDER);
+
+		if (location != null) {
+			point = new GeoPoint((int) (location.getLatitude() * 1000000F),
+					(int) (location.getLongitude() * 1000000F));
+			overlayitem = new OverlayItem(point, "LK GPS", location.toString());
+			itemizedoverlay.addOverlay(overlayitem);
+		}
+
+		location = mLM.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
+
+		if (location != null) {
+			point = new GeoPoint((int) (location.getLatitude() * 1000000F),
+					(int) (location.getLongitude() * 1000000F));
+			overlayitem = new OverlayItem(point, "LK NETWORK", location.toString());
+			itemizedoverlay.addOverlay(overlayitem);
+		}
+
+		location = mLM.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
+
+		if (location != null) {
+			point = new GeoPoint((int) (location.getLatitude() * 1000000F),
+					(int) (location.getLongitude() * 1000000F));
+			overlayitem = new OverlayItem(point, "LK PASSIVE", location.toString());
+			itemizedoverlay.addOverlay(overlayitem);
+		}
+
+		mapOverlays.add(itemizedoverlay);
+
+	}
+
+	@Override
+	protected boolean isRouteDisplayed() {
+		return false;
+	}
+
+}

File src/nctuw/littledot/localtreasure/MapOverlay.java

+package nctuw.littledot.localtreasure;
+
+import java.util.ArrayList;
+
+import android.app.AlertDialog;
+import android.content.Context;
+import android.graphics.drawable.Drawable;
+
+import com.google.android.maps.ItemizedOverlay;
+import com.google.android.maps.OverlayItem;
+
+public class MapOverlay extends ItemizedOverlay<OverlayItem> {
+
+	private ArrayList<OverlayItem>	mOverlays	= new ArrayList<OverlayItem>();
+	private Context									mContext;
+
+	public MapOverlay(Drawable defaultMarker) {
+		super(boundCenterBottom(defaultMarker	));
+	}
+
+	public MapOverlay(Drawable defaultMarker, Context context) {
+		super(boundCenterBottom(defaultMarker));
+		mContext = context;
+	}
+
+	@Override
+	public int size() {
+		return mOverlays.size();
+	}
+
+	@Override
+	protected OverlayItem createItem(int i) {
+		// When the populate() method executes, it will call createItem(int) in the
+		// ItemizedOverlay to retrieve each OverlayItem. You must override this
+		// method to properly read from the ArrayList and return the OverlayItem
+		// from the position specified by the given integer.
+		return mOverlays.get(i);
+	}
+
+	public void addOverlay(OverlayItem overlay) {
+		mOverlays.add(overlay);
+
+		// Each time you add a new OverlayItem to the ArrayList, you must call
+		// populate() for the ItemizedOverlay, which will read each of the
+		// OverlayItem objects and prepare them to be drawn.
+
+		populate();
+	}
+
+	@Override
+	protected boolean onTap(int index) {
+		OverlayItem item = mOverlays.get(index);
+		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
+		dialog.setTitle(item.getTitle());
+		dialog.setMessage(item.getSnippet());
+		dialog.show();
+		return true;
+	}
+
+}