Half of the fun in having location sensitive information is drawing it on a map. Create a new activity class to display a map centered on our current location with markers at our friends locations. While we're at it we can draw a line from our position to each of our friends.
The map control itself is called a MapView, but we can only use aMapView in a MapActivity, so we'll change the inheritance of this activity to MapActivity.
- public class MyMapViewActivity extends MapActivity
- MapView mapView = new MapView(this);
setContentView(mapView);
We'll want to get access to the OverlayController and MapController, so create global variables to store them and assign the references within the OnCreate method. We'll also be using the Locationinformation, so get a reference to that too. With the references assigned set your map zoom and starting location using theMapController. When you're finished OnCreate should look something like this.
- protected void onCreate(Bundle icicle) {
- super.onCreate(icicle);
MapView mapView = new MapView(this);
mapController = mapView.getController();
overlayController = mapView.createOverlayController();
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mapController.zoomTo(9);
setContentView(mMapView);
updateView();
- Double lat = location.getLatitude()*1E6;
Double lng = location.getLongitude()*1E6;
Point point = new Point(lat.intValue(), lng.intValue());
mapController.centerMapTo(point, false);
- MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
overlayController.add(myLocationOverlay, true);
- protected class MyLocationOverlay extends Overlay {
- @Override
public void draw(Canvas canvas, PixelCalculator calculator, boolean
shadow) {
- ... [ draw things here ] ...
- int[] screenCoords = new int[2];
calculator.getPointXY(point, screenCoords);
RectF oval = new RectF(...);
Paint paint = new Paint();
paint.setARGB(200, 255, 0, 0);
canvas.drawOval(oval, paint);
No comments:
Post a Comment