【问题标题】:MapKit View not zooming in to users locationMapKit View 未放大到用户位置
【发布时间】:2017-07-29 23:57:40
【问题描述】:

在我的应用程序中,我有一个MKMapKit 视图,当我的应用程序启动时,如果用户允许定位服务,我希望地图放大到用户的位置。我写的代码是:

override func viewDidLoad() {
    super.viewDidLoad()

    mapKitView.delegate = self
    mapKitView.showsUserLocation = true
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.delegate = self

    if (CLLocationManager.locationServicesEnabled()) {

        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
    }

    locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
    }

    DispatchQueue.main.async {
        self.locationManager.startUpdatingLocation()
    }
}


func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    let noLocation = CLLocationCoordinate2D()
    let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200)
    mapKitView.setRegion(viewRegion, animated: false)
}

在我的应用中,它显示了用户的位置,但没有动画和放大。

【问题讨论】:

标签: ios swift


【解决方案1】:

使用用户的位置计算视图区域并将animated参数更改为true

if let location = mapKitView.userLocation.location {
    let viewRegion = MKCoordinateRegionMakeWithDistance(location, 200, 200)
    mapKitView.setRegion(viewRegion, animated: true)
}

【讨论】:

    【解决方案2】:

    在 Swift 3.0 中,在您的 didUpdateLocation 方法中尝试我的代码。

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
        {
            //get location cordinate
            let locValue:CLLocationCoordinate2D = manager.location!.coordinate
            print(locValue)
            //set updating location stop
            locationManager.stopUpdatingLocation()
    
            let location = locations.last! as CLLocation//create object of CLLocation
            //get location cordinate
    
            let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    
            //set map zooming using region
            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
            self.mapView.setRegion(region, animated: true)
    }
    

    我希望这是工作@Rahul Bir

    【讨论】:

      【解决方案3】:

      在 Swift 3.0 中,如果您想在地图视图的起始视图上缩放地图套件,则应使用此代码。

      var zoomIn = false
      var zoomAnnotation:MKAnnotation
      
      func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
          if let annotation = zoomAnnotation where zoomIn == true {
                 let region = MKCoordinateRegion(center: zoomAnnotation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.075, longitudeDelta: 0.075))
                 mapView.setRegion(region, animated: true)
                 zoomIn = false
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-05-02
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多