我遇到了(我认为)这个mapView委托方法的问题:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
选择了引脚后,会显示一个标注。
当我移动地图以使引脚和标注不在视线范围内(两三个屏幕之外)或放大另一个区域时,它会缩放回选定的引脚。
我猜它正在这样做,因为引脚现在已经没有内存了,而且它正在被重新创建。在重新创建时,它正在被选中并且视图正在缩放。
如何让它不缩放?
下面的Anna K,问这个方法。我还粘贴了viewForAnnotation。
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if ([view.annotation isKindOfClass:[MKUserLocation class]]) {
//
NSLog(@"selected user loc");
} else
{
selectedAnnotation = view.annotation;
if (self.calloutAnnotation == nil) {
self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude
andLongitude:view.annotation.coordinate.longitude];
} else {
self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;
}
// this doesn't allow user to click away from selected pin to another
// [self.mapView addAnnotation:self.calloutAnnotation];
// this fixes it
[self.mapView performSelector:@selector(addAnnotation:) withObject:self.calloutAnnotation afterDelay:0.005];
self.selectedAnnotationView = view;
BasicMapAnnotationView *tempAnnView = (BasicMapAnnotationView *)view;
selectedPin = tempAnnView.tag;
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
originalRegion = self.mapView.region;
if (annotation == self.calloutAnnotation) {
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"storeID == %@", [NSNumber numberWithInt:selectedPin]];
NSMutableArray* mutableFetchResults = [CoreDataHelper searchObjectsInContext:@"Store" :searchPredicate :@"storeName" :YES :self.managedObjectContext];
if ([mutableFetchResults count] > 0)
{
Singleton *singleton = [Singleton sharedSingleton];
Store *tempStore = (Store*)[mutableFetchResults objectAtIndex:0];
if (tempStore.storeIsPicked == [NSNumber numberWithBool:YES]) {
singleton.isPicked = YES;
} else
{
singleton.isPicked = NO;
}
}
AccessorizedCalloutMapAnnotationView *calloutMapAnnotationView = [[[AccessorizedCalloutMapAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"CalloutAnnotation"] autorelease];
calloutMapAnnotationView.contentHeight = 40.0;
if ([mutableFetchResults count] > 0)
{
Store *store = (Store*)[mutableFetchResults objectAtIndex:0];
UILabel *shopName = [[UILabel alloc] initWithFrame:CGRectMake(36, -3, 250, 29)];
shopName.font = [UIFont boldSystemFontOfSize:14.0];
shopName.text = store.storeName;
shopName.backgroundColor= [UIColor clearColor];
UILabel *addressName = [[UILabel alloc] initWithFrame:CGRectMake(36, 13, 250, 29)];
addressName.font = [UIFont systemFontOfSize:9.0];
addressName.textColor = [UIColor lightGrayColor];
addressName.text = store.storeAddress;
addressName.backgroundColor= [UIColor clearColor];
[calloutMapAnnotationView.contentView addSubview:shopName];
[calloutMapAnnotationView.contentView addSubview:addressName];
calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
[shopName release];
[addressName release];
calloutMapAnnotationView.mapView = self.mapView;
return calloutMapAnnotationView;
} else
{
NSLog(@"returning nil .. no stores");
return nil;
}
}
else
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
// do something with blue dot?
}
else
{
BasicMapAnnotationView *annotationView = [[[BasicMapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotation"] autorelease];
BasicMapAnnotation *tempAn = (BasicMapAnnotation *)annotation;
if ([annotation isKindOfClass:[BasicMapAnnotation class]])
{
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"storeID == %@", [NSNumber numberWithInt:tempAn.mapTag]];
NSMutableArray* mutableFetchResults = [CoreDataHelper searchObjectsInContext:@"Store" :searchPredicate :@"storeName" :YES :self.managedObjectContext];
if ([mutableFetchResults count]>0)
{
Store *store = [mutableFetchResults objectAtIndex:0];
BOOL isPicked = [store.storeIsPicked boolValue];
if (isPicked)
{
annotationView.image = [UIImage imageNamed:@"pinPicked.png"];
} else
{
annotationView.image = [UIImage imageNamed:@"pinOff.png"];
}
annotationView.centerOffset = CGPointMake(-8, -9);
annotationView.canShowCallout = NO;
annotationView.tag = tempAn.mapTag;
return annotationView;
}
}
}
}
return nil;
}
答案 0 :(得分:0)
我以前遇到过这个问题,问题是您的地图区域是由引脚位置定义的。您应该将viewDidLoad方法中的mapView setRegion代码更改为所需的位置,然后根据用户位置或任何需要进行更新。
- (void)viewDidLoad{
[super viewDidLoad];
// define span for map: how much area will be shown
MKCoordinateSpan span;
span.latitudeDelta = 0.002;
span.longitudeDelta = 0.002;
// define starting point for map
CLLocationCoordinate2D start;
start.latitude = 42.36873056998856; //Change to what you desire for starting point
start.longitude = -71.11504912376404; //Could also be set to user or pin Location if desired
// create region, consisting of span and location
MKCoordinateRegion region;
region.span = span;
region.center = start;
// move the map to our location
[self.mapView setRegion:region animated:YES];
}