是否可以为 MKMapView 上的用户定位 Pin 设置固定的屏幕位置?
Is it possible to set a fixed on-screen position for the User Location Pin on MKMapView?
我正在尝试设置一个 MKMapView,使用户的位置始终在视图控制器的右上角偏移,我将构建我 UI 的其余部分围绕 "block"。
有没有办法为 MKMapView 设置一个 fixed/offset 中心点,例如使图钉始终位于屏幕上的 x、y 位置屏幕?
如果您无法理解我的目标,我可以提供一个简单的例子。
您可以将像素坐标转换为地图坐标,以便了解将图钉添加到何处,并在每次地图区域更改时更新它:
CGPoint fakecenter = CGPointMake(20, 20);
CLLocationCoordinate2D coordinate = [mapView convertPoint:fakecenter toCoordinateFromView:mapView];
我刚刚弄明白了,
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. 的回答是一个很好的起点,但遗憾的是,这还不足以满足我的需求。
我继续在地图视图的顶部添加了一个蒙版视图,以便更好地可视化我最终想要什么样的UI。
我正在尝试设置一个 MKMapView,使用户的位置始终在视图控制器的右上角偏移,我将构建我 UI 的其余部分围绕 "block"。
有没有办法为 MKMapView 设置一个 fixed/offset 中心点,例如使图钉始终位于屏幕上的 x、y 位置屏幕?
如果您无法理解我的目标,我可以提供一个简单的例子。
您可以将像素坐标转换为地图坐标,以便了解将图钉添加到何处,并在每次地图区域更改时更新它:
CGPoint fakecenter = CGPointMake(20, 20);
CLLocationCoordinate2D coordinate = [mapView convertPoint:fakecenter toCoordinateFromView:mapView];
我刚刚弄明白了,
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. 的回答是一个很好的起点,但遗憾的是,这还不足以满足我的需求。
我继续在地图视图的顶部添加了一个蒙版视图,以便更好地可视化我最终想要什么样的UI。