以正确的方式加载mapview

时间:2011-12-14 06:19:19

标签: iphone ios annotations mkmapview

我正在加载带注释的mapview。我编写的代码如下所示。由于我是OOPS的新手,我知道我可能犯了很多错误。如果有人会审查我的代码并给出一些建议,那将非常有用。

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

我可能犯了很多错误。所有建议将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

以下是您可以选择的链接,并知道如何正确构建代码http://www.highoncoding.com/Articles/804_Introduction_to_MapKit_Framework_for_iPhone_Development.aspx

希望这对你有所帮助。