2017-06-13 11 views
0

Swift 2.1でユーザーの場所をズームインする方法を理解しようとしています。私はインターネットの世界を調査しましたが、解決策を見つけることができませんでした。私はこれを開始助けるためRenier Melianに感謝が、それは場所の許可を要求していないと、エラーメッセージSwiftユーザーの場所をズームインする

を与える
import Foundation 
import MapKit 
import CoreLocation 

class SocialViewController : UIViewController { 

    @IBOutlet weak var mapView: MKMapView! 
    var locationManager = CLLocationManager() 

    override func viewDidLoad() { 
     self.locationManager.requestWhenInUseAuthorization() 
     super.viewDidLoad() 
     self.mapView.showsUserLocation = true 
     self.mapView.userTrackingMode = .Follow 
     self.mapView.delegate = self 
     self.locationManager.startUpdatingLocation() 
     self.locationManager.delegate = self 
    } 
} 

extension SocialViewController : MKMapViewDelegate{ 
    //Adjusting the zoom 
    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { 
     var region = MKCoordinateRegion() 
     region.span = MKCoordinateSpanMake(0.7, 0.7); //Zoom distance 
     let coordinate = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude) 
     region.center = coordinate 
     mapView.setRegion(region, animated: true) 
    } 

    func mapViewWillStartLocatingUser(_ mapView: MKMapView) { 
     debugPrint("startLocating") 
    } 
    func mapViewDidStopLocatingUser(_ mapView: MKMapView) { 
     debugPrint("stopLocating") 
    } 
} 

extension SocialViewController: CLLocationManagerDelegate 
{ 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     debugPrint("received Location") 
    } 
} 

答えて

1

あなたは増やしたり減らすことができますMKCoordinateSpanMakeの値を、このコードを使用して変更してみてくださいズーム

は、あなたのviewDidLoadメソッド

import MapKit 
import CoreLocation 

class SocialViewController : UIViewController { 

@IBOutlet weak var mapView: MKMapView! 
var locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.mapView.showsUserLocation = true 
    self.mapView.userTrackingMode = .follow 
    self.mapView.delegate = self 
    self.locationManager.requestWhenInUseAuthorization() 
    self.locationManager.startUpdatingLocation() 
    self.locationManager.delegate = self 
} 
} 

extension SocialViewController : MKMapViewDelegate{ 
    //Adjusting the zoom 
    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { 
     var region = MKCoordinateRegion() 
     region.span = MKCoordinateSpanMake(0.7, 0.7); //Zoom distance 
     let coordinate = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude) 
     region.center = coordinate 
     mapView.setRegion(region, animated: true) 
    } 

    func mapViewWillStartLocatingUser(_ mapView: MKMapView) { 
     debugPrint("startLocating") 
    } 
    func mapViewDidStopLocatingUser(_ mapView: MKMapView) { 
     debugPrint("stopLocating") 
    } 
} 

extension SocialViewController: CLLocationManagerDelegate 
{ 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     debugPrint("received Location") 
    } 

}

あなたの中のプライバシーキーを追加でこの行を追加します。 Info.plistの

enter image description here

WORKING!

enter image description here

希望これは私がこれをしようとすると、それは `入力するタイプ「SocialViewController」の値を割り当てることができませんと言う

+0

に役立つ「MKMapViewDelgateを?」' – salipshitz

+0

再び私の答えをチェックし@salipshitz、あなたが作っ –

+0

編集されました'MKMapViewDelegate'の代わりに' MKMapViewDelgate'を書いたときのタイプミスです – salipshitz

関連する問題