2016-12-16 6 views
10

ユーザーの場所をマップに表示するだけですが、アプリの起動時にマップを現在の場所にズームする必要がありますが、マップがズームしない理由はわかりませんすべてのそれはこのようなものだ:ここでMapKitユーザーの現在地にズームする

enter image description here

はコードです:

class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 

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

    override func viewDidLoad() { 
     super.viewDidLoad() 

     mapView.delegate = self 
     mapView.showsUserLocation = true 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.requestWhenInUseAuthorization() 
     locationManager.delegate = self 
     DispatchQueue.main.async { 
      self.locationManager.startUpdatingLocation() 
     } 
    } 

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
     let location = locations.last as! CLLocation 
     let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 
     var region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)) 
     region.center = mapView.userLocation.coordinate 
     mapView.setRegion(region, animated: true) 
    } 
+0

削除 'region.center = mapView.userLocation.coordinate' – shallowThought

+0

@shallowThought私がしたけど何もlocationManagerは、それが呼び出された最初の時間が初期化されていないので、このコードスニペットが失敗した –

答えて

11

私は同様の問題に直面し、間違っていただきまし考える4日間を無駄にしました。最後にviewDidLoadメソッドのコードのこれらの行を作成して解決:ViewDidLoad方法で

//Zoom to user location 
    let noLocation = CLLocationCoordinate2D() 
    let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200) 
    mapView.setRegion(viewRegion, animated: false) 

これらの新しい変更のコードを追加します。

override func viewDidLoad() { 
     super.viewDidLoad() 

     mapView.delegate = self 
     mapView.showsUserLocation = true 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.delegate = self 

     //Check for Location Services 
     if (CLLocationManager.locationServicesEnabled()) { 
      locationManager = CLLocationManager() 
      locationManager.delegate = self 
      locationManager.desiredAccuracy = kCLLocationAccuracyBest 
      locationManager.requestAlwaysAuthorization() 
      locationManager.requestWhenInUseAuthorization() 
     } 
     locationManager.requestWhenInUseAuthorization() 
     if CLLocationManager.locationServicesEnabled() { 
      locationManager.startUpdatingLocation() 
     } 
     //Zoom to user location 
     let noLocation = CLLocationCoordinate2D() 
     let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200) 
     mapView.setRegion(viewRegion, animated: false) 

     DispatchQueue.main.async { 
      self.locationManager.startUpdatingLocation() 
     } 
} 

・ホープこれはあなたの問題を解決するのに役立ちます。それ以上の問題があれば、コメントを投稿してください。ありがとう

+0

変化しません。リモートの行: locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.delegate = self – Gadzair

+1

最初に質問を読んでから、 –

+0

と答えてください。なぜ 'locationManager.requestWhenInUseAuthorization()'が重複していますか? – Antek

11

これはSwift 3、XCode 8.2の別のアプローチです。ズームイン変数で指定された場所でアプリを起動しますviewDidLoad()

mapView.showsUserLocation = true 
centerMapOnLocation(location: homeLocation) 

これにで呼び出し、その後

let homeLocation = CLLocation(latitude: 37.6213, longitude: -122.3790) 
let regionRadius: CLLocationDistance = 200 
func centerMapOnLocation(location: CLLocation) 
{ 
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 
              regionRadius * 2.0, regionRadius * 2.0) 
    mapView.setRegion(coordinateRegion, animated: true) 
} 

+2

'homeLocation'がコードによって自動的に見つけられるなら、もっと役に立ちます。 – nyxee

1

コード:まず、ヘルパー関数を書き出す。

import UIKit 
import MapKit 

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 

@IBOutlet weak var mapview: MKMapView! 

let locationmanager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    mapview.mapType = MKMapType.standard 

    let location = CLLocationCoordinate2DMake(22.4651, 70.0771) 

    let span = MKCoordinateSpanMake(0.5, 0.5) 
    let region = MKCoordinateRegionMake(location, span) 
    mapview.setRegion(region, animated: true) 

    let annonation = MKPointAnnotation() 
    annonation.coordinate = location 
    annonation.title = "Chandi Bazar" 
    annonation.subtitle = "Jamnagar" 
    // 
    mapview.addAnnotation(annonation) 

    self.locationmanager.requestWhenInUseAuthorization() 

    if CLLocationManager.locationServicesEnabled() 
    { 
     locationmanager.delegate = self 
     locationmanager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
     locationmanager.startUpdatingLocation() 
    } 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
{ 
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate 
    print("locations = \(locValue.latitude) \(locValue.longitude)") 

    locationmanager.stopUpdatingLocation() 
} 

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 
{ 
    if (annotation is MKUserLocation) 
    { 
     return nil 
    } 

    let annotationidentifier = "Annotationidentifier" 

    var annotationview:MKAnnotationView 
    annotationview = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationidentifier) 

    let btn = UIButton(type: .detailDisclosure) 

    btn.addTarget(self, action: #selector(ViewController.hirenagravat(sender:)), for: .touchUpInside) 

    annotationview.rightCalloutAccessoryView = btn 

    annotationview.image = UIImage(named: "images (4).jpeg") 

    annotationview.canShowCallout = true 

    return annotationview 
} 

func hirenagravat(sender:UIButton) 
{ 
    let fvc = storyboard?.instantiateViewController(withIdentifier: "secondViewController") as? secondViewController 
    self.navigationController?.pushViewController(fvc!, animated: true) 
} 
0

MKMapViewDelegate func:

var isInitiallyZoomedToUserLocation: Bool = false 

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { 
    if !isInitiallyZoomedToUserLocation { 
     isInitiallyZoomedToUserLocation = true 
     mapView.showAnnotations([userLocation], animated: true) 
    } 
} 
0

リージョンを設定すると、マップをもうズームできなくなります。下記修正すること

func yourFuncName() { 
//this is global var 
regionHasBeenCentered = false 
if !self.regionHasBeenCentered { 
    let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01) 
    let userLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(_cllocationOfUserCurrentLocation!.coordinate.latitude, _cllocationOfUserCurrentLocation!.coordinate.longitude) 
    let region: MKCoordinateRegion = MKCoordinateRegionMake(userLocation, span) 

    self.mapView.setRegion(region, animated: true) 
    self.regionHasBeenCentered = true 
    } 

    self.mapView.showsUserLocation = true 
} 
関連する問題