2016-12-05 11 views
1

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

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

カスタムロケーションマネージャクラスを使用し、オプションでdidfinishlaunchingにシングルトン関数を呼び出すとのMapViewクラス

1.Makeシングルトンクラス

のためのviewDidLoadでUserDefault .SETカメラ位置の緯度と経度を保存することができますdidfinishlaunchingwithoption

var locationShareInstance:locationManagerClass = locationManagerClass() 

class locationManagerClass: NSObject, CLLocationManagerDelegate, WebServiceDelegate , UIAlertViewDelegate 
{ 
    var locationManager = CLLocationManager() 
    class func sharedLocationManager() -> locationManagerClass 
    { 
     locationShareInstance = locationManagerClass() 
     return locationShareInstance 
    } 

    func startStandardUpdates() { 

     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.activityType = .automotiveNavigation 
     locationManager.distanceFilter = 10 
     locationManager.pausesLocationUpdatesAutomatically = false 

     if (Bundle.main.object(forInfoDictionaryKey: "NSLocationWhenInUseUsageDescription") != nil) { 
      locationManager.requestWhenInUseAuthorization() 
     } 

     locationManager.startUpdatingLocation() 

    } 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     // If it's a relatively recent event, turn off updates to save power. 
     let location: CLLocation = locations.last! 
     let strLocation = "\(location.coordinate.latitude)" 
     if strLocation == "" { 

     }else{ 
     UserDefaults.standard.set("\(location.coordinate.latitude)", forKey: "lat") 
     UserDefaults.standard.set("\(location.coordinate.longitude)", forKey: "long") 
     UserDefaults.standard.synchronize() 
      debugPrint("Spedd: \(location.speed)") 
//  self.updateLocationToServer() 
      self.stopStandardUpdate() 
     } 
    } 

    func stopStandardUpdate(){ 
     locationManager.stopUpdatingLocation() 
    } 


//MARK:- WHEN DENIED 

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
    if status == CLAuthorizationStatus.denied { 

     NSLog("DENIAL") 

     UserDefaults.standard.set("\(0.0)", forKey: "lat") 
     UserDefaults.standard.set("\(0.0)", forKey: "long") 

     self.generateAlertToNotifyUser() 
    } 
} 

func generateAlertToNotifyUser() { 

    if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.notDetermined{ 

     var title: String 
     title = "" 
     let message: String = "Location Services are not able to determine your location" 

     let alertView: UIAlertView = UIAlertView(title: title, message: message, delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Settings") 
     alertView.show() 
    } 


    if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.denied{ 

     var title: String 

     title = "Location services are off" 
     let message: String = "To post spots or find near by spots, you must turn on Location Services from Settings" 

     let alertView: UIAlertView = UIAlertView(title: title, message: message, delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Settings") 
     alertView.show() 
    } 

    if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.notDetermined 
    { 
     startStandardUpdates() 
    } 
} 

}

コールこの機能

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

let locationManager = locationManagerClass.sharedLocationManager() 
     locationManager.startStandardUpdates() 
} 
あなたのクラスの中のviewDidLoad

3.SETカメラ

if UserDefaults.standard.object(forKey: "lat") != nil { 
       let lat = UserDefaults.standard.object(forKey: "lat") as! String 
       let long = UserDefaults.standard.object(forKey: "long") as! String 
       var userLoc = CLLocationCoordinate2D() 
       userLoc.latitude = CDouble(lat)! 
       userLoc.longitude = CDouble(long)! 
       let span = MKCoordinateSpanMake(0.02, 0.02) 
       let region = MKCoordinateRegion(center: userLoc, span: span) 
       mapVw.setRegion(region, animated: true) 
       mapVw.showsUserLocation = true 

      } 
関連する問題