如何在MKMapView中显示用户位置以及自定义注释?

时间:2014-02-26 06:10:08

标签: ios iphone objective-c mkmapview core-location

我正在使用下面的代码

在mapview中显示一些自定义注释
for(int i=0;i<[[st.defaultmaparray objectAtIndex:0] count];i++)
{
 CLLocationCoordinate2D location;
 location.latitude = [[[st.defaultmaparray objectAtIndex:13] objectAtIndex:i] floatValue];
 location.longitude = [[[st.defaultmaparray objectAtIndex:14] objectAtIndex:i] floatValue];
 MKCoordinateRegion region;
 MKCoordinateSpan span;

 span.latitudeDelta = 0.05;
 span.longitudeDelta = 0.05;

 region.span = span;
 region.center = location;
 [latlongarray addObject:[NSString stringWithFormat:@"%f,%f",location.latitude,location.longitude]];
 [self.mapView regionThatFits:region];
 DisplayMap *ann = [[DisplayMap alloc] init];

 ann.title = [[st.defaultmaparray objectAtIndex:3] objectAtIndex:i];
 ann.subtitle = [NSString stringWithFormat:@"%@,%@,%@",[[st.defaultmaparray objectAtIndex:4] objectAtIndex:i],[[st.defaultmaparray objectAtIndex:9] objectAtIndex:i],[[st.defaultmaparray objectAtIndex:10] objectAtIndex:i]];
 ann.coordinate = region.center;


 [self.mapView addAnnotation:ann];
   //[self.mapView selectAnnotation:ann animated:YES];
}

及其viewforannotation方法如下,

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
int imageint = [latlongarray indexOfObject:[NSString stringWithFormat:@"%f,%f",annotation.coordinate.latitude,annotation.coordinate.longitude]];
static NSString *SFAnnotationIdentifier = @"SFAnnotationIdentifier";
if ([annotation isKindOfClass:[MKUserLocation class]])
{
     NSLog(@" view for UL ");
     return nil;
}
else
{
    MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:SFAnnotationIdentifier];
    customPinView.pinColor = MKPinAnnotationColorRed;
    customPinView.animatesDrop = YES;
    customPinView.canShowCallout = YES;

    AsyncImageView *async = [[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    [async loadImageFromURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[st.defaultmaparray objectAtIndex:5] objectAtIndex:imageint]]];
    async.backgroundColor=[UIColor whitecolor];
    [sfIconView addSubview:async];
    sfIconView.frame = CGRectMake(-15, 0, 50, 50);
    [customPinView addSubview:async];
    return customPinView;
}
 return nil;
}

上面的代码工作正常,输出如下,

enter image description here

但是,现在我需要显示用户的当前位置以及这些自定义注释。用户的位置应该像蓝点标记一样显示,周围有圆形波,如下图所示,

enter image description here

如何添加?请指教。

1 个答案:

答案 0 :(得分:3)

要显示用户位置,请添加以下内容:

self.mapView.showsUserLocation = YES;

如果您在模拟器上运行您的应用,请输入纬度和经度来添加自定义位置 选择iOS模拟器 - &gt;调试 - &gt;位置 - &gt; CustomLocation