iOS MapKit自定义PIN
如何在地图中显示图像而不是图钉。到目前为止,我只能在水龙头上添加插脚。.m的示例代码将非常有用,因为我对iOS编程还是新手。iOS MapKit自定义PIN,ios,mapkit,Ios,Mapkit,如何在地图中显示图像而不是图钉。到目前为止,我只能在水龙头上添加插脚。.m的示例代码将非常有用,因为我对iOS编程还是新手。\pragma mark- #pragma mark - #pragma mark MKMapView delegate - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation { if ([annotation
\pragma mark-
#pragma mark -
#pragma mark MKMapView delegate
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
return annotationView;
else
{
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier] autorelease];
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"someImage.png"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
return annotationView;
}
return nil;
}
#pragma标记MKMapView委托
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
静态NSString*AnnotationIdentifier=@“AnnotationIdentifier”;
MKAnnotationView*annotationView=[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
如果(注释视图)
返回注释视图;
其他的
{
MKAnnotationView*annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier]autorelease];
annotationView.canShowCallout=是;
annotationView.image=[UIImage ImageName:@“someImage.png”];
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
[rightButton addTarget:self action:@selector(WriteMething:)for ControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title for状态:UIControlStateNormal];
annotationView.rightCalloutAccessoryView=rightButton;
annotationView.canShowCallout=是;
annotationView.draggable=是;
返回注释视图;
}
返回零;
}
编辑:
我可以向您解释有关MKAnnotationView
的所有内容,但我想您会发现,苹果提供的文档比任何其他来源的文档都要好得多。检查链接中的概述部分
转到Xcode的组织者,然后转到文档并搜索天气图,它显示了地图示例,并在注释处包含图像。
\pragma标记-
#pragma mark -
#pragma mark MKMapView delegate
-(void)addAllPinsOnMapView
{
MKCoordinateRegion region = mapViewOffer.region;
region.center = CLLocationCoordinate2DMake(12.9752297537231, 80.2313079833984);
region.span.longitudeDelta= 0.1f;
region.span.latitudeDelta= 0.1f;
[mapViewOffer setRegion:region animated:YES];
mapViewOffer.delegate=self;
arrMapPin=[[NSMutableArray alloc] init];
NSArray *name=[[NSArray alloc]initWithObjects:
@"Title1",
@"Title2",
@"Title3", nil];
NSMutableArray *arrCoordinateStr = [[NSMutableArray alloc] initWithCapacity:name.count];
[arrCoordinateStr addObject:@"12.970760345459,80.2190093994141"];
[arrCoordinateStr addObject:@"12.9752297537231,80.2313079833984"];
[arrCoordinateStr addObject:@"12.9788103103638,80.2412414550781"];
for(int i = 0; i < name.count; i++)
{
NSArray *components = [[arrCoordinateStr objectAtIndex:i] componentsSeparatedByString:@","];
double latitude = [components[0] doubleValue];
double longitude = [components[1] doubleValue];
MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
mapPin.title = [name objectAtIndex:i];
mapPin.coordinate = coordinate;
[mapViewOffer addAnnotation:mapPin];
}
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:
(MKAnnotationView *)view
{
NSLog(@"%@",view.annotation.title);
NSLog(@"%f",view.annotation.coordinate.latitude);
NSLog(@"%f",view.annotation.coordinate.longitude);
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calloutTapped:)];
[view addGestureRecognizer:tapGesture];
}
-(void)calloutTapped:(UITapGestureRecognizer *) sender
{
NSLog(@"Callout was tapped");
MKAnnotationView *view = (MKAnnotationView*)sender.view;
id <MKAnnotation> annotation = [view annotation];
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
//[self performSegueWithIdentifier:@"annotationDetailSegue" sender:annotation];
OfferDetailsViewController *objOfferDetailsViewController = [[OfferDetailsViewController alloc]init];
[self.navigationController pushViewController:objOfferDetailsViewController animated:YES];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
static NSString *defaultPinID = @"annotationViewID";
pinView = (MKAnnotationView *)[mapViewOffer dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ){
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
}
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"placeholder"];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:@selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = infoButton;
return pinView;
}
#pragma标记MKMapView委托
-(无效)添加所有PinsonMapView
{
MKCoordinateRegion region=mapViewOffer.region;
region.center=CLLocationCoordinate2DMake(12.9752297537231,80.231307983984);
区域跨度纵向德尔塔=0.1f;
区域.跨度.纬度=0.1f;
[mapViewOffer setRegion:区域动画:是];
mapViewOffer.delegate=self;
arrMapPin=[[NSMutableArray alloc]init];
NSArray*名称=[[NSArray alloc]initWithObjects:
@“标题1”,
@“标题2”,
@“标题3”,无];
NSMutableArray*ArrCoordinateST=[[NSMutableArray alloc]initWithCapacity:name.count];
[arrcoordinardestr addObject:@“12.970760345459,80.2190093994141”;
[arrcoordinator addObject:@“12.9752297537231,80.231307983984”];
[arrcoordinardestr addObject:@“12.9788103638,80.2414550781”];
for(int i=0;i
它可以工作。谢谢请简要说明传递的参数以及注释标识符是什么?这类似于.net的标记属性吗?在我的要求中,我没有使用“someImage.png”,而是使用UIView并使用[annotationView addSubView:myView]。工作正常。但不在点击时调用“didSelectAnnotationView”。有什么问题吗?可能是您需要禁用该视图的用户交互。为什么要将标题设置为UIButtonTypedTailExposition类型的按钮?这有什么意义吗?如果您想用自定义点设置用户的位置,请从上面注释这些行<代码>如果([annotation isKindOfClass:[MKUserLocation class]])返回nil代码>一个小搜索会有很大的帮助:)并且:在发布一个长代码片段之前添加一些描述。