swift - Save map annotation using UserDefaults -
i have been searching relentlessly solution , left no choice except rely on expertise here on stackoverflow. working on saving map annotation if app closes , have been using userdefault save annotation.
this objective c code found , tried converting swift , think there error it. not sure. place code @ viewdidload()
this save annotation
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()
and needed load annotation when app open.
i dont know if viewdidload right place put. put in mapview function of updatinglocation
edited: added code further clarification of have done needed corrected
override func viewdidload() { super.viewdidload() mapview.delegate = self 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) 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 way car location", coordinate: cllocationcoordinate2d(latitude: locationservicecoordinate.latitude, longitude: locationservicecoordinate.longitude)) }
}
i'm not entirely sure you're asking...
for starters when you're saving data userdefaults
key needs string, believe you'll need save data in userdefaults
dictionary
let locationdata = ["lat": parkedcarannotation?.coordinate?.latitude, "long": parkedcarannotation?.coordinate.longitude] userdefaults.standard.set(locationdata, forkey: "pinned_annotation")
and retrieve data call
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) }
now should able set annotation coordinate
Comments
Post a Comment