是否可以在MKMapView上为用户位置图钉设置固定的屏幕位置? [英] Is it possible to set a fixed on-screen position for the User Location Pin on MKMapView?
问题描述
我正在尝试设置 MKMapView ,以使用户的位置始终在视图控制器的右上角偏移,并且我将围绕其余UI进行构建那个块".
I'm trying to set up a MKMapView in a way that the user's location would always be offset in the top right corner of the view controller, and I would build the rest of my UI around that "block".
是否有一种方法可以设置 MKMapView 的固定/偏移中心点,例如,使插针始终位于屏幕的x,y位置?
Is there a way to set a fixed/offset center point for the MKMapView for example in a way that the pin is always at an x, y position on the screen?
如果您在理解我的目标时遇到困难,我可以提供一个草绘的示例.
I can provide a sketched example if you are having trouble understanding my goal.
推荐答案
我刚刚知道了,
func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
// Define a span (for zoom)
let span: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
// Get user location
let location = CLLocationCoordinate2D(
latitude: userLocation.coordinate.latitude,
longitude: userLocation.coordinate.longitude
)
// Get the close region for the user's location
// This zooms into the user's tracked location
let region = MKCoordinateRegion(center: location, span: span)
let adjusted = mapView.regionThatFits(region)
mapView.setRegion(adjusted, animated: true)
// Here we set the offset of the map to be 75% to the right
// and 15% from the top. Gives us a nice top right view.
var rect = mapView.visibleMapRect
let point = MKMapPointForCoordinate(location)
rect.origin.x = point.x - rect.size.width * 0.75
rect.origin.y = point.y - rect.size.height * 0.15
mapView.setVisibleMapRect(rect, animated: true)
}
Pablo A.的回答是一个很好的起点,但可悲的是,这还不足以满足我的要求.
Pablo A.'s answer was a good starting point, but sadly it wasn't enough to do what I was looking for.
我继续在地图视图的顶部添加了蒙版视图,以更好地可视化最终要使用哪种UI.
I went ahead and added a mask view on top of the map view as well to better visualize what kind of UI I was going for in the end.
这篇关于是否可以在MKMapView上为用户位置图钉设置固定的屏幕位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!