Search code examples
iphonemapkitmkpinannotationview

how to handle multiple pins are going to dropped at a one longitude & latitude in iphone sdk?


I have one task to develop application with map-kit.
I am getting multiple pins from server and one pin i have to drop from user current location. This user current location pin should be green & other pins which are coming from server should be in another color.How can i do this??

and another question is i am getting some pins on one another because there latitude & longitude have some minor differences. so at that particular point where the pins are going to be dropped on one another that point i want to give one button (on pin pop up window) which handle the minor difference of lat & long pins means the button tells next pin & as soon as the button pressed the pop up window should go on another pin which is not already selected & open its pop up window which also tells next pin. how can i do this??

i am using code like this for creating pin

for getting user loacation pin

        Place* current = [[[Place alloc] init] autorelease];
    current.name = @"You are here.";
    current.latitude = coordinate.latitude;
    current.longitude = coordinate.longitude;
    PlaceMark *from = [[[PlaceMark alloc] initWithPlace:current] autorelease];              
    [mapView addAnnotation:from];

for getting server location pin.

    int s=1;
    for (j=0 ; j < i - 1   ; j++) 
    {
        //points = [[NSString alloc] initWithFormat:@"point%d",s];
        reports = [NSArray arrayWithArray:lat];
        NSString *titles = [[NSString alloc] initWithFormat:@"%@",[tit objectAtIndex:j]];
        NSString *description = [[NSString alloc] initWithFormat:@"%@",[des objectAtIndex:j]];
        float latitude=[[lat objectAtIndex:j] floatValue];
        float longitude=[[lon objectAtIndex:j] floatValue];
        Place* home = [[[Place alloc] init] autorelease];
        home.name = [NSString stringWithFormat:@"%@",titles];
        home.description = [NSString stringWithFormat:@"%@",description];
        home.latitude = latitude;
        home.longitude = longitude;
        PlaceMark *from = [[[PlaceMark alloc] initWithPlace:home] autorelease];             [mapView addAnnotation:from];
        s++;
        [self centerMap];
    }

this code for creating there popup window.

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{
if (annotation == mapView.userLocation)
return nil;
MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"asdf"];
    if (pin == nil)
    pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: @"asdf"] autorelease];
else
pin.annotation = annotation;
pin.userInteractionEnabled = YES;
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[disclosureButton setFrame:CGRectMake(0, 0, 30, 30)];
pin.rightCalloutAccessoryView = disclosureButton;
pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;}

this code for handle there events means pin is calling the button action.

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{ nslog (do something)
}

My all codes are working properly. i want just answer of above given questions..pls help me


Solution

  • Answer For your first question:

    First Find your current location refer THIS ANSWER with all comments But use Following code to do this.

    #pragma mark -
    #pragma mark Location Manager functions
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {   NSLog(@"Inside Location Delegate"); 
    /*NSString *val=[[NSString alloc] initWithFormat:@"Previous Location is:\n Lattitude : %f\n Longitude : %f \n\n Current location is :\n Lattitude : %f\n Longitude : %f",oldLocation.coordinate.latitude, oldLocation.coordinate.longitude,newLocation.coordinate.latitude, newLocation.coordinate.longitude];
    NSLog(@"%@",val);*/
    [self setMapCenter:newLocation.coordinate];
    
    PlaceMark *addAnnotation = [[[PlaceMark alloc] initWithCoordinate:newLocation.coordinate] retain];
    [addAnnotation setTitle:@"Your Location"];
    [addAnnotation setSubTitle:@""];        
    [self._mapView addAnnotation:addAnnotation];
    userAnnoFlag = TRUE;
    
    [self._mapView selectAnnotation:[[self._mapView annotations] lastObject] animated:YES];
    
    [self.locationManager stopUpdatingLocation];
    }
    

    Then Use following method to set the pins:

    #pragma mark -
    #pragma mark Annotations Functions
    
    - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    @try {
        MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyPin"];  
        annView.animatesDrop=FALSE;
        annView.canShowCallout = YES;
        [annView setSelected:YES];
        if (userAnnoFlag == TRUE){
            annView.pinColor = MKPinAnnotationColorRed;
            userAnnoFlag = FALSE;
        }
        if ([mapView userLocation]) {
            annView.pinColor = MKPinAnnotationColorRed;
        }
        else{
            annView.pinColor = MKPinAnnotationColorGreen;
        }   
        annView.calloutOffset = CGPointMake(-5, 5); 
        return annView;
    }
    @catch (NSException * e) {      
    }
    @finally {      
    }
    return 0;
    }
    

    And in case of your second question, you better check the difference between latitude and longitude, manually, and pass it if it has a enough difference(according to your requirement).