我正在申请将引脚/注释放在mapview中。当用户靠近引脚时,我希望引脚改变颜色。一切正常,引脚放在我想要的地方,当我足够接近时,我收到一条警告信息。但我不知道如何更新引脚颜色。我是否必须删除注释并替换它们?似乎没必要,我正在寻找的是某种更新/刷新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
rows是包含不同位置及其信息的对象
这是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 :(得分: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;
}