ios - MKLocalSearch WITHOUT MKMapView -
i use mklocalsearch display results pre-defined string results relevant users position, examples have seen far require or use mkmapview set users location, , perhaps use search bar gather text needed search.
i perform search on predefined string , load results in tableview, without first having map, there example of how this?
editing add more detail, including code trying use. code not produce table of results.
further edit: anna below has pointed out problem might in uisearchdisplaycontroller, have ripped current code straight out of working example project, can't see things going wrong or why uisearchdisplaycontroller not showing results.
header file:
#import <uikit/uikit.h> #import <mapkit/mapkit.h> #import <corelocation/corelocation.h> @interface callacabviewcontroller : uiviewcontroller <cllocationmanagerdelegate, uitableviewdatasource, uitableviewdelegate, uisearchdisplaydelegate> { cllocationmanager *locationmanager; mklocalsearch *localsearch; mklocalsearchresponse *results; } -(ibaction)closebuttonclicked:(id)sender; @end
implementation file:
- (void)viewdidload { [super viewdidload]; locationmanager = [[cllocationmanager alloc] init]; locationmanager.delegate = self; [self.searchdisplaycontroller setdelegate:self]; locationmanager.distancefilter = kcldistancefilternone; locationmanager.desiredaccuracy = kcllocationaccuracybest; [locationmanager startupdatinglocation]; } #pragma mark - locationupdating -(void)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray *)locations { cllocation *newlocation = [locations lastobject]; cllocation *oldlocation; if (locations.count > 1) { oldlocation = [locations objectatindex:locations.count-2]; } else { oldlocation = nil; } nslog(@"didupdatetolocation %@ %@", newlocation, oldlocation); mkcoordinateregion userlocation = mkcoordinateregionmakewithdistance(newlocation.coordinate, 1500.00, 1500.00); [self performsearch:userlocation]; } #pragma mark - search methods -(void)performsearch:(mkcoordinateregion)aregion { // cancel previous searches. [localsearch cancel]; [locationmanager stopupdatinglocation]; // perform new search. mklocalsearchrequest *request = [[mklocalsearchrequest alloc] init]; request.naturallanguagequery = @"taxi"; request.region = aregion; [uiapplication sharedapplication].networkactivityindicatorvisible = yes; localsearch = [[mklocalsearch alloc] initwithrequest:request]; [localsearch startwithcompletionhandler:^(mklocalsearchresponse *response, nserror *error){ [uiapplication sharedapplication].networkactivityindicatorvisible = no; if (error != nil) { [[[uialertview alloc] initwithtitle:nslocalizedstring(@"map error",nil) message:[error localizeddescription] delegate:nil cancelbuttontitle:nslocalizedstring(@"ok",nil) otherbuttontitles:nil] show]; return; } if ([response.mapitems count] == 0) { [[[uialertview alloc] initwithtitle:nslocalizedstring(@"no results",nil) message:nil delegate:nil cancelbuttontitle:nslocalizedstring(@"ok",nil) otherbuttontitles:nil] show]; return; } results = response; [self.searchdisplaycontroller.searchresultstableview reloaddata]; }]; nslog(@"debug"); } #pragma mark - tableview delegate methods - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [results.mapitems count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *identifier = @"searchresultscell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:identifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:identifier]; } mkmapitem *item = results.mapitems[indexpath.row]; cell.textlabel.text = item.name; cell.detailtextlabel.text = item.placemark.addressdictionary[@"street"]; return cell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { [self.searchdisplaycontroller setactive:no animated:yes]; } -(ibaction)closebuttonclicked:(id)sender { [locationmanager stopupdatinglocation]; [self dismissviewcontrolleranimated:yes completion:nil]; } @end
i got working in end end, without using provided search table. stored response results , implemented own uitableview, assigning each result cell.
Comments
Post a Comment