如何在地图视图中添加标记?

4

我是一名iPhone应用程序的新用户,我想在我的MKMapView中显示地图标记。请问如何做到这一点?

请给我一些有价值的建议。


你应该考虑接受一些其他问题的答案。这样你会得到更多的回答。 - RedBlueThing
3个回答

4
你需要创建一个实现MKAnnotation协议的代理:
@interface AnnotationDelegate : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id) initWithCoordinate:(CLLocationCoordinate2D)coord;

@end

@implementation AnnotationDelegate

@synthesize coordinate;

- (id) initWithCoordinate:(CLLocationCoordinate2D)coord
{
    coordinate.latitude = coord.latitude;
    coordinate.longitude = coord.longitude;
    return self;
}

@end

对于你的每个地图点,你需要实例化一个 AnnotionDelegate 对象(传入该点的坐标),并将其添加到 MKMapView 中:

AnnotationDelegate * annotationDelegate = [[[AnnotationDelegate alloc] initWithCoordinate:coordinate] autorelease];
[self._mapView addAnnotation:annotationDelegate];

1
NSString * urlString = nil;
        urlString = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%@",12.58, 77.35, [NSString stringWithFormat:@"%@,%@,%@",[detailInfoDict valueForKey:@"address"],[detailInfoDict valueForKey:@"city"],[detailInfoDict valueForKey:@"state"]]];
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: urlString]]

0
if([points retainCount] > 0)
  {
    [points release];
    points = nil;
  }


  if([annotationAry retainCount] > 0)
  {
    [annotationAry release];
    annotationAry = nil;
  }
  points = [[NSMutableArray alloc]init];
  annotationAry = [[NSMutableArray alloc]init]; 
  for(int i=0;i<[longitudeary count];i++)
  { 
    CLLocation* currentLocation1 = [[CLLocation alloc] initWithLatitude:[[latitudeary objectAtIndex:i]doubleValue] longitude:[[longitudeary objectAtIndex:i]doubleValue]];

    [points addObject:currentLocation1];
  }


  for(int i=0;i<[points count];i++)
  {
    // CREATE THE ANNOTATIONS AND ADD THEM TO THE MAP
    CSMapAnnotation* annotation = nil;

    // create the start annotation and add it to the array
      annotation = [[[CSMapAnnotation alloc] initWithCoordinate:[[points objectAtIndex:i] coordinate] 
                          annotationType:CSMapAnnotationTypeImage 
                              title:@"123456..."
                               shID:[shIDary objectAtIndex:i] 
                              catID:[catIDary objectAtIndex:i]
                             ciggUse:[ciggaretteUSEary objectAtIndex:i]
                             wifiUse:[wifiUSEary objectAtIndex:i]
                            controller:self]autorelease];


    [annotationAry addObject:annotation];
  }

  [mapViewmy addAnnotations:[NSArray arrayWithArray:annotationAry]];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接