2017-04-14 13 views
0

私はこの解決策を絶え間なく探し求めていましたが、ここでStackoverflowの専門知識に頼る以外は選択肢がありません。私は、アプリケーションが終了し、私は注釈を保存するためにUserDefaultを使用している場合マップの注釈を保存する作業です。UserDefaultsを使用してマップの注釈を保存する

これはObjective Cのコードであり、Swiftに変換しようとしましたが、エラーがあると思います。私はあまりよく分からない。私は、これは、注釈

var pinnedAnnotation: CLLocationCoordinate2D = (parkedCarAnnotation?.coordinate)! 
    var coordinateData: NSData = NSData(bytesNoCopy: pinnedAnnotation, length: sizeof(pinnedAnnotation), freeWhenDone: false) 
    UserDefaults.standard.set(coordinateData, forKey: pinnedAnnotation) 
    UserDefaults.standard.synchronize() 

を保存したときにアプリが開いて、私は負荷アノテーションを必要とされ

)(のviewDidLoadでこのコードを配置します。

viewDidLoadを配置するのが適切かどうかはわかりません。私が何を全くわからないんだけど

override func viewDidLoad() { 
    super.viewDidLoad() 
    mapView.delegate = self as MKMapViewDelegate 
    checkLocationAuthorizationStatus() 
    tabBar.delegate = self 

    if UserDefaults.standard.object(forKey: "pinnedAnnotation") != nil { 
     let annotation = MKPointAnnotation() 

     self.mapView.addAnnotation(annotation) 
    } 

} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let pinnedAnnotation: CLLocationCoordinate2D = (parkedCarAnnotation?.coordinate)! 
    let locationData = ["latitude": parkedCarAnnotation?.coordinate.latitude, "longitude": parkedCarAnnotation?.coordinate.longitude] 
    UserDefaults.standard.set(locationData, forKey: "pinnedAnnotation") 
    UserDefaults.standard.synchronize() 
    print("Saving data ", UserDefaults.standard.set(locationData, forKey: "pinnedAnnotation")) 

} 

func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { 
    if(item.tag == 0){ 
     if mapView.annotations.count == 1{ 
      mapView.addAnnotation(parkedCarAnnotation!) 

     } else { 
      mapView.removeAnnotation(mapView.annotations as! MKAnnotation) 
     } 
    } 

extension ViewController: MKMapViewDelegate { 
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if let annotation = annotation as? ParkingSpot{ 
     let identifier = "pin" 
     var view: MKPinAnnotationView 
     view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
     view.canShowCallout = true 
     view.animatesDrop = true 
     view.pinTintColor = UIColor.orange 
     view.calloutOffset = CGPoint(x: -8, y: -3) 
     view.rightCalloutAccessoryView = UIButton.init(type:.detailDisclosure) as UIView 
     return view 
    } else { 
     return nil 
    } 
} 

extension ViewController: CLLocationManagerDelegate{ 
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { 
    centerMapOnLocation(location: CLLocation(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)) 
    let locationServiceCoordinate = LocationService.instance.locationManager.location!.coordinate 

    parkedCarAnnotation = ParkingSpot(title: "My Parking Spot", locationName: "Find your way back to your car location", coordinate: CLLocationCoordinate2D(latitude: locationServiceCoordinate.latitude, longitude: locationServiceCoordinate.longitude)) 


} 

}

答えて

0

を修正するために必要な、私がやっていることのさらなる明確化のためのコードを追加しました:以前私が編集したupdatingLocation

のMapViewの機能でそれを置きますあなたが文字列であることをUserDefaultsにキーのニーズをデータを保存しているときに、私はまた、あなたがDictionaryとしてUserDefaultsにあなたのデータを保存する必要がありますと信じて、...手始めに

を求めています

let locationData = ["lat": parkedCarAnnotation?.coordinate?.latitude, "long": parkedCarAnnotation?.coordinate.longitude] 
UserDefaults.standard.set(locationData, forKey: "pinned_annotation") 
そしてあなたがうまくいけば、私はあなたがちょっと私の質問に答えると思います

+0

座標を使用して注釈を設定することができるはずですあなたは今

if let annotationData = UserDefaults.standard.object(forKey: "pinned_annotation") as? Dictionary { guard let lat = annotationData["lat"], let long = annotationData["long"] else { return } let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long) } 

を呼ぶだろうデータが、私の残りの半分を取得します質問は、コードをどこに置くのですか?私はmapViewを使用しており、十分な機能があります。私はそれをfunc locationManager(_ manager:CLLocationManager、didUpdateLocations locations:[CLLocation])に埋め込むように言われました。 –

+0

あなたは、ユーザの現在の場所を取得する必要がある場合にのみ使用する必要があります。そうであれば、その機能にデータを保存して、ユーザーがその場所を更新するたびにデータを保存します。アノテーションを設定するのは、あなたのUIを設定した場所に置きます。 –

+0

上記のコードを更新しました。何が間違っているのか教えてください。 –

関連する問題