我从XML解析得到纬度和经度如何在地图上显示MapAnnotation(pin),请帮助我....
答案 0 :(得分:1)
将显示地图的视图的控制器需要实现MKMapViewDelegate协议的 - - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation;
方法。这个方法中的代码看起来就像底部的代码(对于格式不佳的道歉,我无法在这里或底部找到答案)。
然后在您的控制器代码中的某个位置,您需要调用[self.mapView addAnnotation: annotation];
行的内容,其中注释是在步骤1中创建的自定义注释类的实例,其中lat / long设置等
最后,为了正确调用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;
}
我希望它会对你有所帮助。