/** * Android App that utilizes the GPS functionality in an Android Phone **/ package com.GPS.GPSLocator; //Classes in order to show map view & related functionalities import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; import com.google.android.maps.MapController; import com.google.android.maps.GeoPoint; import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.Geocoder; import android.location.Address; //Classes related to display location icon on the screen import com.google.android.maps.Overlay; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Point; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.util.List; import java.util.Locale; import java.io.IOException; import org.apache.http.client.CircularRedirectException; import android.os.Bundle; import android.widget.Toast; /** * Class GPSLocatorActivity * * Description: This class extends MapActivity. It sets the basic view and * navigations to Google Maps view. It also gets an instance of location manager * and sets location listener for callback functions. * * @author Madhavi * @author Viral */ public class GPSLocatorActivity extends MapActivity { // Actual view being shown on screen private MapView mapView; // Controller for MVC private MapController mapController; // Location manager related objects private LocationManager locationManager; private LocationListener locationListener; // Location update variables private long LocUpdateTimeouts = 0; //5000; // in mili-seconds private Float LocUpdateDistance = (float) 0; //10; // meters /* * function onCreate() This is a callback function. It is called by Android * OS framework. This call is very important to start the android activity. * * @see com.google.android.maps.MapActivity#onCreate(android.os.Bundle) * * @see com.GPS.GPSLocator.GPSLocationListener */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Get a location manager. String context = Context.LOCATION_SERVICE; locationManager = (LocationManager) getSystemService(context); // create a context Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); String provider = locationManager.getBestProvider(criteria, true); // get current location Location location = locationManager.getLastKnownLocation(provider); // Set location listener & callback // GPSLocationListener is a custom class. locationListener = new GPSLocationListener(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); mapView = (MapView) findViewById(R.id.mapView); // enable Street view by default mapView.setStreetView(true); mapView.setBuiltInZoomControls(true); // set default zoom level mapController = mapView.getController(); mapController.setZoom(16); } /** * function: isRouteDisplayed() We are not showing routes hence this will * always return false. */ @Override protected boolean isRouteDisplayed() { return false; } /** * Class GPSLocationListener * * This class implements LocationListener. It mainly handles the location * updates. * * @author Viral * @author Madhavi */ private class GPSLocationListener implements LocationListener { /* * function: onLocationChanged(Location) This function updates the * current position on the map. This happens whenever location update is * received. */ @Override public void onLocationChanged(Location location) { if (location != null) { // create GeoPoint object from location update GeoPoint point = new GeoPoint( (int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6)); // update map to show required area mapController.animateTo(point); // update zoom level mapController.setZoom(16); // add marker // show current location // 1. create a map overlay MapOverlay mapOverlay = new MapOverlay(); // 2. show current GeoPoint location mapOverlay.setPointToDraw(point); List listOfOverlays = mapView.getOverlays(); listOfOverlays.clear(); // 3. update overlay listOfOverlays.add(mapOverlay); // String address = ConvertPointToLocation(point); // Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT) // .show(); // force redraw mapView.invalidate(); } } // we do not utilize these functions for now // implemented as interface requirements @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } } /** * Class MapOverlay * * It extends Overlay class to draw current location on the map view. * * @author Madhavi * @author Viral */ class MapOverlay extends Overlay { // point to be drawn private GeoPoint pointToDraw; // setter method for the point to be drawn public void setPointToDraw(GeoPoint point) { pointToDraw = point; } // getter method for the point to be drawn public GeoPoint getPointToDraw() { return pointToDraw; } /** * function: draw(Canva, Mapview, boolean, long) * * This function draws given bitmap on to the map view. * */ @Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { super.draw(canvas, mapView, shadow); // convert point to pixels Point screenPts = new Point(); mapView.getProjection().toPixels(pointToDraw, screenPts); // add marker //Bitmap bmp = BitmapFactory.decodeResource(getResources(), red); Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.icon); Paint oPaint = new Paint(); oPaint.setAntiAlias(true); //canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, oPaint); return true; } } }