
[code]

    import UIKit
    import CoreLocation
    import MapKit
    
    class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    
    
        @IBOutlet weak var theMap: MKMapView!
    
        //@IBOutlet weak var theLabel: UILabel!
    
        var manager:CLLocationManager!
        var myLocations: [CLLocation] = []
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            //Setup our Location Manager
            manager = CLLocationManager()
            manager.delegate = self
            manager.desiredAccuracy = kCLLocationAccuracyBest
            manager.requestAlwaysAuthorization()
            manager.startUpdatingLocation()
    
            //Setup our Map View
            theMap.delegate = self
            theMap.mapType = MKMapType.Standard
            theMap.showsUserLocation = true
        }
    
        func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
            //theLabel.text = "\(locations[0])"
            myLocations.append(locations[0] as! CLLocation)
    
            let spanX = 0.001
            let spanY = 0.001
            var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
            theMap.setRegion(newRegion, animated: true)
    
            if (myLocations.count > 1){
                var sourceIndex = myLocations.count - 1
                var destinationIndex = myLocations.count - 2
    
                let c1 = myLocations[sourceIndex].coordinate
                let c2 = myLocations[destinationIndex].coordinate
                var a = [c1, c2]
                var polyline = MKPolyline(coordinates: &a, count: a.count)
                theMap.addOverlay(polyline)
    
                //theMap.setUserTrackingMode(MKUserTrackingMode.FollowWithHeading, animated: true)
            }
        }
    
        func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    
            if overlay is MKPolyline {
                var polylineRenderer = MKPolylineRenderer(overlay: overlay)
                polylineRenderer.strokeColor = UIColor.blueColor()
                polylineRenderer.lineWidth = 4
                return polylineRenderer
            }
            return nil
        }
    }
    
[/code]

* * *

上記のプログラムですが、中心がずっと現在地であるため画面を大きくすると、中心にしようとするためか画面の拡大ができません。  
マップの中心に１度だけ現在地が表示されれば、あとは自由に拡大、縮小できるように変えたいと思っています。  
わかる方がいましたら教えてください。


CC BY-SA 3.0
2015-08-21T10:02:34.510
14701
2016-06-06T11:53:18.920
2015-08-22T05:09:47.453
5519
11014
question
swift xcode
マップの中心を一度だけ現在地にする方法


`didUpdateLocations`で

[code]

    manager.stopUpdatingLocation()
    
[/code]

を実行してGPSを止めればいいのではないでしょうか。


CC BY-SA 3.0
2015-08-21T10:57:01.383
14705
2015-08-21T10:57:01.383
5750
14701
answer

[code]

    locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject])
    
[/code]

の中で

[code]

    theMap.userTrackingMode = MKUserTrackingModeNone
    
[/code]

を入れると解決するんじゃないでしょうか？


CC BY-SA 3.0
2015-08-22T03:30:04.583
14720
2015-08-22T03:30:04.583
10708
14701
answer

[code]

        var regionout:Bool = false
    
        func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
        if(var regionout:Bool == false){        
        //theLabel.text = "\(locations[0])"
                myLocations.append(locations[0] as! CLLocation)
    
                let spanX = 0.001
                let spanY = 0.001
                var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
                theMap.setRegion(newRegion, animated: true)
    
                if (myLocations.count > 1){
                    var sourceIndex = myLocations.count - 1
                    var destinationIndex = myLocations.count - 2
    
                    let c1 = myLocations[sourceIndex].coordinate
                    let c2 = myLocations[destinationIndex].coordinate
                    var a = [c1, c2]
                    var polyline = MKPolyline(coordinates: &a, count: a.count)
                    theMap.addOverlay(polyline)
    
                    //theMap.setUserTrackingMode(MKUserTrackingMode.FollowWithHeading, 　　　animated: true)
                }
        regionout = true 
        }
    }
    
[/code]


CC BY-SA 3.0
2016-06-06T10:15:41.163
26504
2016-06-06T11:53:18.920
2016-06-06T11:53:18.920
8044
14563
14701
answer