Chapter 34 Maps
Listing 34.13 Adding markers (LocatrFragment.java)
private void updateUI() {
...
LatLng itemPoint = new LatLng(mMapItem.getLat(), mMapItem.getLon());
LatLng myPoint = new LatLng(
mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
BitmapDescriptor itemBitmap = BitmapDescriptorFactory.fromBitmap(mMapImage);
MarkerOptions itemMarker = new MarkerOptions()
.position(itemPoint)
.icon(itemBitmap);
MarkerOptions myMarker = new MarkerOptions()
.position(myPoint);
mMap.clear();
mMap.addMarker(itemMarker);
mMap.addMarker(myMarker);
LatLngBounds bounds = new LatLngBounds.Builder()
...
}
When you call addMarker(MarkerOptions), the GoogleMap builds a Marker instance and adds it to the
map. If you need to remove or modify the marker in the future, you can hold on to this instance. In this
case, you will be clearing the map every time you update it. As a result, you do not need to hold on to
the Markers.
Run Locatr and press the search button, and you should see your two markers show up (Figure 34.3).
Figure 34.3 Geographic looming