iPhone SDK MapKIt 多点和注释

发布于 2024-10-14 18:22:39 字数 1461 浏览 2 评论 0原文

我是 iPhone SDK 开发的新手,我正在尝试使用 MapKit 制作一个应用程序,我已经完成了第一步,我想向应用程序添加多个引脚和注释,但我在这里迷失了。

以下是代码,我如何向此代码添加更多引脚

-(void)viewDidLoad{
 [super viewDidLoad];

 [mapView setMapType:MKMapTypeStandard];
 [mapView setZoomEnabled:YES];
 [mapView setScrollEnabled:YES];
    MKCoordinateRegion region={{0.0,0.0,},{0.0,0.0}};
 region.center.latitude = 26.438047;
 region.center.longitude = 50.116422;
 region.span.latitudeDelta=0.01f;
 region.span.longitudeDelta=0.01f;
 [mapView setRegion:region animated:YES]; 
 [mapView setDelegate:self]; 

 DisplayMap *ann = [[DisplayMap alloc] init];
 ann.title = @"Corporate Office";
 ann.subtitle =@"King Khalid Street";
 ann.coordinate=region.center;
 [mapView addAnnotation:ann];
 }

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>) annotation{
 MKPinAnnotationView *pinView=nil;
 if (annotation != mapView.userLocation) {
  static NSString *defaultPinID = @"com.invasivecode.pin";
  pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
  if (pinView ==nil) pinView = [[[MKPinAnnotationView alloc]
            initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
  pinView.pinColor=MKPinAnnotationColorRed;
  pinView.canShowCallout=YES;
  pinView.animatesDrop=YES;
  pinView.calloutOffset= CGPointMake(-5, 5);
  }
 else {
  [mapView.userLocation setTitle:"I am here"];
 }
 return pinView;
}

I am new to IPhone SDK Development i am trying to make an application with MapKit i have done the first bit i want to add multiple pins and annotation to the application but i am lost here.

Following is the code how can i add more pins to this code

-(void)viewDidLoad{
 [super viewDidLoad];

 [mapView setMapType:MKMapTypeStandard];
 [mapView setZoomEnabled:YES];
 [mapView setScrollEnabled:YES];
    MKCoordinateRegion region={{0.0,0.0,},{0.0,0.0}};
 region.center.latitude = 26.438047;
 region.center.longitude = 50.116422;
 region.span.latitudeDelta=0.01f;
 region.span.longitudeDelta=0.01f;
 [mapView setRegion:region animated:YES]; 
 [mapView setDelegate:self]; 

 DisplayMap *ann = [[DisplayMap alloc] init];
 ann.title = @"Corporate Office";
 ann.subtitle =@"King Khalid Street";
 ann.coordinate=region.center;
 [mapView addAnnotation:ann];
 }

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>) annotation{
 MKPinAnnotationView *pinView=nil;
 if (annotation != mapView.userLocation) {
  static NSString *defaultPinID = @"com.invasivecode.pin";
  pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
  if (pinView ==nil) pinView = [[[MKPinAnnotationView alloc]
            initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
  pinView.pinColor=MKPinAnnotationColorRed;
  pinView.canShowCallout=YES;
  pinView.animatesDrop=YES;
  pinView.calloutOffset= CGPointMake(-5, 5);
  }
 else {
  [mapView.userLocation setTitle:"I am here"];
 }
 return pinView;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-10-21 18:22:39

您已经走在正确的轨道上,只需重用您的代码即可获得多个点。
例如:

 DisplayMap *ann = [[DisplayMap alloc] init];   


 for( int i =1;i<=5;i++ ){
     region.center.latitude = 26.438047+i;
     region.center.longitude = 50.116422+i;
     ann.title = [NSString stringWithFormat:@"title %d",i)];
     ann.subtitle =[NSString stringWithFormat:@"subtitle %d",i)];
     ann.image = [NSString stringWithFormat@"image_%d.png",i];

     ann.coordinate=region.center;
     [mapView addAnnotation:ann];
  }
 [ann release];

在结果中,将显示不同坐标的 5 个点。 (具有相同的名称和副标题)。

编辑:显示不同的引脚图像。您必须将新字段作为 NSString *image 添加到 DisplayMap。并在 for 循环中添加路径图像。

- (MKAnnotationView *) mapView:(MKMapView *)amapView viewForAnnotation:(id      <MKAnnotation>) annotation
    {
 NSLog(@"pinnview before release %d",[pinView retainCount]);

if (pinView !=nil) {
    pinView =nil;
    [pinView release];
}
NSLog(@"pinnview after release %d",[pinView retainCount]);

// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

if(annotation != map.userLocation)
{

    static NSString *defaultPinID = @"your-pin";

    pinView = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

    if ( counting < [map.annotations count])
    {
        counting++;

        pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];


        for(DisplayMap* a in map.annotations)
        {
            if (annotation == a){
                pinView.image =
                [UIImage imageWithContentsOfFile:
                 [[NSBundle mainBundle] pathForResource:a.image ofType:nil]];   
            }
        }
        pinView.centerOffset= CGPointMake(0,-10);
        pinView.canShowCallout = YES;


    }

}

return pinView;

}

you are on the right track already, just reuse your code to make multiple points.
for example :

 DisplayMap *ann = [[DisplayMap alloc] init];   


 for( int i =1;i<=5;i++ ){
     region.center.latitude = 26.438047+i;
     region.center.longitude = 50.116422+i;
     ann.title = [NSString stringWithFormat:@"title %d",i)];
     ann.subtitle =[NSString stringWithFormat:@"subtitle %d",i)];
     ann.image = [NSString stringWithFormat@"image_%d.png",i];

     ann.coordinate=region.center;
     [mapView addAnnotation:ann];
  }
 [ann release];

in result, will display 5 points in different coordinate. (with same name and subtitle).

Edited: show different pin image. you have to add new field as NSString *image to DisplayMap. and add your path image inside for loop.

- (MKAnnotationView *) mapView:(MKMapView *)amapView viewForAnnotation:(id      <MKAnnotation>) annotation
    {
 NSLog(@"pinnview before release %d",[pinView retainCount]);

if (pinView !=nil) {
    pinView =nil;
    [pinView release];
}
NSLog(@"pinnview after release %d",[pinView retainCount]);

// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

if(annotation != map.userLocation)
{

    static NSString *defaultPinID = @"your-pin";

    pinView = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

    if ( counting < [map.annotations count])
    {
        counting++;

        pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];


        for(DisplayMap* a in map.annotations)
        {
            if (annotation == a){
                pinView.image =
                [UIImage imageWithContentsOfFile:
                 [[NSBundle mainBundle] pathForResource:a.image ofType:nil]];   
            }
        }
        pinView.centerOffset= CGPointMake(0,-10);
        pinView.canShowCallout = YES;


    }

}

return pinView;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文