Login     Sign Up
Tugadar (@admin)
14 days ago

Annotating “Where Am I?” This final modification to “Where Am I?” creates and adds a new Overlay that displays a red circle at the device’s current position.

Start by creating a new MyPositionOverlay Overlay class in the WhereAmI project.

package com.paad.whereami;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Point;

import android.graphics.RectF;

import android.location.Location;

import com.google.android.maps.GeoPoint;

import com.google.android.maps.MapView;

import com.google.android.maps.Overlay;

import com.google.android.maps.Projection;

public class MyPositionOverlay extends Overlay {


public void draw(Canvas canvas, MapView mapView, boolean shadow) {



public boolean onTap(GeoPoint point, MapView mapView) { return false;



Create a new instance variable to store the current Location, and add setter and getter methods for it.

Location location;

public Location getLocation() {

return location;


public void setLocation(Location location) {

this.location = location;


Override the draw method to add a small red circle at the current location.

private final int mRadius = 5;


public void draw(Canvas canvas, MapView mapView, boolean shadow) { Projection projection = mapView.getProjection();

if (shadow == false) {

// Get the current location

Double latitude = location.getLatitude()*1E6;

Double longitude = location.getLongitude()*1E6;

GeoPoint geoPoint;

geoPoint = new GeoPoint(latitude.intValue(),longitude.intValue());

// Convert the location to screen pixels

Point point = new Point();

projection.toPixels(geoPoint, point);

RectF oval = new RectF(point.x - mRadius, point.y - mRadius, point.x + mRadius, point.y + mRadius);

// Setup the paint

Paint paint = new Paint();

paint.setARGB(250, 255, 0, 0);



Paint backPaint = new Paint();

backPaint.setARGB(175, 50, 50, 50);


RectF backRect = new RectF(point.x
+ 2 +

- 3*mRadius,

+ 65,
point.y + mRadius);

Draw the marker canvas.drawOval(oval, paint); canvas.drawRoundRect(backRect, 5, 5, backPaint);

canvas.drawText(“Here I Am”, point.x + 2*mRadius, point.y, paint);


super.draw(canvas, mapView, shadow);


Now open the WhereAmI Activity class, and add the MyPositionOverlay to the MapView.

Start by adding a new instance variable to store the MyPositionOverlay, then override onCreate to create a new instance of the class, and add it to the MapView’s Overlay list.

MyPositionOverlay positionOverlay;


public void onCreate(Bundle icicle) {



MapView myMapView = (MapView)findViewById(R.id.myMapView); mapController = myMapView.getController();





Add the MyPositionOverlay positionOverlay = new MyPositionOverlay(); List<Overlay> overlays = myMapView.getOverlays();


LocationManager locationManager;

String context = Context.LOCATION_SERVICE;

locationManager = (LocationManager)getSystemService(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);

Location location = locationManager.getLastKnownLocation(provider);


locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);


Finally, update the updateWithNewLocation method to pass the new location to the overlay.

private void updateWithNewLocation(Location location) { String latLongString;

TextView myLocationText;

myLocationText = (TextView)findViewById(R.id.myLocationText); String addressString = “No address found”;

if (location != null) {

Update my location marker positionOverlay.setLocation(location);

Update the map location.

Double geoLat = location.getLatitude()*1E6; Double geoLng = location.getLongitude()*1E6; GeoPoint point = new GeoPoint(geoLat.intValue(),



double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = “Lat:” + lat + “\nLong:” + lng;

double latitude = location.getLatitude();

double longitude = location.getLongitude();

Geocoder gc = new Geocoder(this, Locale.getDefault()); try {

List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); StringBuilder sb = new StringBuilder();

if (addresses.size() > 0) {

Address address = addresses.get(0);

for (int i = 0; i < address.getMaxAddressLineIndex(); i++)


sb.append(address.getLocality()).append(“\n”); sb.append(address.getPostalCode()).append(“\n”); sb.append(address.getCountryName());


addressString = sb.toString();

} catch (IOException e) {} } else {

latLongString = “No location found”;


myLocationText.setText(“Your Current Position is:\n” + latLongString + “\n” + addressString);


When run, your application will display your current device location with a red circle and supporting text, as shown in Figure 7-7.

It’s worth noting that this is not the preferred technique for displaying your current location on a map. This functionality is implemented natively by Android through the MyLocationOverlay class. If you want to display and follow your current location, you should consider using this class (as shown in the next section) instead of implementing it manually as shown here.