将setUserTrackingMode设置为MKUserTrackingModeFollow而不更改缩放级别 [英] setUserTrackingMode to MKUserTrackingModeFollow without changing zoom level

查看:136
本文介绍了将setUserTrackingMode设置为MKUserTrackingModeFollow而不更改缩放级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说:

将跟踪模式设置为MKUserTrackingModeFollow或MKUserTrackingModeFollowWithHeading会导致地图视图将地图定位在该位置的中心并开始跟踪用户的位置.如果将地图缩小,则地图视图会自动放大用户位置,从而有效地更改当前的可见区域.

Setting the tracking mode to MKUserTrackingModeFollow or MKUserTrackingModeFollowWithHeading causes the map view to center the map on that location and begin tracking the user’s location. If the map is zoomed out, the map view automatically zooms in on the user’s location, effectively changing the current visible region.

我的问题是,有没有一种方法可以在设置用户跟踪模式的同时保留地图上的当前缩放级别?

My question, is there a way we can retain current zoom level on map, while setting the user tracking mode?

推荐答案

我能够执行此操作的方法是调用使用用户位置和中心的MKCoordinateRegionMakeWithDistance调用.我用5000作为我的值.这就是我的测试代码. `导入UIKit 导入CoreLocation 导入MapKit

The way i was able to do this was to call a MKCoordinateRegionMakeWithDistance call that uses the users location and center. I used 5000 for my values. This is what my test code looks like. `import UIKit import CoreLocation import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate{

@IBOutlet weak var mapView: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    if (CLLocationManager.locationServicesEnabled()){
        mapView.showsUserLocation = true
        mapView.mapType = MKMapType.satellite
        mapView.setUserTrackingMode(MKUserTrackingMode.followWithHeading, animated: true)
        //locationManager = CLLocationManager()
        //locationManager.delegate = self
        //locationManager.desiredAccuracy = kCLLocationAccuracyBest
        //locationManager.requestAlwaysAuthorization()
        //locationManager.startUpdatingLocation()
    }


}


/*func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    let location = locations.last as! CLLocation

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    //let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
    let region = MKCoordinateRegionMakeWithDistance(center, 500, 500)

    self.mapView.setRegion(region, animated: true)
}*/


@IBAction func centerButton(_ sender: UIButton, forEvent event: UIEvent) {
    let location = MKUserLocation()
    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let region = MKCoordinateRegionMakeWithDistance(center, 5000, 5000)
    self.mapView.setRegion(region, animated: true)
    mapView.setUserTrackingMode(MKUserTrackingMode.followWithHeading, animated: true)

}

} ` 不要阅读任何已注释掉的locationManager信息.这里重要的是要记住,调用setUserTrackingMode不会影响缩放级别,而只会将中心移动到用户位置,因此,如果您使用region和distance方法设置缩放级别,然后再调用setUserTrackingMode,它将假定缩放.这使我每次更新并始终跟踪用户当前位置时,始终可以缩小到合理的缩放级别.

} ` dont read any of the commented out locationManager info. The important thing here is to remember that calling setUserTrackingMode does not affect the zoom level but just moves the center to the users location so if you set a zoom level using the region and distance method, and then call setUserTrackingMode it will assume that zoom. This allows me to always zoom out to a reasonable zoom level each time we recenter and follow the users current location.

这篇关于将setUserTrackingMode设置为MKUserTrackingModeFollow而不更改缩放级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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