I have a MKMapView with a MKScaleView.
If I visualise a generic map I have the scale in km.
When I change the MKMapRect using visibleMapRect, the scale doesn't change.
If I use setVisibleMapRect(_ mapRect: MKMapRect, animated animate: Bool), the scale change but not to the correct one. For example, it shows a scale saying one inch corresponds to 250 m while it is 150 m.
The same issue of I use MKCoordinateRegion.
Instead, if I zoom in or zoom out pinching on the map, the scale updates correctly.
Am I doing something wrong? How can I fix this?
Sample code:
import UIKit
import MapKit
let CORNER_RADIUS: CGFloat = 8.0
let METERS_PER_MILE: Double = 1609.344
class PIAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
private(set) var title: String?
private(set) var subtitle: String?
init(location: CLLocationCoordinate2D,
title: String? = nil, subtitle: String? = nil) {
coordinate = location
self.title = title
self.subtitle = subtitle
}
}
class PISimpleMapView: MKMapView {
private let HALF_MAP_SIDE_MULTIPLIER: Double = 1.4
private let pinIdentifier = "pinIdentifier"
private var scaleView: MKScaleView?
typealias PinAnnotationView = MKMarkerAnnotationView // MKPinAnnotationView
required init?(coder: NSCoder) {
super.init(coder: coder)
inizialize()
}
override init(frame: CGRect) {
super.init(frame: frame)
inizialize()
}
func inizialize() {
layer.cornerRadius = CORNER_RADIUS
register(PinAnnotationView.self,
forAnnotationViewWithReuseIdentifier: pinIdentifier)
addScale()
}
private func addScale() {
let scale = MKScaleView(mapView: self)
scale.translatesAutoresizingMaskIntoConstraints = false
scale.scaleVisibility = .visible // always visible
addSubview(scale)
let guide = safeAreaLayoutGuide
NSLayoutConstraint.activate([
scale.leftAnchor.constraint(equalTo: guide.leftAnchor, constant: 16.0),
scale.rightAnchor.constraint(equalTo: guide.centerXAnchor),
scale.topAnchor.constraint(equalTo: guide.topAnchor),
scale.heightAnchor.constraint(equalToConstant: 20.0)
])
scaleView?.removeFromSuperview()
scaleView = scale
}
func displayPinOnMap(location: CLLocation) {
let annotation = PIAnnotation(location: location.coordinate,
title: "Sample", subtitle: nil)
addAnnotation(annotation)
// Position the map so that all overlays and annotations are visible on screen.
visibleMapRect = visibleArea(from: annotation)
// setVisibleMapRect(visibleArea(from: annotation), animated: true)
// region = MKCoordinateRegion(visibleArea(from: annotation))
}
private func visibleArea(from annotation: PIAnnotation) -> MKMapRect {
let annotationPoint = MKMapPoint(annotation.coordinate)
return MKMapRect(x: annotationPoint.x - HALF_MAP_SIDE_MULTIPLIER * METERS_PER_MILE,
y: annotationPoint.y - HALF_MAP_SIDE_MULTIPLIER * METERS_PER_MILE,
width: HALF_MAP_SIDE_MULTIPLIER * 2.0 * METERS_PER_MILE,
height: HALF_MAP_SIDE_MULTIPLIER * 2.0 * METERS_PER_MILE)
}
}
Post
Replies
Boosts
Views
Activity
I am using PHPickerViewController to get assets from the photo library. It works fine, but it presents the images so that the newest is first. I would like to choose the order. In some cases I prefer the oldest image to be first.
I have checked in PHPickerConfiguration with no success. I am just wondering if it is possible.
Since I upgraded to iOS 15, watchOS 8 and Xcode 13, after I turned off and on my Mac, every first time I want to launch an app from Xcode to my iPhone appear this message (even if I'm not developing for watchOS):
iPhone is busy: Making Apple Watch ready for development
The Mac stays stuck on it for at least 20 minutes.
The only solution I found (until now) is to switch off WiFi on my Watch.
Is there a better solution?