Working with map objects
During the class, we did operations with objects in the map. We have created MarkerManager. A class to help us manage markers in the map. It has a HashMap, which holds all the markers in the map. It is useful to hold the references for later use, like removing them.
Part of the MarkerManager is also a method for creating a custom marker icon from a layout file.
Once you create a marker, you can add a tag to it. The tag is very useful for holding the information about what is the object that marker represents.
class MarkerManager {
private val markers: HashMap<Long, Marker> = hashMapOf()
fun addMarkerToMap(context: Activity,
map: GoogleMap, place: Place ) {
if (!markers.containsKey(place.id)){
// the map of markers does not contain the marker with this id
val options = MarkerOptions()
options.position(LatLng(place.latitude, place.longitude))
options.icon(BitmapDescriptorFactory.fromBitmap(createCustomMarkerBitmap(context, place.id!!)))
options.draggable(true)
val marker = map.addMarker(options)
marker.tag = place.id
markers.put(place.id!!, marker)
}
}
fun removeMarker(id:Long) {
if (markers.containsKey(id)){
val marker = markers.get(id)
marker?.remove()
markers.remove(id)
}
}
private fun createCustomMarkerBitmap(context: Activity, id: Long): Bitmap {
val markerView = LayoutInflater.from(context).inflate(R.layout.marker_layout, null)
val textView = markerView.findViewById<TextView>(R.id.markerIdTV)
textView.text = id.toString()
val displayMetrics = DisplayMetrics()
context.windowManager.defaultDisplay.getMetrics(displayMetrics)
markerView.measure(displayMetrics.widthPixels, displayMetrics.heightPixels)
markerView.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels)
markerView.buildDrawingCache()
val bitmap = Bitmap.createBitmap(
markerView.getMeasuredWidth(),
markerView.getMeasuredHeight(),
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
markerView.draw(canvas)
return bitmap
}
}
Another part of working with map is controlling the map virtual camera. We have also created a class for it.
class MapManager {
fun zoomToLocation(location: LatLng, googleMap: GoogleMap){
googleMap.animateCamera(
CameraUpdateFactory.newLatLngZoom(location, 7.0f))
}
fun zoomToAllPlaces(googleMap: GoogleMap, places: MutableList<Place>){
if (places.size > 0) {
val builder = LatLngBounds.Builder()
for (place in places) {
builder.include(LatLng(place.latitude, place.longitude))
}
googleMap.animateCamera(
CameraUpdateFactory.newLatLngBounds(builder.build(), 100)
)
}
}
}
The class has two methods. One is for zooming to a specified location. The zoom level is between 2 and 21.
The second method is for zooming to all places using the LatLngBounds class.
Honourable mentions during the class:
- MaterialCardView — class for creating a card like design element. Very useful.
- lateinit — a keyword in kotlin the specify, that we will initialize the class later in code.
- ConstrainedLayout — very useful layout for holding other elements.
- moveCamera — a method of google map to just move the camera.
- animateCamera — a method of google map to animate the change of camera position. Looks better than move.