【问题标题】:iOS set MKMapView center so supplied location is in the bottom centeriOS 设置 MKMapView 中心,所以提供的位置在底部中心
【发布时间】:2014-01-29 16:35:24
【问题描述】:

我有一个 MKMapView 和一个永不改变的 CLLocationCoordinate2D。我要做的是使地图居中,以便该坐标位于地图的底部中心。我可以用简单的方法将地图居中在这个坐标上:

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(mapCenter, 10000, 10000);
[self.mapView setRegion:viewRegion animated:YES];

但是我怎样才能使地图以使 mapCenter 坐标位于地图底部的点为中心呢?如果可能的话,无论地图的初始缩放级别如何,我都希望它能够正常工作。

【问题讨论】:

    标签: ios mkmapview


    【解决方案1】:

    我给你写了一个快速的方法,应该可以解决问题......

    在获得以位置坐标为中心的MKCoordinateRegion 后,您可以通过添加该区域纬度跨度的一小部分(在本例中为四分之一)来创建一个新的CLLocationCoordinate2D 中心点。使用新的中心点和旧区域的跨度创建一个新的坐标区域,将其设置为MKMapViewregion,就可以开始了。

    附: - 如果您希望位置一直居中在底部,请通过添加区域纬度跨度的一半(而不是四分之一)来创建新的 CLLocationCoordinate2D 中心点。

    -(void)setLocation:(CLLocationCoordinate2D)location inBottomCenterOfMapView:(MKMapView*)mapView
    {
        //Get the region (with the location centered) and the center point of that region
        MKCoordinateRegion oldRegion = [mapView regionThatFits:MKCoordinateRegionMakeWithDistance(location, 800, 800)];
        CLLocationCoordinate2D centerPointOfOldRegion = oldRegion.center;
    
        //Create a new center point (I added a quarter of oldRegion's latitudinal span)
        CLLocationCoordinate2D centerPointOfNewRegion = CLLocationCoordinate2DMake(centerPointOfOldRegion.latitude + oldRegion.span.latitudeDelta/4.0, centerPointOfOldRegion.longitude);
    
        //Create a new region with the new center point (same span as oldRegion)
        MKCoordinateRegion newRegion = MKCoordinateRegionMake(centerPointOfNewRegion, oldRegion.span);
    
        //Set the mapView's region
        [worldView setRegion:newRegion animated:YES];
    }
    

    【讨论】:

      【解决方案2】:

      马修太棒了!谢谢!

      这里是 SWIFT 3 中的相同解决方案:

      func setLocation(location: CLLocationCoordinate2D, inBottomCenterOfMapView: MKMapView) {
          let oldRegion = mapView.regionThatFits(MKCoordinateRegionMakeWithDistance(location, 800, 800))
          let centerPointOfOldRegion = oldRegion.center
          let centerPointOfNewRegion = CLLocationCoordinate2DMake(centerPointOfOldRegion.latitude + oldRegion.span.latitudeDelta/4.0, centerPointOfOldRegion.longitude)
          let newRegion = MKCoordinateRegionMake(centerPointOfNewRegion, oldRegion.span)
          worldView.setRegion(newRegion, animated: true)
      }
      

      【讨论】:

        【解决方案3】:

        请注意,上面的解决方案并没有真正改变地图中心,只是移动地图视图并在底部显示一个点。例如,如果您使用上面的代码并根据设备标题更新地图视图,当您旋转设备时,旋转不是围绕调整后的中心(在视图底部)......它仍然围绕视图的中间,即设备的中心。一个带注释的图钉(在视图的底部)然后在与新中心等距的半径上围绕地图视图浮动,并且可能从视图中消失(因为屏幕宽度

        要解决此问题,请设置约束以使地图视图固定到顶部布局指南,然后根据与屏幕宽度匹配的设备屏幕分辨率向地图视图添加约束,对于高度,将地图偏移到主视图下方(例如,将地图视图的高度设置为屏幕的 1.7 倍)在 Swift 4 中:

        let screenSize = UIScreen.main.bounds
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height*1.7
        
        let widthConstraint = NSLayoutConstraint(item: self.yourMapView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: screenWidth)
        self.yourMapView.addConstraint(widthConstraint)
        
        let heightConstraint = NSLayoutConstraint(item: self.yourMapView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: screenHeight)    
        self.yourMapView.addConstraint(heightConstraint)
        

        【讨论】:

          猜你喜欢
          • 2012-10-19
          • 1970-01-01
          • 1970-01-01
          • 2011-09-04
          • 1970-01-01
          • 1970-01-01
          • 2011-08-01
          • 1970-01-01
          • 2014-11-28
          相关资源
          最近更新 更多