use*_*930 0 mapkit mkmapview ios mkmapviewdelegate swift
我的地图上有一个图钉的坐标。我尝试将地图置于该图钉的中心,但我不想将图钉直接放在地图的中间,而是放在屏幕的 1/3 处。我尝试通过以下解决方法来做到这一点:
let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinatesOfThePin, 500, 500)
mapView.setRegion(coordinateRegion, animated: true)
let frame = mapView.frame
let myPoint = CGPointMake(frame.midX, frame.midY/3)
let myCoordinate = mapView.convertPoint(myPoint, toCoordinateFromView: mapView)
let coordinateRegion2 = MKCoordinateRegionMakeWithDistance(myCoordinate, 500, 500)
mapView.setRegion(coordinateRegion2, animated: true)
Run Code Online (Sandbox Code Playgroud)
但它根据地图的第一个位置显示随机位置。你能帮我重写我的代码,这样我就可以放置一个引脚位置并得到如下结果:
-------------------
| |
| |
| X | <-- my pin in the 1/3 of the visible map area.
| |
| |
| |
| |
| |
| |
| |
| |
-------------------
Run Code Online (Sandbox Code Playgroud)
如果您希望该点使用当前的缩放比例,但仅以某个坐标为中心,使其位于屏幕上方的 1/3,则只需设置centerCoordinate,然后将其向上推动六分latitudeDelta之一span:
func updateMapForCoordinate(coordinate: CLLocationCoordinate2D) {
var center = coordinate;
center.latitude -= self.mapView.region.span.latitudeDelta / 6.0;
mapView.setCenterCoordinate(center, animated: true);
}
Run Code Online (Sandbox Code Playgroud)
center根据您的需要调整调整,但希望这说明了一种简单的方法。显然,这个小技巧只有在地图没有倾斜且不旋转的情况下才有效。
如果要先放大,可以设置相机,然后重置centerCoordinate:
func updateMapForCoordinate(coordinate: CLLocationCoordinate2D) {
let camera = MKMapCamera(lookingAtCenterCoordinate: coordinate, fromDistance: 1000, pitch: 0, heading: 0)
mapView.setCamera(camera, animated: false)
var center = coordinate;
center.latitude -= self.mapView.region.span.latitudeDelta / 6.0;
mapView.setCenterCoordinate(center, animated: false);
}
Run Code Online (Sandbox Code Playgroud)