14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

Chapter 7: Maps, Geocoding, and Location-Based Services<br />

do {<br />

Double lat;<br />

lat = earthquakes.getFloat(EarthquakeProvider.LATITUDE_COLUMN) * 1E6;<br />

Double lng;<br />

lng = earthquakes.getFloat(EarthquakeProvider.LONGITUDE_COLUMN) * 1E6;<br />

GeoPoint geoPoint = new GeoPoint(lng.intValue(), lat.intValue());<br />

quakeLocations.add(geoPoint);<br />

} while(earthquakes.moveToNext());<br />

5.3. Call refreshQuakeLocations from the Overlay’s constructor. Also register a<br />

DataSetObserver on the results Cursor that refreshes the Earthquake Location<br />

list if a change in the Earthquake Cursor is detected.<br />

public EarthquakeOverlay(Cursor cursor) {<br />

super();<br />

earthquakes = cursor;<br />

}<br />

quakeLocations = new ArrayList();<br />

refreshQuakeLocations();<br />

earthquakes.registerDataSetObserver(new DataSetObserver() {<br />

@Override<br />

public void onChanged() {<br />

refreshQuakeLocations();<br />

}<br />

});<br />

5.4. Complete the EarthquakeOverlay by overriding the draw method to iterate<br />

over the list of GeoPoints, drawing a marker at each earthquake location. In this<br />

example, a simple red circle is drawn, but it could easily be modifi ed to include<br />

additional information, such as by adjusting the size of each circle based on the<br />

magnitude of the quake.<br />

int rad = 5;<br />

@Override<br />

public void draw(Canvas canvas, MapView mapView, boolean shadow) {<br />

Projection projection = mapView.getProjection();<br />

// Create and setup your paint brush<br />

Paint paint = new Paint();<br />

paint.setARGB(250, 255, 0, 0);<br />

paint.setAnti<strong>Ali</strong>as(true);<br />

paint.setFakeBoldText(true);<br />

if (shadow == false) {<br />

for (GeoPoint point : quakeLocations) {<br />

Point myPoint = new Point();<br />

projection.toPixels(point, myPoint);<br />

245

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!