Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift地图套件-远离用户位置_Ios_Xcode_Swift_Mapkit - Fatal编程技术网

Ios Swift地图套件-远离用户位置

Ios Swift地图套件-远离用户位置,ios,xcode,swift,mapkit,Ios,Xcode,Swift,Mapkit,我想显示用户位置和周围区域,但我也想允许用户在该区域周围平移。现在,如果我尝试在地图上的其他地方滚动,它会自动将我带回到基本区域,用户位于中心。我该怎么阻止这一切?我想在用户处于中间的情况下显示初始视图,但我也希望能够四处滚动。提前谢谢你们的帮助 导入UIKit 导入地图套件 导入核心定位 类ViewControllerMain:UIViewController、MKMapViewDelegate、CLLocationManagerDelegate{ @IBOutlet weak var map

我想显示用户位置和周围区域,但我也想允许用户在该区域周围平移。现在,如果我尝试在地图上的其他地方滚动,它会自动将我带回到基本区域,用户位于中心。我该怎么阻止这一切?我想在用户处于中间的情况下显示初始视图,但我也希望能够四处滚动。提前谢谢你们的帮助

导入UIKit 导入地图套件 导入核心定位

类ViewControllerMain:UIViewController、MKMapViewDelegate、CLLocationManagerDelegate{

@IBOutlet weak var mapView: MKMapView!

var locationManager:CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager = CLLocationManager()
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.delegate = self
    locationManager.startUpdatingLocation()
    mapView.showsUserLocation = true
    mapView.delegate = self

    let longPress = UILongPressGestureRecognizer(target: self, action: "action:")
    longPress.minimumPressDuration = 1.0
    mapView.addGestureRecognizer(longPress)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    let regionToZoom = MKCoordinateRegionMake(manager.location.coordinate, MKCoordinateSpanMake(0.01, 0.01))
    mapView.setRegion(regionToZoom, animated: true)
}

您在didUpdateLocations中的代码正在重置区域。您有两个选项

  • 无论您是否已经设置了第一个位置,都将其存储在ivar中。只有在尚未设置的情况下,才可以设置区域

  • 设置一个运行15秒的计时器。如果用户移动了地图,则重置计时器。计时器过期后,可以重新居中到用户位置

  • 这将使地图以用户为中心,但使他们能够稍微平移一点以获得一些上下文

    如果您仍在寻找Swift和Obj C中的

    ,这就是locationManager fun结尾部分所需的全部内容。如果您使用Swift,请务必查找我链接到的答案上的注释

    设置好后,在didUpdateLocations中使用控制流,以便仅当用户未触摸地图时,它才重新调整用户位置的中心

    以下是我的完整代码示例:

    @IBOutlet weak var theMap: MKMapView!
    
    // ... 
    
    
    // This var and the three following functions are used to tell if the map moves because of the user. 
    // This is used in the control flow in didUpdateLocations
    
    private var mapChangedFromUserInteraction = false
    
    
    private func mapViewRegionDidChangeFromUserInteraction() -> Bool {
        let view: UIView = self.theMap.subviews[0] as UIView
        //  Look through gesture recognizers to determine whether this region change is from user interaction
        if let gestureRecognizers = view.gestureRecognizers {
            for recognizer in gestureRecognizers {
                if( recognizer.state == UIGestureRecognizerState.Began || recognizer.state == UIGestureRecognizerState.Ended ) {
                    return true
                }
            }
        }
        return false
    }
    
    func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
        mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()
        if (mapChangedFromUserInteraction) {
            // user changed map region
            println("user changed map region")
    
    
        }
    }
    
    func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        if (mapChangedFromUserInteraction) {
            // user changed map region
    
            println("user changed map region")
    
    
        }
    }
    
    // This function is called each time the user moves.  
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    
    
    
    
    // Use Control Flow: if the user has moved the map, then don't re-center.
    // NOTE: this is using 'mapChangedFromUserInteraction' from above. 
    
        if mapChangedFromUserInteraction == true {
    
            // do nothing, because the user has moved the map.
    
        }
    
        else {
    
            // update on location to re-center on the user.
    
            // set X and Y distances for the span (zoom). This is very zoomed in.
            let spanX = 0.0005
            let spanY = 0.0005
    
            // Create a region using the user's location, and the zoo. 
            var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
    
            // set the map to the new region
            theMap.setRegion(newRegion, animated: true)
    
            }
    
    }
    

    你能举一个例子说明这两种选择吗?我理解这个概念。从语法上说,我做了一些不正确的事情
    @IBOutlet weak var theMap: MKMapView!
    
    // ... 
    
    
    // This var and the three following functions are used to tell if the map moves because of the user. 
    // This is used in the control flow in didUpdateLocations
    
    private var mapChangedFromUserInteraction = false
    
    
    private func mapViewRegionDidChangeFromUserInteraction() -> Bool {
        let view: UIView = self.theMap.subviews[0] as UIView
        //  Look through gesture recognizers to determine whether this region change is from user interaction
        if let gestureRecognizers = view.gestureRecognizers {
            for recognizer in gestureRecognizers {
                if( recognizer.state == UIGestureRecognizerState.Began || recognizer.state == UIGestureRecognizerState.Ended ) {
                    return true
                }
            }
        }
        return false
    }
    
    func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
        mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()
        if (mapChangedFromUserInteraction) {
            // user changed map region
            println("user changed map region")
    
    
        }
    }
    
    func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        if (mapChangedFromUserInteraction) {
            // user changed map region
    
            println("user changed map region")
    
    
        }
    }
    
    // This function is called each time the user moves.  
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    
    
    
    
    // Use Control Flow: if the user has moved the map, then don't re-center.
    // NOTE: this is using 'mapChangedFromUserInteraction' from above. 
    
        if mapChangedFromUserInteraction == true {
    
            // do nothing, because the user has moved the map.
    
        }
    
        else {
    
            // update on location to re-center on the user.
    
            // set X and Y distances for the span (zoom). This is very zoomed in.
            let spanX = 0.0005
            let spanY = 0.0005
    
            // Create a region using the user's location, and the zoo. 
            var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
    
            // set the map to the new region
            theMap.setRegion(newRegion, animated: true)
    
            }
    
    }