如何在MKMapView中将图钉和地图保持在移动叠加上方的中心 [英] How to keep pin and map centered above moving overlay in an MKMapView

查看:193
本文介绍了如何在MKMapView中将图钉和地图保持在移动叠加上方的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我(通过平移手势")在地图上垂直移动另一个视图时,如何使大头针保持在地图上的中心位置,以使大头针保持在覆盖图上方(而不是实际的MapKit覆盖图).

How can I keep a pin centered on a map whilst I move (via Pan Gesture) another view vertically over the map such that the pin remains above the overlay (not an actual MapKit overlay).

请参阅所附的屏幕快照,了解开始和结束状态.

See attached screenshots for the first and final states.

当用户上下移动时,我已经获得了覆盖层和屏幕顶部之间空间的CGRect.但是,到目前为止,当用户向上平移时,我如何使用它来移动地图并固定地图,而当用户向下平移时,如何再次缩小地图,这使我难以理解.

I've got the CGRect of the space between the overlay and the top of the screen as the user pan's up / down. However, how I use that to move the map and pin whilst zooming into the map as the user pans upward..and zoom back out again when the user pans downward, has eluded me so far.

我尝试了不同的方法,从尝试调整可见矩形到调整地图视图的框架.答案可能在于某些MKMapRect/Region技巧..

I've tried different approaches, from attempting to adjust the visible rect to adjusting the map view's frame. The answer may lie in some MKMapRect / Region trickery..

(手形图标由 Freepik CC BY 3.0)

(Hand icon by Freepik CC BY 3.0)

推荐答案

实际上,keithbhunter的代码很慢,因为除了更新区域快于其加载速度之外,地图还改变了高度,这会导致额外的开销!

Actually, keithbhunter's code is slow because besides updating the region faster than it can load it, the map is also changing height which causes extra overhead!

我更新了代码,使其运行流畅.

I updated the code so that it runs smooth.

使用此代码,我要做的是保持地图视图的大小不变,但是我移动中心点以补偿滑动视图的高度.

With this code what i do is keep the map view the same size but instead i move the point of center to compensate for the height of the sliding view.

要使此代码正常工作,您必须修改keithbhunter的设置,以便将mapView的底部约束完全固定在超级视图的底部(而不是滑动视图)(这样,mapView的大小始终与超级视图相同).其余设置相同.

For this code to work, you have to modify keithbhunter's setup so that the mapView's bottom constraint is pinned completely to the superview's bottom (and not to the slidingView (so that the mapView is always the same size as the super view). For the rest the setup is the same.

还可以使用变量maxMetersDistance

在这里,我一直以埃菲尔铁塔为中心

Here i'm, always centering on the Eifel tower

import UIKit
import MapKit

class ViewController: UIViewController {

    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var slidingView: UIView!
    @IBOutlet weak var slidingViewHeight: NSLayoutConstraint!
    var maxMetersDistance:CGFloat = 10000.0; // customize this to set how far the map zooms out of the interest area

    override func viewDidLoad() {
        super.viewDidLoad()
        let pan = UIPanGestureRecognizer(target: self, action: "viewDidPan:")
        self.slidingView.addGestureRecognizer(pan)
        firstTimeCenter()
    }

    func firstTimeCenter(){
         var coordinate = CLLocationCoordinate2D(latitude: 48.8582, longitude: 2.2945)
        let region = MKCoordinateRegionMakeWithDistance(coordinate, Double(maxMetersDistance), Double(maxMetersDistance))
        self.mapView.setRegion(region, animated: true)
    }

    func reloadMap() {
        let height = CGFloat(self.slidingViewHeight.constant)
        var regionDistance = (maxMetersDistance / self.view.frame.height) * height
        regionDistance = maxMetersDistance - regionDistance
        var coordinate = CLLocationCoordinate2D(latitude: 48.8582, longitude: 2.2945)
        var mapRect = mapView.visibleMapRect;
        var metersPerMapPoint = MKMetersPerMapPointAtLatitude(coordinate.latitude);
        var metersPerPixel = CGFloat(metersPerMapPoint) * CGFloat(mapRect.size.width) / CGFloat(mapView.bounds.size.width);
        var totalMeters = Double(metersPerPixel) * Double(height/2)

        coordinate = self.translateCoord(coordinate, MetersLat: -totalMeters, MetersLong: 0.0)

        let region = MKCoordinateRegionMakeWithDistance(coordinate, Double(regionDistance), Double(regionDistance))
        self.mapView.setRegion(region, animated: false)
    }

    func viewDidPan(panGesture: UIPanGestureRecognizer) {
        let location = panGesture.locationInView(self.view)
        self.slidingViewHeight.constant = self.view.frame.size.height - location.y
        self.reloadMap()
    }

    func translateCoord(coord:CLLocationCoordinate2D, MetersLat:Double,  MetersLong:Double)->CLLocationCoordinate2D{
        var tempCoord = CLLocationCoordinate2D()
        var tempRegion = MKCoordinateRegionMakeWithDistance(coord, MetersLat, MetersLong);
        var tempSpan = tempRegion.span;
        tempCoord.latitude = coord.latitude + tempSpan.latitudeDelta;
        tempCoord.longitude = coord.longitude + tempSpan.longitudeDelta;
        return tempCoord;
    }

}

这篇关于如何在MKMapView中将图钉和地图保持在移动叠加上方的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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