【问题标题】:How to show a map within two coordinates?如何在两个坐标内显示地图?
【发布时间】:2016-05-03 06:19:36
【问题描述】:

我想显示两个坐标之间的地图。我已使用此代码显示包含两个坐标的地图矩形。但是我的两个坐标都没有显示,而是显示了两个坐标之间的区域,不包括坐标。 我应该怎么做才能包括我的两个坐标?

    let coordinate1 = CLLocationCoordinate2DMake(28.53, 77.39)
    let coordinate2 = CLLocationCoordinate2DMake(29.13,76.69)
    // convert them to MKMapPoint
    let p1 = MKMapPointForCoordinate (coordinate2);
    let p2 = MKMapPointForCoordinate (coordinate1);

    let mapRect = MKMapRectMake(fmin(p1.x,p2.x), fmin(p1.y,p2.y), fabs(p1.x-p2.x), fabs(p1.y-p2.y));
    mapView.setVisibleMapRect(mapRect, animated: true)

【问题讨论】:

    标签: ios swift mapkit coordinates cllocationmanager


    【解决方案1】:

    基本上这是由于自动布局。我们应该使用mapViewDidFinishLoadingMap(mapView: MKMapView)方法来设置它。

    func mapViewDidFinishLoadingMap(mapView: MKMapView) {
        // this is where visible maprect should be set
        let coordinate1 = CLLocationCoordinate2DMake(28.53, 77.39)
        let coordinate2 = CLLocationCoordinate2DMake(29.13,76.69)
        // convert them to MKMapPoint
        let p1 = MKMapPointForCoordinate (coordinate2);
        let p2 = MKMapPointForCoordinate (coordinate1);
    
        let mapRect = MKMapRectMake(fmin(p1.x,p2.x), fmin(p1.y,p2.y), fabs(p1.x-p2.x), fabs(p1.y-p2.y));
        mapView.setVisibleMapRect(mapRect, animated: true)
    }
    

    【讨论】:

      【解决方案2】:

      你可以试试下面的函数,它使通过的点在地图中居中:

      func centerLocationInMapView(centerPoint: CLLocationCoordinate2D) {
          floatForRadiusInMiles = 10.0 // you can ignore this i have taken this for my custom radius property
          var scalingFactor: Double = abs((cos(2 * M_PI * centerPoint.latitude / 360.0)))
          var coordinateSpan: MKCoordinateSpan = MKCoordinateSpanMake(floatForRadiusInMiles / 69.0, floatForRadiusInMiles / (scalingFactor * 69.0))
          var coordinateRegion: MKCoordinateRegion = MKCoordinateRegionMake(centerPoint, coordinateSpan)
          self.mapViewForHomeScreen.scrollEnabled = true
          mapViewForHomeScreen.setRegion(coordinateRegion, animated: TRUE)
          mapViewForHomeScreen.regionThatFits(coordinateRegion)
      }
      

      调用这个函数

      self.centerLocationInMapView( (first latitude + second latitude)/2 , (first longitude + second longitude)/2 )
      

      在两个纬度,经度之间传递中心点,以便中心成为地图 中心!

      【讨论】:

      • 使用这个我肯定会得到中心点,但地图仍然显示中心点周围的区域而不是坐标。如果我增加半径,它会显示两个坐标,但我希望半径是动态的。
      • 你在各自的坐标上添加了大头针吗?
      • 没有实际上 Pins 不存在。我只想要这两个坐标之间的地图。
      • 在这些坐标中添加图钉,并调整地图缩放级别,以便在地图角处调整图钉,然后在获得相应的矩形后移除图钉......!
      猜你喜欢
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      相关资源
      最近更新 更多