2014-02-26 71 views
-2

我展示一些定製註釋中的MapView使用以下代碼,如何在MKMapView中顯示用戶位置以及自定義註釋?

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

如何添加?請指教。

回答

3

要顯示用戶位置補充一點:

self.mapView.showsUserLocation = YES; 

如果您運行的模擬器上的應用程序,請輸入經緯度 選擇iPhone模擬器添加自定義位置 - >調試 - >位置 - > CustomLocation

+0

這個簡單的事情,我錯過了,感謝回憶。 – NAZIK

相關問題