0

我的地图上有一个大头针的坐标。我尝试将我的地图放在那个大头针的中心,但我不想把大头针直接放在地图的中间,而是放在屏幕的 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)

但它会根据地图的第一个位置显示随机位置。你能帮我重写我的代码,这样我就可以放置一个引脚位置并获得如下结果:

-------------------
|                 |
|                 |
|        X        | <-- my pin in the 1/3 of the visible map area.
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
-------------------
4

1 回答 1

4

如果您希望该点使用当前缩放比例,但只是以坐标为中心,使其位于屏幕上方的 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);
}

调整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);
}
于 2016-05-18T03:17:15.643 回答