1

I'm making an applikation that will put pins/annotations in a mapview. When the user is getting close to a pin, I want the pin to change color. Everything works fine, the pins is placed where I want and I get an alert message when I get close enough. But I'm not sure how to update the pin color. Do I have to remove the annotations and replace them? Seems unnecessary, all I'm looking for is some kind of update/refresh the mapview without having to replace annotations.

CLLocation *place =[[CLLocation alloc] initWithCoordinate:location altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
    AddresAnnotation *ann = [[AddresAnnotation alloc] initWithCoordinate:place.coordinate];
    [ann setTitle:[rows placeName]];
    [mapView addAnnotation:ann];

location is a CLLocationCoordinate2D
rows is an object containing the different locations and its information

Here's the mapView delegate method: (not sure why the last "}" is outside of the code sample)

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

    if (annotation != self.mapView.userLocation) {
        annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Plats"] autorelease];
        [annView setPinColor:MKPinAnnotationColorRed];
        annView.animatesDrop = TRUE;
        annView.canShowCallout = YES;
        return annView;
    } else {
        zoomButton.enabled = TRUE;
        return nil;
    }

}

Emil
  • 161
  • 2
  • 10

1 Answers1

0

You need to make a loop that will test if the location is within x miles of the user. here is an example of what i mean that i used in my own code

for (CLLocation *prox in locations) {
    NSLog(@"prox %@", prox);
    float distanceFromLocation = [mapView.userLocation.location distanceFromLocation:prox]/1609.344;
    NSLog(@"distance %f", distanceFromLocation);
    if (distanceFromLocation <= 10) {
        NearbyLocation *nearbyLocation = [[NearbyLocation alloc]init];
        NSString *key = [NSString stringWithFormat:@"%d", index];
        nearbyLocation.title = [storedTitles objectForKey:key];
        nearbyLocation.loction = prox;
        nearbyLocation.subtitle = [NSString stringWithFormat:@"%.1f miles away", distanceFromLocation];
        nearbyLocation.lat = prox.coordinate.latitude;
        nearbyLocation.lon = prox.coordinate.longitude;
        [newArray addObject:nearbyLocation];
        [nearbyLocation release];
    }
    index++;
}
NSLog(@"new array %d prox %d", [newArray count], [proximityOutlet.nearbyLocations count]);
if ([newArray count] > [proximityOutlet.nearbyLocations count]) {
    NSLog(@"set array");
    // if the new array has actually added any objects, set the array and switch views;
    proximityOutlet.nearbyLocations = newArray;
    //[self.navigationController pushViewController:proximityOutlet animated:YES];
    proximityOutlet.modalPresentationStyle = UIModalPresentationPageSheet;
    proximityOutlet.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:proximityOutlet animated:YES];
}
[newArray release];

}

basically, create a loop that will search through an array of locations. have it [mapView.userLocation.location distanceFrom:prox]/1609.334 //prox representing an instance of a location that is tested to see if the within x miles of the user.

then say

if ([mapview.userLocation.location distanceFrom:prox]/1609.334 < (however many miles)){

        annotationView = x;
}
Jordan Brown
  • 638
  • 1
  • 5
  • 14