p-lr / MapView

A Fast, memory efficient Android library to display tiled maps, with support for markers, paths, and rotation.
Apache License 2.0
184 stars 38 forks source link

Marker size when changing the zoom level #25

Closed sana-20 closed 3 years ago

sana-20 commented 3 years ago

Hi Peter.

I’ve been developing metro app using your library and I added multiple markers in the map. But I come across the problem that some markers are overlapped as you can see below images.

What I want is changing the size of a marker along with the zoom level of the map or replace the marker image with other size. Say, if the map is zoomed out, the marker would be getting smaller so a user can click each marker precisely.

It would be great if you have any advice for solving this problem.

zoom_inzoom_out

p-lr commented 3 years ago

Hi @sana-20

Assuming your marker is a custom view, you need to scale it in your onDraw() overload. Here is an example, which draws a line between two points on the map. The line needs to be redrawn when the scale changes, just like your custom markers. You can see that all informations needed (scale, scroll, rotation) is taken from a ReferentialData instance.

To get the updated ReferentialData when e.g the scale changes, you need to use the addReferentialLister API on your MapView (see below the documentation of this API):

/**
 * Add a [ReferentialListener] to the MapView. This [ReferentialListener] will be notified of any
 * change of scale, rotation angle, or rotation pivot point.
 * Using this API, the MapView holds a reference on the provided [ReferentialListener] instance.
 * Don't forget to remove it when it's no longer needed, using [removeReferentialOwner].
 */
 fun addReferentialListener(listener: ReferentialListener) {
    listener.onReferentialChanged(referentialData)
    refListenerList.add(listener)
 }
sana-20 commented 3 years ago

Hi @peterLaurence. As your explanation, I changed my custom marker class and added addReferentialListener in mapView, then I called resizeMarker() in onReferentialChanged. It works as intended now. Thanks for your help.

p-lr commented 3 years ago

Great! One day, when you decide to migrate to Jetpack Compose, there will be a MapCompose library ready: https://github.com/peterLaurence/MapCompose For now, it's too early (Jetpack Compose is in beta).