简体   繁体   中英

Load mapview in the correct way

I am loading a mapview with annotation. I have written the code as shown below. As I am new to OOPS I know that I may have committed a lot of mistakes. It would be really helpful, if someone would review my code and give some suggestions.

- (void)viewDidLoad
{
[super viewDidLoad];

if(groupOrContactSelected){
    UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.infoButton];
    UIBarButtonItem *segmentedButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    NSArray *toolbarItems = [NSArray arrayWithObjects: infoButtonItem, flexibleSpace, segmentedButton, nil];
    [self setToolbarItems:toolbarItems];
    self.navigationController.toolbar.translucent = true;
    self.navigationController.toolbarHidden = NO;
    [infoButtonItem release];
    [segmentedButton release];
    [flexibleSpace release];

    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    mapView.delegate=self; 

    [self.view addSubview:mapView]; 

    addressbook = ABAddressBookCreate();

    for (i = 0; i<[groupContentArray count]; i++) {

        person = ABAddressBookGetPersonWithRecordID(addressbook,[[groupContentArray objectAtIndex:i] intValue]);
        ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty);
        NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty);
        for (NSDictionary *addressDict in address) 
        {
            addAnnotation = nil;
            firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
            lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 
            NSString *country = [addressDict objectForKey:@"Country"];
            NSString *streetName = [addressDict objectForKey:@"Street"];
            NSString *cityName = [addressDict objectForKey:@"City"];
            NSString *stateName = [addressDict objectForKey:@"State"];
            NSString *fullAddress = [streetName stringByAppendingFormat:@"%@/%@/%@", cityName, stateName, country];
            mapCenter = [self getLocationFromAddressString:fullAddress];
            if(stateName != NULL || country != NULL || streetName != NULL || cityName != NULL){

                addAnnotation = (SJAddressAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:[groupContentArray objectAtIndex:i]];

                if(addAnnotation == nil){
                    addAnnotation = [[[SJAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName Recordid:[groupContentArray objectAtIndex:i] ]                                 autorelease];
                    [mapView addAnnotation:addAnnotation];}
            }
        }
        CFRelease(addressProperty);

    }
    [self zoomToFitMapAnnotations:mapView];

    [self.navigationItem setHidesBackButton:YES animated:YES];
    NSString *mapTitle = localizedString(@"MAP_TITLE");
    [self setTitle:mapTitle];
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

    NSString *close = localizedString(@"CLOSE");
    UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:close style:UIBarButtonItemStylePlain target:self action:@selector(onClose:)];   

    self.navigationItem.rightBarButtonItem = closeButton;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    [closeButton release];
}

[searchDirectionSegmentedControl release];
[mapView release];
}

I may have committed a lot of mistakes. All suggestions will be appreciated. Thanks

Here is link which you can prefer and know how to structure the code properly http://www.highoncoding.com/Articles/804_Introduction_to_MapKit_Framework_for_iPhone_Development.aspx

Hope this help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM