Question

I am loading a map view. And I am adding pins to the map, based on address of the contacts in address book. I am having some strange cases where some pins(maybe 2 out of 10) don't drop in the map view . I have checked the address, which is valid. But the pin is not dropping. What could be the reason for this.

    for (i = 0; i<[groupContentArray count]; i++) {
        person = ABAddressBookGetPersonWithRecordID(addressbook,[[groupContentArray objectAtIndex:i] intValue]);
        ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty);
        NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty);
        NSLog(@"Address %@", address);
        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"];

            if (streetName == (id)[NSNull null] || streetName.length == 0 ) streetName = @"";
            if (stateName == (id)[NSNull null] || stateName.length == 0 ) stateName = @"";
            if (cityName == (id)[NSNull null] || cityName.length == 0 ) cityName = @"";
            if (country == (id)[NSNull null] || country.length == 0 ) country = @"";


            NSString *fullAddress = [streetName stringByAppendingFormat:@"%@/%@/%@", cityName, stateName, country];
            mapCenter = [self getLocationFromAddressString:fullAddress];
            if(stateName != NULL || country != NULL || streetName != NULL || cityName != NULL){

                if ((mapCenter.latitude == 0) && (mapCenter.longitude == 0))
                {
                    // Alert View
                }
                else{
                addAnnotation = (MyAddressAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:[groupContentArray objectAtIndex:i]];

                if(addAnnotation == nil){
                    addAnnotation = [[[MyAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName Recordid:[groupContentArray objectAtIndex:i] ]autorelease];

                    [mapView addAnnotation:addAnnotation];}
                                    }
            }
        }
        CFRelease(addressProperty);

    }

Edit: I am editing my question and be more specific. I am using the code to drop pins on my mapView. On my Mac PC, if there are 20 pins to be dropped in the map. All the 20 pins are dropping accurately and working fine. But when I am using IPhone or IPad, the pin count reduces massively. That is, out of some 20 pins I am dropping, only 4 pins are showing on the map. I am not able to figure out a reason for this.

Was it helpful?

Solution 2

My bad. In the "fulladdress" string(between streetName and cityName) that I use, were not separated by a space or "," or "/". Therefore some of the addresses were not being recognised.

OTHER TIPS

This code:

else {
    addAnnotation = (MyAddressAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:[groupContentArray objectAtIndex:i]];

    if (addAnnotation == nil) {
        addAnnotation = [[[MyAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName Recordid:[groupContentArray objectAtIndex:i] ]autorelease];

        [mapView addAnnotation:addAnnotation];
    }
}

makes no sense.


The dequeueReusableAnnotationViewWithIdentifier method is for dequeuing MKAnnotationView objects -- not id<MKAnnotation> objects.

The dequeuing code belongs in the viewForAnnotation delegate method anyway, not here.

Here, you should just create your annotation objects (MyAddressAnnotation) and call addAnnotation on them.

I also highly recommend you change the name of your annotation variable from addAnnotation to something else so you can't confuse it with the map view's method addAnnotation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top