2017-02-14 15 views
0

私はプログラミングの初心者です。 私は以下のコードを見つけましたが、中心の場所を除いてすべてを行います(ズームイン、マップ、青い点すべての作業)。 シミュレータで(都市を走って)走ると、青い点がページから飛びます。"マップ上の中心位置"をどのようにして作れますか?

import UIKit 
import MapKit 
import CoreLocation 
import Foundation 

class ViewController: UIViewController, CLLocationManagerDelegate { 
    @IBOutlet weak var map: MKMapView! 

    var locationManager: CLLocationManager? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     locationManager = CLLocationManager() 
     locationManager!.delegate = self 

     map.showsUserLocation = true 

     if CLLocationManager.authorizationStatus() == .authorizedWhenInUse { 
      locationManager!.startUpdatingLocation() 
     } else { 
      locationManager!.requestWhenInUseAuthorization() 
     } 
    } 

    private func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
     switch status { 
     case .notDetermined: 
      print("NotDetermined") 
     case .restricted: 
      print("Restricted") 
     case .denied: 
      print("Denied") 
     case .authorizedAlways: 
      print("AuthorizedAlways") 
     case .authorizedWhenInUse: 
      print("AuthorizedWhenInUse") 
      locationManager!.startUpdatingLocation() 
     } 
    } 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let location = locations.first! 
     let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500) 
     map.setRegion(coordinateRegion, animated: true) 
     locationManager?.stopUpdatingLocation() 
     locationManager = nil 
    } 
} 
+0

マップビューで 'userTrackingMode'プロパティを使用するだけではどうですか? – xoudini

+0

ありがとうございますXoudini私はプログラミングの初心者で、userTrackingModeについて何も知らないので、どのように使用するかわかりません。 – Doug

+0

'yourMapView.userTrackingMode = .follow'は、位置情報サービスがオンになっているときにユーザーを自動的にセンタリングします。[documentation](https://developer.apple.com/reference/mapkit/mkmapview/1616208-usertrackingmode)を参照してください。 – xoudini

答えて

0

だけで認証を取得し、map.showsUserLocation = trueを設定し、バックスタンド

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

の実装を削除します。

0

私のSwift 3コードが動作しテストされています。 mapViewを読み込み、ユーザの許可を求め、自分の位置にズームインします。

import UIKit 
import MapKit 

class ViewController: UIViewController, CLLocationManagerDelegate { 
@IBOutlet weak var mapView: MKMapView! 

let locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
    locationManager.activityType = .automotiveNavigation 
    locationManager.distanceFilter = 10.0 
    mapView.showsUserLocation = true 
    mapView.mapType = .standard 
    self.mapView.delegate = self 
} 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.startUpdatingLocation() 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let userLocation:CLLocation = locations[0] as CLLocation 
    locationManager.stopUpdatingLocation() 
    let location = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude) 
    let span = MKCoordinateSpanMake(0.05, 0.05) 
    let region = MKCoordinateRegion (center: location,span: span)  
    mapView.setRegion(region, animated: true) 
} 
} 
+0

ありがとうMohsenエラーが発生しました – Doug

+0

ありがとうMohsenコードをコピー&ペーストするとエラーが発生します。 "ViewController"タイプの値を "MKMapViewDelegate"に割り当てることはできません。 – Doug

+0

MKMapViewDelegateをviewControllerのプロトコルとして追加するか、コードからmapView.delegate = selfを削除してください。 –

関連する問題