Tracking MKMapViewCoordinate Center During Panning

I implement the map function in my application, where I allow the user to set their current location by panning.

All this time I want to have MKAnnotation in centerCoordinate . Therefore, I want to keep track of when the center of the Coordinate map changes and correctly changes the coordinate of the annotation. The behavior would be similar to the behavior of Uber, Hailo and others.

I tried a time-based implementation where all 0.00001s centerCoordinate will be checked, and the annotation will also be moved. But if the card does not move accurately, the annotation jumps from one place to another, which does not create a good user interface.

Another implementation that I tried is gesture recognizers and MKMapView delegate MKMapView ( regionDidChange / regionWillChange ). This, again, leads to a very sharp transition.

Can anyone advise me how to do this better?

+5
source share
1 answer

I suggest not using the actual id<MKAnnotation> (at least for this mode of "setting the current location").

Instead of this:

  1. Add a view (e.g., UIImageView ) containing the pin image (or any icon you like) in the front of the map view.
  2. This kind of pin should not be a sub-representation of the type of map.
  3. The pin view must be a subview of the same view that the map view is a subview (for example, both must be subviews of the same superview).
  4. The pin view must be sized and positioned so that it is displayed above the center of the map view (you can make the pin view have the same frame and the same auto-marking / auto-size logic as the map view so that they remain visually synchronized regardless of size screen or orientation).
  5. If you use UIImageView , set its content mode to "center" and the background color to "clear" (default is clear).
  6. On the pin view, user interaction must be disabled so that the user can still interact with the map view behind him. As the user moves or zooms in on the map, the front view of the pin seems to instantly move.
  7. If necessary, the application can get location coordinates from mapView.centerCoordinate in the regionDidChangeAnimated: MKMapViewDelegate method (or in pan-pin gesture recognizers) or only when the user says that he has completed positioning. I do not recommend using a timer (especially every 0.00001 s) to request the current center coordinate.
  8. When the user indicates that the current position is where he wants to finally place the annotation, you can then create and add the actual annotation at this coordinate and hide the output of the "location setting mode" output.
+27
source

Source: https://habr.com/ru/post/891536/


All Articles