20.08.2016 Views

Professional Android 4 Application Development

Create successful ePaper yourself

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

Creating Map-Based Activities x 545<br />

criteria.setPowerRequirement(Criteria.POWER_LOW);<br />

criteria.setAltitudeRequired(false);<br />

criteria.setBearingRequired(false);<br />

criteria.setSpeedRequired(false);<br />

criteria.setCostAllowed(true);<br />

String provider = locationManager.getBestProvider(criteria, true);<br />

Location l = locationManager.getLastKnownLocation(provider);<br />

updateWithNewLocation(l);<br />

}<br />

locationManager.requestLocationUpdates(provider, 2000, 10,<br />

locationListener);<br />

7. The final step is to modify the updateWithNewLocation method to re-center the map on the<br />

current location using the Map Controller:<br />

private void updateWithNewLocation(Location location) {<br />

TextView myLocationText;<br />

myLocationText = (TextView)findViewById(R.id.myLocationText);<br />

String latLongString = “No location found”;<br />

String addressString = “No address found”;<br />

if (location != null) {<br />

// Update the map location.<br />

Double geoLat = location.getLatitude()*1E6;<br />

Double geoLng = location.getLongitude()*1E6;<br />

GeoPoint point = new GeoPoint(geoLat.intValue(),<br />

geoLng.intValue());<br />

mapController.animateTo(point);<br />

double lat = location.getLatitude();<br />

double lng = location.getLongitude();<br />

latLongString = “Lat:” + lat + “\nLong:” + lng;<br />

double latitude = location.getLatitude();<br />

double longitude = location.getLongitude();<br />

Geocoder gc = new Geocoder(this, Locale.getDefault());<br />

if (!Geocoder.isPresent())<br />

addressString = “No geocoder available”;<br />

else {<br />

try {<br />

List addresses = gc.getFromLocation(latitude, longitude, 1);<br />

StringBuilder sb = new StringBuilder();<br />

if (addresses.size() > 0) {<br />

Address address = addresses.get(0);<br />

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

sb.append(address.getAddressLine(i)).append(“\n”);<br />

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

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

Saved successfully!

Ooh no, something went wrong!