【问题标题】:Swift zoom in on location快速放大位置
【发布时间】:2017-07-22 04:39:53
【问题描述】:

我有一个地址,我正在尝试使用该地址显示在带有图钉的地图视图上,而不是实际坐标。我可以将地图与图钉一起显示,但它在我的位置上放大得不够近。无论如何我可以放大该位置吗?在此先感谢

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    let address = (community!["address"] as AnyObject)

    let location : String = address as! String
    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(location) { (placemarks, error) in
        if let placemarks = placemarks {
            if placemarks.count != 0 {
                let annotation = MKPlacemark(placemark: placemarks.first!)
                self.mapView.addAnnotation(annotation)


            }
        }
    }
}

【问题讨论】:

    标签: ios swift mkmapview


    【解决方案1】:

    使用此扩展程序,可以满足您的需求

    extension MKMapView {
      func zoomToUserLocation() {
         self.zoomToUserLocation(latitudinalMeters: 1000, longitudinalMeters: 1000)
      }
    
      func zoomToUserLocation(latitudinalMeters:CLLocationDistance,longitudinalMeters:CLLocationDistance)
      {
        guard let coordinate = userLocation.location?.coordinate else { return }
        self.zoomToLocation(location: coordinate, latitudinalMeters: latitudinalMeters, longitudinalMeters: longitudinalMeters)
      }
    
      func zoomToLocation(location : CLLocationCoordinate2D,latitudinalMeters:CLLocationDistance = 100,longitudinalMeters:CLLocationDistance = 100)
      {
        let region = MKCoordinateRegionMakeWithDistance(location, latitudinalMeters, longitudinalMeters)
        setRegion(region, animated: true)
      }
    
    }
    

    像这样在你的代码中使用

    @IBOutlet weak var mapView: MKMapView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let address = (community!["address"] as AnyObject)
    
        let location : String = address as! String
        let geocoder = CLGeocoder()
        geocoder.geocodeAddressString(location) { (placemarks, error) in
            if let placemarks = placemarks {
                if placemarks.count != 0 {
                    let annotation = MKPlacemark(placemark: placemarks.first!)
                    self.mapView.addAnnotation(annotation)
                    //Using it
                    self.mapView.zoomToLocation(location: annotation.coordinate)
    
                }
            }
        }
    }
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:
      In viewcontoller.m
      
      func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
          if Is_Centered == false {
              Map.setCenter(userLocation.coordinate, animated: true)
              Is_Centered = true
              OperationQueue.mainQueue.addOperation({() -> Void in
                  self.setZoomOnMap(userLocation.coordinate, map: Map)
              })
          }
      }
      
      
      func setZoomOnMap(_ location: CLLocationCoordinate2D, map mapName: MKMapView) {
          let region: MKCoordinateRegion
          let span1: MKCoordinateSpan
          span1.latitudeDelta = 0.002
          // change as per your zoom level
          span1.longitudeDelta = 0.005
          region.span = span1
          region.center = location
          mapName.setRegion(region, animated: true)
          mapName.regionThatFits(region)
          mapName.isZoomEnabled = true
      }
      

      【讨论】:

        【解决方案3】:

        选择您的 mapkit 并进入检查编辑器并单击缩放选项而不是其工作...

        【讨论】:

        • 我想在加载开始时放大位置。我不想让用户手动操作。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-05
        • 2021-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多