我正在开发一个应用程序,我在其中获取 Facebook 位置并MKMapView
使用以下方法将它们显示为注释。
-(void) connectionDidFinishLoading: (NSURLConnection *) connection
{
latitudeArray = [[NSMutableArray alloc]init];
longitudeArray = [[NSMutableArray alloc]init];
nameArray = [[NSMutableArray alloc]init];
placeImgArray = [[NSMutableArray alloc]init];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSDictionary *placesDict = [NSJSONSerialization JSONObjectWithData:empJsonData options:kNilOptions error:nil];
NSLog(@"COUNT = %i",placesDict.count);
NSLog(@"Places Dictionary = %i",[[placesDict objectForKey:@"data"] count]);
if ([[placesDict objectForKey:@"data"]count] >= 2) {
for (int i = 0; i<= [[placesDict objectForKey:@"data"] count] -1; i++) {
NSString *latitude = [[[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"location"]objectForKey:@"latitude"];
NSString *longitude = [[[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"location"]objectForKey:@"longitude"];
NSString *name = [[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"name"];
imgURlStr = [[[placesDict objectForKey:@"data"]objectAtIndex:i]objectForKey:@"id"];
[latitudeArray addObject:latitude];
[longitudeArray addObject:longitude];
[nameArray addObject:name];
[placeImgArray addObject:imgURlStr];
CLLocationCoordinate2D fbPlace;
fbPlace.latitude = [latitude doubleValue];
fbPlace.longitude = [longitude doubleValue];
Annotation *fbAnno = [[Annotation alloc]init];
fbAnno.coordinate = fbPlace;
fbAnno.title = name;
[mapView addAnnotation:fbAnno];
}
}
[mapView reloadInputViews];
}
现在我需要获取与这些地方关联的图像并将它们作为注释图像而不是默认图钉。我怎样才能做到这一点 ??我尝试了以下代码,但应用程序崩溃了。
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:defaultPinID] autorelease];
UIButton *calloutButton = [UIButton buttonWithType:UIButtonTypeCustom];
calloutButton.frame = CGRectMake(0, 0, 40, 20);
calloutButton.backgroundColor = [UIColor whiteColor];
[calloutButton setTitle:@"Chatin" forState:UIControlStateNormal];
calloutButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:12];
calloutButton.titleLabel.textColor = [UIColor blackColor];
pinView.rightCalloutAccessoryView = calloutButton;
//pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
//pinView.animatesDrop = YES;
//////////////// Downloading Place Images from Facebook//////////////////////
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSString *placeImgURLStr = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=small",imgURlStr];
// `imgURlStr` is `NSString` containing `id` of the Facebook place.
NSData *data0 = [NSData dataWithContentsOfURL:[NSURL URLWithString:placeImgURLStr]];
UIImage *image = [UIImage imageWithData:data0];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
pinView.image = image;
pinView.hidden =NO;
});
});
//////////////////////////////////////////////////////////////////////////////////
}
else {
[mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}