public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set up the map view - res/game.xml
setContentView(R.layout.game);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
//when clicked goal will be added - see below
mapView.setOnTouchListener(this);
// cb: this gets the gps location - not working
//LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//LocationListener myLocationListener = new CurrentLocationListener();
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, myLocationListener);
//Location currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//double Latitude = currentLocation.getLatitude();
//double Longitude = currentLocation.getLongitude();
//set up map overlays ("views") that show images
mapOverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.icon);
itemizedOverlay = new GameOverlay(drawable);
point = new GeoPoint(37779300, -122419200);
OverlayItem overlayitem = new OverlayItem(point, "", "");
// map controller and zoom - zooms in at level 17 to geopoint
MapController mc = mapView.getController();
mc.setZoom(17);
mc.animateTo(point);
//adds the overlay
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public boolean onTouch(View v, MotionEvent event) {
// variable so only one goal point can be set
goalSet = goalSet +1;
if(goalSet <=1) {// if a goal hasn't been set
if (event.getAction() == MotionEvent.ACTION_DOWN) { // if the user pushes down on the map
//set the goal geopoint based on the x/y coordinates of the phone interface
GeoPoint pGoal = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Log.d("tag","test " + pGoal);
// set up a new overlay
mapOverlays = mapView.getOverlays();
// new drawable needed for different image/icon
drawableGoal = this.getResources().getDrawable(R.drawable.goal);
itemizedGoalOverlay = new GameOverlay(drawableGoal);
OverlayItem goaloverlayitem = new OverlayItem(pGoal, "", "");
itemizedGoalOverlay.addOverlay(goaloverlayitem);
mapOverlays.add(itemizedGoalOverlay);
// code to get the address of the geopoint pressed
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
pGoal.getLatitudeE6() / 1E6,
pGoal.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
//popup - used just for testing to view address/lat/log info
Toast.makeText(getBaseContext(), add + ", " + pGoal.getLatitudeE6() / 1E6 + "," + pGoal.getLongitudeE6() /1E6, Toast.LENGTH_SHORT).show();
Log.v("tag", "test point = " + point + "goal = " +pGoal);
//calculate the distance between the user and the goal
double distanceUserGoal = calculateDistance(point, pGoal);
Log.v("tag", "test distance = " + calculateDistance(point, pGoal));
}
catch (IOException e) {
e.printStackTrace();
}
super.onTouchEvent(event);
return true;
}
}
// basically "removes" ontoucheventlistener - hack - couldn't find an alternate solution
return false;
}