IPhone SDK MapKIt多点和注释 [英] IPhone SDK MapKIt Multiple Points and Annotation
问题描述
我是IPhone SDK开发的新手我正在尝试使用MapKit创建应用程序我已经完成了第一项我想在应用程序中添加多个引脚和注释但我在这里迷失了。
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;
}
推荐答案
你走在正确的轨道上已经,只需重用你的代码来制作多个点。
例如:
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];
将在不同坐标中显示5个点。 (具有相同的名称和副标题)。
in result, will display 5 points in different coordinate. (with same name and subtitle).
编辑:显示不同的图像。你必须将新字段作为NSString *图像添加到DisplayMap。并在for循环中添加你的路径图像。
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;
}
这篇关于IPhone SDK MapKIt多点和注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!