swift - MKLocalSearchRequest giving me weird results -
so ive implemented searchbar maps application , giving me weirds results. if search "oslo" instance, points me specific weird spot north of city , zoomed far in. there must i've done wrong in code here. able spot issue?
// when user submits search query func searchbarsearchbuttonclicked(searchbar: uisearchbar){ // resign first responder , dismiss searchbar. searchbar.resignfirstresponder() dismissviewcontrolleranimated(true, completion: nil) // create , start search request localsearchrequest = mklocalsearchrequest() localsearchrequest.naturallanguagequery = searchbar.text localsearch = mklocalsearch(request: localsearchrequest) localsearch.startwithcompletionhandler { (localsearchresponse, error) -> void in if localsearchresponse == nil{ let alertcontroller = uialertcontroller(title: nil, message: "place not found", preferredstyle: uialertcontrollerstyle.alert) alertcontroller.addaction(uialertaction(title: "dismiss", style: uialertactionstyle.default, handler: nil)) self.presentviewcontroller(alertcontroller, animated: true, completion: nil) return } let center = cllocationcoordinate2d(latitude: localsearchresponse!.boundingregion.center.latitude, longitude: localsearchresponse!.boundingregion.center.longitude) self.updatecurrentmapregion(center, distance: 5000) nsnotificationcenter.defaultcenter().postnotificationname("locationupdate", object: nil, userinfo: ["latitude": center.latitude, "longitude": center.longitude]) } } also, 1 issue i've hardcoded distance 5000 meters when updating map region after getting search result. how can handle in more dynamic manner?
whats alternative using mklocalsearchrequest() in mind sounds should use when looking stuff nearby , not larger entities such cities, etc.
first thing first: mklocalsearch correct api using, though might sound bit weird you're doing!
mklocalsearch gets list of locations, , returns bounding box around locations best fit thinks you're looking for. tried running code , found oslo:
the problem you're experiencing you're using center of bounding box pin location, instead of oslo placemark.
so instead of line of code here:
let center = cllocationcoordinate2d(latitude: localsearchresponse!.boundingregion.center.latitude, longitude: localsearchresponse!.boundingregion.center.longitude) you use:
let center = localsearchresponse?.mapitems.first?.placemark.coordinate (this optional, can choose how handle it. i'll show preferred way in bit.)
in addition, solves problem how far zoom in. simple, can use region's bounding box zoom level. i'm not sure how updatecurrentmapregion:distance method works, mkmapview has convenient setregion:animated method can use (if have access).
mapview.setregion(localsearchresponse!.boundingregion, animated: true) to sum up, if have reference mapview, should able replace completion block , have work:
guard let localsearchresponse = localsearchresponse, mapitem = localsearchresponse.mapitems.first else { let alertcontroller = uialertcontroller(title: nil, message: "place not found", preferredstyle: uialertcontrollerstyle.alert) alertcontroller.addaction(uialertaction(title: "dismiss", style: uialertactionstyle.default, handler: nil)) self.presentviewcontroller(alertcontroller, animated: true, completion: nil) return } let center = mapitem.placemark.coordinate self.mapview.setregion(localsearchresponse.boundingregion, animated: true) nsnotificationcenter.defaultcenter().postnotificationname("locationupdate", object: nil, userinfo: ["latitude": center.latitude, "longitude": center.longitude]) in block, replaced if statement guard set both values, no longer have force unwrap optionals. if can't have access mapview in method, can switch updatecurrentmapregion:center fits better, or math make bounding region fit within values.

Comments
Post a Comment