Skip to main content
本文から 18 文字削除; タイトルを編集
ソース リンク
kishikawa katsumi
  • 8,520
  • 2
  • 18
  • 40

swift マップの中心を一度だけ現在地にする方法

------swift-------

swift マップの中心を一度だけ現在地にする方法

------swift-------

マップの中心を一度だけ現在地にする方法

コードの編集をしました。
ソース リンク
Harawo
  • 4,599
  • 2
  • 13
  • 17

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

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
    }
}

}

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
}

}

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
    }
}
ソース リンク

swift マップの中心を一度だけ現在地にする方法

------swift-------

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
}

}


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