ios - Trouble at reloading map annotations -
i'm making app should load gas stations , prices on state. map should reload prices if user selects diferent type of gas (magna, premium , diesel). made through uisegment control.
the trouble when reload map doesn't print correct price (the default 1 magna, , when select other type of gas doesn't load new prices).
this code.
class mapviewcontroller: uiviewcontroller,mkmapviewdelegate, cllocationmanagerdelegate,uicollectionviewdelegate, uicollectionviewdatasource { let manager = cllocationmanager() public let smagna = "magna" public let spremium = "premium" public let sdiesel = "diesel" public let min_time: clong = 400 private let min_distance: float = 1000 private var ubicaciones_selected: [ubicacion] = [] private var ubicaciones_magna: [ubicacion] = [] private var ubicaciones_premium: [ubicacion] = [] private var ubicaciones_diesel: [ubicacion] = [] private let request_location = 1 private var latlon: string = "" private var mtype: string = "magna" var ubicaciones:[ubicacion] = [] var ubigaspin = mkpointannotation() @ibaction func maptype(_ sender: any) { if mapa.maptype == mkmaptype.standard{ mapa.maptype = mkmaptype.satellite } else { mapa.maptype = mkmaptype.standard } } override func viewdidload() { super.viewdidload() self.mapa.delegate = self //con esto obtendremos la ubicacion del usuario manager.delegate = self manager.desiredaccuracy = kcllocationaccuracybest manager.requestwheninuseauthorization() mapa.showsuserlocation = true manager.startupdatinglocation() //se cargan los pines y las gasolinas loadgas(tipo: mtype) } func loadgas(tipo:string){ mtype = tipo var ubicaciones:[ubicacion] = [] switch tipo { case smagna: ubicaciones = ubicaciones_magna case spremium: ubicaciones = ubicaciones_premium case sdiesel: ubicaciones = ubicaciones_diesel default: ubicaciones = ubicaciones_magna } if ubicaciones.count == 0 { let url = url(string: "http://192.241.214.56/api/"+tipo+"/?format=json") urlsession.shared.datatask(with: url!, completionhandler: { (data, response, error) in if(error != nil){ print("error") }else{ do{ let ubicaciones_json = try jsonserialization.jsonobject(with: data!, options:.allowfragments) as! [[string : anyobject]] ubicacion in ubicaciones_json{ let nombre:string = ubicacion["nombre"] as! string let direccion:string = ubicacion["direccion"] as! string let precio_magna:float = ubicacion["precio_magna"] as! float let precio_premium:float = ubicacion["precio_premium"] as! float let precio_diesel:float = ubicacion["precio_diesel"] as! float let ubicacion:string = ubicacion["ubicacion"] as! string let p = ubicacion() p.ubicacion = ubicacion p.setlatlng() p.nombre = nombre p.direccion = direccion p.precio_magna = precio_magna p.precio_premium = precio_premium p.precio_diesel = precio_diesel ubicaciones.append(p) } self.ubicaciones = ubicaciones operationqueue.main.addoperation({ self.updatepins(ubicaciones: ubicaciones) }) }catch let error nserror{ print(error) } } }).resume() }else{ self.ubicaciones = ubicaciones self.updatepins(ubicaciones: ubicaciones) } } func updatepins(ubicaciones:[ubicacion]){ mapa.removeannotations(mapa.annotations) ubicacion in ubicaciones{ let anno = custonanno(ubicacion:ubicacion, image: #imageliteral(resourcename: "icon")) anno.coordinate = cllocationcoordinate2d(latitude: ubicacion.latitude!, longitude: ubicacion.longitude!) anno.title=ubicacion.nombre //anno.subtitle="$\(ubicacion.getprecio(tipo: mtype))" self.mapa.addannotation(anno) } } @ibaction func changesegment(_ sender: uisegmentedcontrol) { print(sender.selectedsegmentindex) switch sender.selectedsegmentindex { case 0: loadgas(tipo: smagna) case 1: loadgas(tipo: spremium) case 2: loadgas(tipo: sdiesel) default: loadgas(tipo: smagna) } } }
i think problem ubicaciones vars, have local var , instance var same name, need work time instance var instead of create another, losing information on , on again making network calls needed every time change gas type
try replacing loadgas method
by one
func loadgas(tipo:string){ mtype = tipo switch tipo { case smagna: if(ubicaciones_magna.count > 0){ self.ubicaciones = ubicaciones_magna self.updatepins(ubicaciones: self.ubicaciones) return } case spremium: if(ubicaciones_premium.count > 0){ self.ubicaciones = ubicaciones_premium self.updatepins(ubicaciones: self.ubicaciones) return } case sdiesel: if(ubicaciones_diesel.count > 0){ self.ubicaciones = ubicaciones_diesel self.updatepins(ubicaciones: self.ubicaciones) return } default: if(ubicaciones_magna.count > 0){ self.ubicaciones = ubicaciones_magna self.updatepins(ubicaciones: self.ubicaciones) return } } let url = url(string: "http://192.241.214.56/api/"+tipo+"/?format=json") urlsession.shared.datatask(with: url!, completionhandler: { (data, response, error) in if(error != nil){ print("error") }else{ do{ let ubicaciones_json = try jsonserialization.jsonobject(with: data!, options:.allowfragments) as! [[string : anyobject]] var newubications:[ubicacion] = [] ubicacion in ubicaciones_json{ let nombre:string = ubicacion["nombre"] as! string let direccion:string = ubicacion["direccion"] as! string let precio_magna:float = ubicacion["precio_magna"] as! float let precio_premium:float = ubicacion["precio_premium"] as! float let precio_diesel:float = ubicacion["precio_diesel"] as! float let ubicacion:string = ubicacion["ubicacion"] as! string let p = ubicacion() p.ubicacion = ubicacion p.setlatlng() p.nombre = nombre p.direccion = direccion p.precio_magna = precio_magna p.precio_premium = precio_premium p.precio_diesel = precio_diesel newubications.append(p) } switch tipo { case smagna: self.ubicaciones_magna = newubications self.ubicaciones = self.ubicaciones_magna case spremium: self.ubicaciones_premium = newubications self.ubicaciones = self.ubicaciones_premium case sdiesel: self.ubicaciones_diesel = newubications self.ubicaciones = ubicaciones_diesel default: self.ubicaciones_magna = newubications self.ubicaciones = self.ubicaciones_magna } operationqueue.main.addoperation({ self.updatepins(ubicaciones: self.ubicaciones) }) }catch let error nserror{ print(error) } } }).resume() }
Comments
Post a Comment