이 문제는 이미 2 주 동안 나를 괴롭 히고 있습니다!사용자 정의 다중 핀을 표시하면 위치가 잘못된 핀이 표시됩니다.
나는 탭바 응용 프로그램이 있습니다. 하나의 탭에는 점을 입력하고 다른 탭에는 점이 맵에 표시됩니다. 핀은 점의 유형에 따라 달라야합니다.
내가 직면하는 문제는 한 탭에서 다른 탭으로 전환 할 때마다 핀 이미지가 다른 이미지로 바뀌어야한다는 것입니다. 예를 들어지도에 4 점, 3 점이 원으로 표시되고 3 점이 삼각형으로 표시되면 삼각형이 한 지점에서 다른 지점으로 이동합니다. 심상은 확실히 무작위로 변화하는 것처럼 보입니다.
ViewController.m이 그것을
-(void) viewWillAppear:(BOOL)animated
{
// Select the type of map
if (isMapSelected == NO) {
self.mapView.mapType = MKMapTypeSatellite;
}
else {
self.mapView.mapType = MKMapTypeStandard;
}
// Add region to the map (center and span)
[self addRegion];
// Removing old annotation
[self.mapView removeAnnotations:mapLocations];
// Initializing arrays for the annotations
mapLocations = [[NSMutableArray alloc]init];
[self addAnnotation];
}
-(void) addAnnotation
{
CLLocationCoordinate2D mapLocation;
IGAMapAnnotation *mapAnnotation;
// Calculate how many points are included
NSInteger numberOfPoints = [coordinatesTempArray count];
// Annotations will be added only of the flight plan includes at least one point
if (numberOfPoints > 0)
{
// Trying to add coordinates from the array of coordinates
for (NSInteger i=0; i < ([coordinatesTempArray count]); i++) {
mapAnnotation = [[IGAMapAnnotation alloc]init];
// Taking a point in the array and getting its coordinates
self.mapCoordinates = [coordinatesTempArray objectAtIndex:i];
// Getting a point in the array and getting its lattitude and longitude
self.mapLatitude = [[self.mapCoordinates objectAtIndex:0]doubleValue];
self.mapLongitude = [[self.mapCoordinates objectAtIndex:1]doubleValue];
// Assigning the point coordinates to the coordinates to be displayed on the map
mapLocation.latitude = self.mapLatitude;
mapLocation.longitude = self.mapLongitude;
// Adding coordinates and title to the map annotation
mapAnnotation.coordinate = mapLocation;
mapAnnotation.title = [navaidNamesTempArray objectAtIndex:i];
mapAnnotation.subtitle = nil;
mapAnnotation.navaidType = [navaidTypesTempArray objectAtIndex:i];
// Adding the annotation to the array that will be added to the map
[mapLocations addObject:mapAnnotation];
}
// Adding annotations to the map
[self.mapView addAnnotations:mapLocations];
}
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[IGAMapAnnotation class]])
{
IGAMapAnnotation *myLocation = (IGAMapAnnotation *) annotation;
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"IGAMapAnnotation"];
if (annotationView == nil)
annotationView = myLocation.annotationView;
else
annotationView.annotation = annotation;
return annotationView;
}
else
return nil;
}
IGAMapAnnotation.m
@synthesize coordinate = _coordinate;
@synthesize title = _title;
@synthesize subtitle = _subtitle;
@synthesize type = _type;
// Tried to have this init method but was never able to make it work. Without it, the program crashes too!!!!
-(id)initWithTitle:(NSString *)newTitle Type:(NSString *)type Location:(CLLocationCoordinate2D) newCoordinate
{
self = [super init];
if (self) {
_title = newTitle;
_coordinate = newCoordinate;
_type = type;
}
return self;
}
-(MKAnnotationView *) annotationView {
MKAnnotationView *annotationView = [[MKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:@"IGAMapAnnotation"];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
if ([self.type isEqual: @"A"] || [self.type isEqual: @"B"] || [self.type isEqual: @"C"])
{
annotationView.image = [UIImage imageNamed:@"circle.png"];
}
else if ([self.type isEqual: @"D"])
{
annotationView.image = [UIImage imageNamed:@"triangle.png"];
}
else if ([self.type isEqual: @"E"])
{
annotationView.image = [UIImage imageNamed:@"square.png"];
}
else
{
annotationView.image = [UIImage imageNamed:@"oval.png"];
}
return annotationView;
}
@end
입니다 :
그래서,이 코드입니다. 지금까지, 그 행동은 나에게 의미가 없습니다. 도움 주셔서 감사합니다.
한 주 :이 '이동 [self.mapView의 removeAnnotations을 : mapLocations]'addAnnotation에 viewWillAppear에서의 상기 문제를 해결하지만, 그 이전 특수 핀 제거되지 . –