ios - Storing CLLocation and keeping annotation -
my app has 3 tabs, tab pinning location, , detailview of pinned location on second tab. trying save location of pin nsuserdefaults. location stay pinned upon reloading app, , therefore detail view still display detail view of pinned location. here have far,
func locationmanager(manager: cllocationmanager!, didupdatelocations locations: [anyobject]!) { // find location of user var userlocation:cllocation = locations[0] as! cllocation var latitude = userlocation.coordinate.latitude var longitude = userlocation.coordinate.longitude var latdelta:cllocationdegrees = 0.01 var longdelta: cllocationdegrees = 0.01 var span: mkcoordinatespan = mkcoordinatespanmake(latdelta, longdelta) var location:mkuserlocation = currentlocation; var region: mkcoordinateregion = mkcoordinateregionmake(location.coordinate, span) var coordinate:cllocationcoordinate2d = cllocationcoordinate2dmake(latitude, longitude); carinitiallocation = userlocation; let locationdata = nskeyedarchiver.archiveddatawithrootobject(carinitiallocation); nsuserdefaults.standarduserdefaults().setobject(locationdata, forkey: "locationdata"); carinitialcoordinate = coordinate; self.map.setregion(region, animated: true); } override func viewdidload() { super.viewdidload() if let loadeddata = nsuserdefaults.standarduserdefaults().dataforkey("locationdata") { if let loadedlocation = nskeyedunarchiver.unarchiveobjectwithdata(loadeddata) as? cllocation { println(loadedlocation.coordinate.latitude); println(loadedlocation.coordinate.longitude); var annotation = mkpointannotation() annotation.coordinate = loadedlocation.coordinate annotation.title = title self.map.addannotation(annotation) } } map.addannotations(artworks) map.delegate = self; manager.delegate = self; manager.desiredaccuracy = kcllocationaccuracybest; manager.requestwheninuseauthorization(); manager.startupdatinglocation(); self.map.showsuserlocation = true; currentlocation = map.userlocation; }
i want pinlocation button deactivated once user has pinned location once. try so:
@ibaction func pinlocationbutton(sender: anyobject) { // add location array, can retrieved , put temporary storage //places.append(["name":title,"lat":"\(newcoordinate.latitude)","lon":"\(newcoordinate.longitude)"]) if let loadeddata = nsuserdefaults.standarduserdefaults().dataforkey("locationdata") { if let loadedlocation = nskeyedunarchiver.unarchiveobjectwithdata(loadeddata) as? cllocation { println(loadedlocation.coordinate.latitude); println(loadedlocation.coordinate.longitude); pinlocationbutton.enabled = false; } } var location = carinitiallocation var coordinate = carinitialcoordinate clgeocoder().reversegeocodelocation(manager.location, completionhandler: { (placemarks, error) -> void in var title = "" if (error == nil) { if let p = clplacemark(placemark: placemarks?[0] as! clplacemark) { var subthoroughfare:string = "" var thoroughfare:string = "" if p.subthoroughfare != nil { subthoroughfare = p.subthoroughfare } if p.thoroughfare != nil { thoroughfare = p.thoroughfare } completeaddress = self.displaylocationinfo(p); title = "\(subthoroughfare) \(thoroughfare)" } } // annotation, i.e pins var annotation = mkpointannotation() annotation.coordinate = coordinate annotation.title = title self.map.addannotation(annotation) // nsuserdefaults.standarduserdefaults().setobject(places, forkey: "places") }) }
then, in detailvc attempt reverse geocode pinned location, defaulting current location.. don't understand why here's code:
super.viewdidload() addresslabel.font = uifont(name: addresslabel.font.fontname, size: 18) smallmapview.delegate = self; locationmanager.delegate = self; smallmapview.maptype = mkmaptype.hybrid; locationmanager.desiredaccuracy = kcllocationaccuracybest; locationmanager.requestwheninuseauthorization(); locationmanager.startupdatinglocation(); smallmapview.zoomenabled = true; smallmapview.rotateenabled = true; if let loadeddata = nsuserdefaults.standarduserdefaults().dataforkey("locationdata") { if let loadedlocation = nskeyedunarchiver.unarchiveobjectwithdata(loadeddata) as? cllocation { println(loadedlocation.coordinate.latitude); println(loadedlocation.coordinate.longitude); clgeocoder().reversegeocodelocation(loadedlocation, completionhandler: { (placemarks, error) -> void in var title = ""; var subtitle = ""; var locality = ""; if(error == nil) { if let placemark = clplacemark(placemark: placemarks?[0] as! clplacemark) { var subthoroughfare:string = ""; var thoroughfare:string = ""; var locality:string = ""; var postalcode:string = ""; var administrativearea:string = ""; var country:string = ""; if (placemark.subthoroughfare != nil) { subthoroughfare = placemark.subthoroughfare; } if(placemark.thoroughfare != nil) { thoroughfare = placemark.thoroughfare; } if(placemark.locality != nil) { locality = placemark.locality; } if(placemark.postalcode != nil) { postalcode = placemark.postalcode; } if(placemark.administrativearea != nil) { administrativearea = placemark.administrativearea; } if(placemark.country != nil) { country = placemark.country; } println("viewcontroller placmark data:"); println(locality); println(postalcode); println(administrativearea); println(country); title = " \(subthoroughfare) \(thoroughfare) \n \(locality), \(administrativearea) \n \(postalcode) \(country)"; subtitle = " \(subthoroughfare) \(thoroughfare)"; println(title); self.addresslabel.text = title; } } var latitude = loadedlocation.coordinate.latitude; var longitude = loadedlocation.coordinate.longitude; var latdelta:cllocationdegrees = 0.001; var longdelta:cllocationdegrees = 0.001; var span: mkcoordinatespan = mkcoordinatespanmake(latdelta, longdelta); var overallloc = cllocationcoordinate2dmake(latitude, longitude); var region:mkcoordinateregion = mkcoordinateregionmake(overallloc, span); var annotation = mkpointannotation(); annotation.coordinate = loadedlocation.coordinate; annotation.title = subtitle; self.smallmapview.addannotation(annotation); self.smallmapview.setregion(region, animated: true) }) } } }
in pinlocationbutton function, use manager.location wrong, should use
clgeocoder().reversegeocodelocation(location, completionhandler: { (placemarks, error) -> void in var title = "" if (error == nil) { // ....
Comments
Post a Comment