如何将地图视图居中,使选定的图钉不在地图的中间,而是在地图的1/3? [英] How can I center my mapview so that the selected pin is not in the middle of the map but in 1/3 of it?

查看:97
本文介绍了如何将地图视图居中,使选定的图钉不在地图的中间,而是在地图的1/3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的地图上只有一个图钉的坐标.我尝试将地图的中心放在该图钉上,但我不想将图钉直接放在地图的中间,而是在屏幕的1/3处.我尝试通过以下解决方法来做到这一点:

I have coordinates of a single pin on my map. I try to center my map on that pin, but I don't want to keep the pin directly in the middle of the map, but in 1/3 of the screen. I tried to do it in the following workaround:

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)

,但根据地图的第一个位置显示随机位置.您能帮我重写代码吗,以便我可以按一下图钉的位置并得到如下结果:

but it shows random positions depending on the first position of the map. Can you help me with rewriting my code so that I could just put a pin location and get the result as follows:

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

推荐答案

如果您希望该点使用当前的缩放比例,但仅以坐标为中心,使其位于屏幕上方的1/3处,则可以只需设置centerCoordinate,然后将其微调为spanlatitudeDelta的六分之一即可:

If you want the point to use the current zoom in scale, but just center on a coordinate such that it's 1/3rd the way up the screen, you could just set the centerCoordinate and then nudge it up one sixth the latitudeDelta of the span:

func updateMapForCoordinate(coordinate: CLLocationCoordinate2D) {
    var center = coordinate;
    center.latitude -= self.mapView.region.span.latitudeDelta / 6.0;
    mapView.setCenterCoordinate(center, animated: true);
}

根据需要调整center的调整,但是希望这可以说明一种简单的方法.显然,此小技巧仅在地图没有倾斜且未旋转的情况下有效.

Tweak the adjustment of the center as you see fit, but hopefully this illustrates one simple approach. Obviously, this little trick only works if the map is has no pitch and isn't rotated.

如果要先放大,可以设置相机,然后重设centerCoordinate:

If you want to zoom in first, you can set the camera, and then reset the 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);
}

这篇关于如何将地图视图居中,使选定的图钉不在地图的中间,而是在地图的1/3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆