iOS MapKit自定义引脚

时间:2011-08-22 10:02:25

标签: ios mapkit

如何在地图中显示图像而不是图钉。到目前为止,我只能添加引脚。 .m的示例代码非常有用,因为我还是iOS编程新手。

3 个答案:

答案 0 :(得分:53)

#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;
 }

编辑:

我可以向你解释MKAnnotationView的所有内容,但我认为你会发现Apple提供的文档比任何其他来源都要好得多。查看链接中的概述部分。

https://developer.apple.com/documentation/mapkit/mkannotationview

答案 1 :(得分:6)

转到Xcode的组织者,然后转到文档并搜索weatherMap,它显示了地图的示例,其中包含注释中的图像。

答案 2 :(得分:0)

#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;
}