1

ユーザーが地域内のすべての場所を検索できるようにする MKlocalSearch を使用して UISearchBar を実装しました。場所とその場所に関連付けられているビジネスの名前を保持する Json 形式の情報を解析し、それらの座標に注釈を配置しました。SearchBar で実行したいのは、ユーザーの場所から 75 マイル以内にある注釈を配置した場所のみを検索し、ユーザーが検索した場合に他の場所を認識しないようにすることです。検索ワードは、各 Json オブジェクトに関連付けられた情報でもある clubName にしたいと思います。つまり、クラブ「Roxy」を検索すると、ユーザーが 75 マイル以内にいる場合はフィルターに表示されますが、「home depot」を検索すると、フィルターに表示されます。その場所には注釈が設定されていないため、何も表示されません。これが私の現在のコードです。

#import "ViewController.h"
#import "DetailController.h"
#import "Annotation.h"


#import "City.h"

@interface ViewController (){
MKLocalSearch *localSearch;
MKLocalSearchResponse *results;
}
@property (nonatomic, strong) IBOutlet DetailController *detailViewController;



 @end
#define getDatalURL @"http://www.club-hop.com/apptest.php"

@implementation ViewController

@synthesize mapView,jsonArray,citiesArray;

- (void)viewDidLoad
{
[super viewDidLoad];

[self retrieveData];
UINavigationBar *bar= [self.navigationController navigationBar];


[bar setTintColor:[UIColor blackColor]];
self.detailViewController = [[DetailController alloc] init];
[self.searchDisplayController setDelegate:self];
[self.ibSearchBar setDelegate:self];
//Zoom the map to current location.
[self.mapView setShowsUserLocation:YES];
[self.mapView setUserInteractionEnabled:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow];

City * cityObject;

// load external page into UIWebView
NSMutableArray * locations= [[NSMutableArray alloc]init];
CLLocationCoordinate2D location;
Annotation * myAnn;

for(int u=0; u<citiesArray.count;u++){
    cityObject=[citiesArray objectAtIndex:u];

    myAnn=[[Annotation alloc]init];

    myAnn.city=cityObject;     // Store the city object on the annotation

    NSNumber *aLat= cityObject.Latitude;
    NSNumber *aLon= cityObject.Longitude;

    double lat = [aLat doubleValue];
    double lon = [aLon doubleValue];

    location.latitude= lat;
    location.longitude=lon;
    myAnn.coordinate = location;
    myAnn.title=cityObject.clubName;
    myAnn.subtitle=cityObject.cityName;

    [locations addObject:myAnn];}

[self.mapView addAnnotations:locations];


}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}



//class methods
-(void) retrieveData{
NSURL * url= [NSURL URLWithString:getDatalURL];
NSData * data= [NSData dataWithContentsOfURL:url];
jsonArray= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//setup cities array
citiesArray=[[NSMutableArray alloc]init];

for(int i=0; i<jsonArray.count;i++){
    NSString * cID= [[jsonArray objectAtIndex:i] objectForKey:@"id"];
    NSString * cName= [[jsonArray objectAtIndex:i] objectForKey:@"cityName"];
    NSString * cCountry= [[jsonArray objectAtIndex:i] objectForKey:@"cityCountry"];
    NSString * cLine= [[jsonArray objectAtIndex:i] objectForKey:@"clubLine"];
    NSString * pri=[[jsonArray objectAtIndex:i] objectForKey:@"price"];
    NSString * promo=[[jsonArray objectAtIndex:i] objectForKey:@"promo"];
     NSString * camera=[[jsonArray objectAtIndex:i] objectForKey:@"camera"];
    NSString * clName= [[jsonArray objectAtIndex:i] objectForKey:@"clubName"];
    NSNumber * cLatitude= [[jsonArray objectAtIndex:i] objectForKey:@"Latitude"];
    NSNumber * cLongitude= [[jsonArray objectAtIndex:i] objectForKey:@"Longitude"];

    [citiesArray addObject:[[City alloc]initWithCityName:cName andCityCountry:cCountry  andClubName:clName andClubLine:cLine andPrice:pri andPromo:promo andCamera:camera andLatitude:cLatitude andLongitude:cLongitude andCityId:cID]];

}

}


#pragma mark - MKMapViewDelegate

// user tapped the disclosure button in the callout
//
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view  calloutAccessoryControlTapped:(UIControl *)control

{   

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                     bundle:nil];
self.detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"Page2"];

Annotation *myAnnotation=(Annotation *)view.annotation;


self.detailViewController.city=myAnnotation.city;


[self.navigationController pushViewController:self.detailViewController animated:YES];


}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pin=nil;
if ([annotation isKindOfClass:[Annotation class]])
{
    pin=(MKAnnotationView *)[mapView   dequeueReusableAnnotationViewWithIdentifier:@"myAnnotation"];
    if (pin == nil)
    {
        pin=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pin.image=[UIImage imageNamed:@"mappin.png"];
        pin.centerOffset=CGPointMake(0.0, pin.image.size.height/-2);
        pin.canShowCallout=YES;
    }
  }
return pin;
}



#pragma mark - Search Methods

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

// Cancel any previous searches.
[localSearch cancel];

// Perform a new search.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = searchBar.text;
request.region = self.mapView.region;

[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];
}];
}

- (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];

MKMapItem *item = results.mapItems[indexPath.row];


[self.mapView setCenterCoordinate:item.placemark.location.coordinate animated:YES];

[self.mapView setUserTrackingMode:MKUserTrackingModeNone];

}



@end

ViewController.h

#import <UIKit/UIKit.h>
#import <Mapkit/Mapkit.h>

@interface ViewController : UIViewController <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) NSMutableArray * jsonArray;
@property (nonatomic, strong) NSMutableArray * citiesArray;
@property (strong, nonatomic) IBOutlet UISearchBar *ibSearchBar;


-(void) retrieveData;


@end
4

0 に答える 0