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 a
MapView in a
MapActivity, so we'll change the inheritance of this activity to
MapActivity.
public class MyMapViewActivity extends MapActivity
To
display the map we need to create a new
MapView and set it as the content for our activity in the
OnCreate method.
MapView mapView = new MapView(this);
setContentView(mapView);
This will make the
MapView fill the entire screen, so use views like
LinearLayout if we want to create a
more complicated UI layout.
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 the
MapController. 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();
}
updateView is where we do the work. Start by getting our current location and convert the Lat/Long to a map
Point, then centre the map on our current location.
Double lat = location.getLatitude()*1E6;
Double lng = location.getLongitude()*1E6;
Point point = new Point(lat.intValue(), lng.intValue());
mapController.centerMapTo(point, false);
The only thing left to do on our map is draw markers and link them up with lines. To do this you need to create a new class that extends
Overlay, and add this using the
OverlayController.
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
overlayController.add(myLocationOverlay, true);
The work in the Overlay class is done by overriding the draw method.
protected class MyLocationOverlay extends Overlay {@Override
public void draw(Canvas canvas, PixelCalculator calculator, boolean
shadow) {... [ draw things here ] ...
}
}
I start by drawing a 'marker' on my current location. There doesn't seem to be support for 'traditional' Google Maps markers but you can achieve the same thing by
drawing on the map canvas; I chose to draw small circles as markers. First you need to use the
PixelCalculator to convert your Lat/Long points to screen coordinates, then create a
Paint object to define the colours and settings for your brush. Then paint your markers.
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);
I add my friends locations the same way as before, iterating over my address book grabbing names and locations. I filter out anyone too far away (say 10km) and draw markers, names (
drawText), and joining lines (
drawLine) to those nearby.