2011-04-14 40 views
1

我正在制作一个应用程序,它会将引脚/注释放入ma​​pview中。当用户靠近销钉时,我希望销钉改变颜色。一切工作正常,引脚放在我想要的地方,当我接近时我会收到一条警告消息。但我不知道如何更新引脚颜色。我是否必须删除注释并将其替换?似乎没有必要,我正在寻找的是一种更新/刷新地图视图,而不必更换注释如何更新/更改已经放置在我的mapview中的针的颜色?

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

位置是CLLocationCoordinate2D
行由含有不同的位置和它的信息

这里的的MapView委托方法的对象:(不知道为什么在过去“}”是代码样品之外)

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

}

回答

0

你需要做一个循环,这将考验该位置是在用户的x英里内。这里是我在我自己的代码中使用

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

} 

基本上,创建一个循环,将通过位置搜索一个数组中的我是什么意思的例子。有它 [mapView.userLocation.location distanceFrom:prox] /1609.334 // prox表示被测试的位置实例,以查看用户的x英里内是否。

然后说

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

     annotationView = x; 
}