我有纬度和经度如何在mapview中显示注释?

时间:2010-06-05 14:36:19

标签: iphone

我从XML解析得到纬度和经度如何在地图上显示MapAnnotation(pin),请帮助我....

2 个答案:

答案 0 :(得分:1)

  1. 创建一个实现MKAnnotation协议的新类。这将保持纬度/经度,并且还有一个标题和描述,如果您在地图上渲染后选择注释,它将会显示。
  2. 将显示地图的视图的控制器需要实现MKMapViewDelegate协议的 - - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation;方法。这个方法中的代码看起来就像底部的代码(对于格式不佳的道歉,我无法在这里或底部找到答案)。

  3. 然后在您的控制器代码中的某个位置,您需要调用[self.mapView addAnnotation: annotation];行的内容,其中注释是在步骤1中创建的自定义注释类的实例,其中lat / long设置等

  4. 最后,为了正确调用viewForAnnotation方法,并且很容易错过,在界面构建器中,确保将MKMapView的委托出口设置为您的控制器(实现MKMapViewDelegate)协议

    -(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
    
      static NSString *AnnotationViewIdentifier = @"annotationViewIdentifier";
    
      MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier: AnnotationViewIdentifier];
    
      if (annotationView == nil) {
        annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: AnnotationViewIdentifier] autorelease];
    
    // This is all optional and depends on your requirements
        annotationView.animatesDrop = NO;
        annotationView.canShowCallout = YES;
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annotationView.enabled = YES;
        annotationView.pinColor = MKPinAnnotationColorGreen;
      }
      return annotationView;
    }
    

答案 1 :(得分:0)

首先,您要创建MKAnnotation Protocol类(此处MyAnnotation类实现MKAnnotation协议)。此类应包括lat,long,title,subtitle以及您想要的任何内容。

其次,在视图控制器中,您要显示引脚,并且您将实现此代码,

 AnnObj = [[MyAnnotation alloc] init];

 AnnObj.latitude =  [[latitude objectAtIndex:storyIndex] floatValue];

 AnnObj.longitude = [[longitude objectAtIndex:storyIndex] floatValue];

 MKCoordinateRegion region;

 region.center = location;

 MKCoordinateSpan span;

 region.center.latitude = [[latitude objectAtIndex:storyIndex] floatValue];   

 region.center.longitude = [[longitude objectAtIndex:storyIndex] floatValue]; 

 span.latitudeDelta = 0.0005;  

 span.longitudeDelta = 0.0005;

 region.span = span;

 [mapview setRegion:region animated:TRUE];

 AnnObj.title = titleString;

 AnnObj.subTitle = subTitleString;

 NSString * titleString = [[buildingNames objectAtIndex:storyIndex]    stringByReplacingOccurrencesOfString:@"\n\t" withString:@""];

 [eventPoints addObject:AnnObj];

 [mapview addAnnotations:eventPoints];

 Third, Implement the MKAnnotation delegate method,

 - (MKAnnotationView *)mapView: (MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>) annotation {

    if (lmapView.userLocation == annotation){

        return nil;
      }

    MKAnnotationView* myCusAnn =  (MKAnnotationView*)[lmapView dequeueReusableAnnotationViewWithIdentifier:@"eventview"];

    if(myCusAnn == nil)
    {
      myCusAnn = [[[ MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"eventview"] autorelease];

    }

    myCusAnn.canShowCallout = YES;

    [myCusAnn setEnabled:YES];

    return myCusAnn;

  }

我希望它会对你有所帮助。