2016-12-17 5 views
0

現在、私のコードはユーザーの現在地にピンをドロップします。ビューを元の位置に戻し、その現在の位置ピンの周りに中心を置くため、地図を移動しようとすると小さな問題が1つあります。ユーザーがマップをナビゲートして移動できるようにし、ユーザーがビューコントローラーを切り替えて(別のタブに移動して)戻ってくると、マップはユーザーの位置ピンの中心に配置されます。私はこれを行うためにこのコードを修正しようとしていましたが、どこから始めるのか幸運はありませんでした。Swift MapViewはユーザーの現在地周辺に固定されています

あなたはグローバルスコープで変数newPinを宣言し、グローバルnewPinが使用されていないので、mapLongPress(...)でローカルに新しい変数を宣言let newPin = ...

import UIKit 
import MapKit 
import CoreLocation 
let newPin = MKPointAnnotation() 

class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 



@IBOutlet weak var map: MKMapView! 

let locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // User's location 

    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    if #available(iOS 8.0, *) { 
    locationManager.requestAlwaysAuthorization() 
    } else { 
    // Fallback on earlier versions 
    } 
    locationManager.startUpdatingLocation() 


    // add gesture recognizer 
    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(MapVC.mapLongPress(_:))) // colon needs to pass through info 
    longPress.minimumPressDuration = 1.5 // in seconds 
    //add gesture recognition 
    map.addGestureRecognizer(longPress) 
} 

// func called when gesture recognizer detects a long press 

func mapLongPress(_ recognizer: UIGestureRecognizer) { 

    print("A long press has been detected.") 

    let touchedAt = recognizer.location(in: self.map) // adds the location on the view it was pressed 
    let touchedAtCoordinate : CLLocationCoordinate2D = map.convert(touchedAt, toCoordinateFrom: self.map) // will get coordinates 

    let newPin = MKPointAnnotation() 
    newPin.coordinate = touchedAtCoordinate 
    map.addAnnotation(newPin) 


} 

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


    map.removeAnnotation(newPin) 

    let location = locations.last! as CLLocation 

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) 

    //set region on the map 
    map.setRegion(region, animated: true) 

    newPin.coordinate = location.coordinate 
    map.addAnnotation(newPin) 





} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


} 

答えて

1

あなたのコードでは、いくつかの問題があります。

didUpdateLocations()では、最初に(グローバル)newPin注釈を削除して(なぜ?)、機能の最後にもう一度設定します。グローバルnewPinは何も役に立つものに決して設定されていなかったので、これは決して望ましい結果を得られません。

さらに、didUpdateLocations()では、地図の地域と中心点を現在の場所に設定します。これはすべての場所の更新時に行われ、地図をパンしようとすると奇妙な結果が得られます。ビューが表示されたら、中央と地域を設定するには

、そのような何かしてみてください:私はmapLongPressからローカル変数newPinが冗長で見る

class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 
    @IBOutlet weak var map: MKMapView! 
    let locationManager = CLLocationManager() 

    // class variable for the current location 
    var lastLocation: CLLocation? 

    override func viewDidLoad() { 
     // ... 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     if self.lastLocation != nil { 
      // set center and region to current location 
      let center = CLLocationCoordinate2D(latitude: self.lastLocation.coordinate.latitude, longitude: self.lastLocation.coordinate.longitude) 
      let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) 

      //set region on the map 
      map.setRegion(region, animated: true) 
     } 
    } 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     self.lastLocation = locations.last 
    } 

} 
+0

ああ。 didUpdateLocations()に関しては、私は絶えず更新していたことに気づいていませんでした。ビューコントローラを開いたときに更新するにはどうしたらいいですか?ユーザーはマップをパンすることができますか? – Kevin

+0

最後に取得した場所をクラス変数に保存し、 'ViewDidAppear'に領域と中心点を設定することができます – zisoft

+0

これは素朴に聞こえるかもしれませんが、私に負担してください。関数didUpdateLocations()の外で場所(または地域/中心点)を保存できるように、クラス変数を設定するにはどうすればよいですか?私はグローバル変数を使ってこれを行う方法しか知りません。 – Kevin