如何在mkmap的注释中添加图像 [英] How to add image in annotation in mkmap
本文介绍了如何在mkmap的注释中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Xcode 的新手,我需要在注释点中设置图像.我正在尝试用特定图像替换典型的 iOS mapkitpin".我是编码新手,所以我不确定为什么这不起作用,但这是我尝试过的我的代码是
I am new to Xcode, i need to set the image in annotation point. I am trying to replace the typical iOS mapkit "pin" with a particular image. I'm new to coding so I'm not sure exactly why this isn't working, but here is what i've attempted My code is
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlMapString=[NSString stringWithFormat:@"http://www.xxxx.com/xxxx_webservice/map.php?format=json&truckno=%@",nam2];
NSURL *urlMap=[NSURL URLWithString:urlMapString];
NSData *dataMap=[NSData dataWithContentsOfURL:urlMap];
NSError *errorMap;
NSDictionary *jsonMap = [NSJSONSerialization JSONObjectWithData:dataMap options:kNilOptions error:&errorMap]; NSArray *resultsMap = [jsonMap valueForKey:@"posts"];
NSArray *resMap = [resultsMap valueForKey:@"post"];
NSArray *latitudeString=[resMap valueForKey:@"latitude"];
NSString *latOrgstring = [latitudeString objectAtIndex:0];
double latitude=[latOrgstring doubleValue];
NSArray *longitudeString=[resMap valueForKey:@"longitude"];
NSString *longOrgstring = [longitudeString objectAtIndex:0];
double longitude=[longOrgstring doubleValue];
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude=latitude;
center.longitude=longitude;
//Span
MKCoordinateSpan span;
span.latitudeDelta=THE_SPAN;
span.longitudeDelta=THE_SPAN;
myRegion.center=center;
myRegion.span=span;
//Set our mapView
[MapViewC setRegion:myRegion animated:YES];
//Annotation
//1.create coordinate for use with the annotation
CLLocationCoordinate2D wimbLocation;
wimbLocation.latitude=latitude;
wimbLocation.longitude=longitude;
Annotation * myAnnotation= [Annotation alloc];
myAnnotation.image = [UIImage imageNamed:@"icon.gif"];
CLLocation *someLocation=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:someLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
addressOutlet=[dictionary valueForKey:@"Street"];
City=[dictionary valueForKey:@"City"];
State=[dictionary valueForKey:@"State"];
myAnnotation.coordinate=wimbLocation;
if (addressOutlet!=NULL&&City!=NULL)
{
myAnnotation.title=addressOutlet;
myAnnotation.subtitle=[NSString stringWithFormat:@"%@,%@", City, State];
myAnnotation.image = [UIImage imageNamed:@"icon.gif"];
}
else if (addressOutlet==NULL&&City!=NULL)
{
myAnnotation.title=City;
myAnnotation.subtitle=[NSString stringWithFormat:@"%@,%@", City, State];
myAnnotation.image = [UIImage imageNamed:@"icon.gif"];
}
else if (addressOutlet!=NULL&&City==NULL)
{
myAnnotation.title=addressOutlet;
myAnnotation.subtitle=[NSString stringWithFormat:@"%@", State];
myAnnotation.image = [UIImage imageNamed:@"icon.gif"];
}
else if(addressOutlet==NULL&&City==NULL)
{
myAnnotation.title=State;
myAnnotation.subtitle=[NSString stringWithFormat:@"%@",State];
myAnnotation.image = [UIImage imageNamed:@"icon.gif"];
}
[self.MapViewC addAnnotation:myAnnotation];
}];
}
请指导我设置图像..提前致谢
Please guide me to set the image.. Thanks in Advance
推荐答案
可以使用 mapView Delegate 方法.
You can use mapView Delegate method.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
return nil;
static NSString *s = @"identifier";
MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
if (!pin) {
pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
pin.canShowCallout = YES;
pin.image = [UIImage imageNamed:@"pin.png"];
pin.calloutOffset = CGPointMake(0, 0);
}
return pin;
}
这篇关于如何在mkmap的注释中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文