我試圖設置一個MKMapView用戶的位置總是偏移在視圖控制器的右上角,我會圍繞該「塊」構建我的其餘用戶界面。是否可以爲MKMapView上的用戶定位銷設置固定的屏幕位置?
有沒有辦法設置一個固定/偏移中心點爲MKMapView例如以一種方式,引腳總是在屏幕上的x,y位置?
如果您無法理解我的目標,我可以提供一個草圖示例。
我試圖設置一個MKMapView用戶的位置總是偏移在視圖控制器的右上角,我會圍繞該「塊」構建我的其餘用戶界面。是否可以爲MKMapView上的用戶定位銷設置固定的屏幕位置?
有沒有辦法設置一個固定/偏移中心點爲MKMapView例如以一種方式,引腳總是在屏幕上的x,y位置?
如果您無法理解我的目標,我可以提供一個草圖示例。
我想通了就在剛纔,
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答的答案是一個很好的起點,但遺憾的是,它不足以做我正在尋找的東西。
我繼續前進,並在地圖視圖頂部添加了遮罩視圖,以便更好地直觀地瞭解我最終要做什麼樣的UI。
您可以將像素座標圖,以座標知道在哪裏添加您的腳,每一個地圖區域發生變化時更新:
CGPoint fakecenter = CGPointMake(20, 20);
CLLocationCoordinate2D coordinate = [mapView convertPoint:fakecenter toCoordinateFromView:mapView];
所以這基本上將意味着我將不得不隱藏用戶的位置,但繼續跟隨用戶,並用自己的腳? –
實際上,在你讀兩次你想做的事情之後,你應該能夠得到用戶用戶座標和先前計算出的座標之間的差異,並將該地圖置於'user location - diff'中。 –
以前計算的座標是指虛假中心位置? –