21.04.2014 Views

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

CURSO ANDROID<br />

70<br />

Movemos el centro <strong>de</strong>l mapa hacia esta ubicación:<br />

mapController.animateTo(point);<br />

mapController.setZoom(15);<br />

Posterior a esto, intentaremos hacer geolocalización <strong>de</strong>l punto, obtener una dirección a partir <strong>de</strong> las<br />

coor<strong>de</strong>nadas <strong>de</strong>l punto. Primero instanciamos un objeto Geoco<strong>de</strong>r que respon<strong>de</strong>rá <strong>de</strong> acuerdo a la<br />

localidad configurada en el teléfono.<br />

Geoco<strong>de</strong>r geoCo<strong>de</strong>r = new Geoco<strong>de</strong>r(this, Locale.getDefault());<br />

A través <strong>de</strong> este Geoco<strong>de</strong>r y con el punto <strong>de</strong> la ubicación intentamos obtener alguna dirección asociada<br />

y se lo hacemos saber al usuario por medio <strong>de</strong> un toast (aviso).<br />

try {<br />

List addresses = geoCo<strong>de</strong>r.getFromLocation(point.getLatitu<strong>de</strong>E6() /<br />

1E6, point.getLongitu<strong>de</strong>E6() / 1E6, 1);<br />

String address = “”;<br />

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

for (int i = 0; i < addresses.get(0).getMaxAddressLineIn<strong>de</strong>x(); i++)<br />

address += addresses.get(0).getAddressLine(i) + “\n”;<br />

}<br />

Toast.makeText(this, address, Toast.LENGTH_SHORT).show();<br />

} catch (IOException e) {<br />

e.printStackTrace();<br />

}<br />

Por último, instanciamos la clase MyOverlay construída previamente para agregar un nuevo marker en<br />

el listado <strong>de</strong> overlays <strong>de</strong>l mapa e invalidamos la vista para que vuelva a dibujarse todo.<br />

List mapOverlays = mapView.getOverlays();<br />

MyOverlay marker = new MyOverlay(point);<br />

mapOverlays.add(marker);<br />

mapView.invalidate();<br />

En el método onCreate vamos a instanciar un LocationManager para mostrar el primer marker y solicitar<br />

actualizaciones:

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

Saved successfully!

Ooh no, something went wrong!