如何在Mapview ios 8中将注释引脚添加为自定义图像

时间:2015-09-16 10:21:35

标签: ios objective-c iphone mkmapview mkannotation

我正在使用mapview并希望添加自定义图像以在地图视图中显示位置,如何添加我无法添加的图像。我用过的这段代码。

-(void)addAllPins
{
    self.mapView.delegate=self;
    for(int i = 0; i < name1.count; i++)
    {
        [self addPinWithTitle:name1[i] AndCoordinate:arrCoordinateStr[i]];
    }
}
-(void)addPinWithTitle:(NSString *)title AndCoordinate:(NSString *)strCoordinate
{
    MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];

    // clear out any white space
    strCoordinate = [strCoordinate stringByReplacingOccurrencesOfString:@" " withString:@""];

    // convert string into actual latitude and longitude values
    NSArray *components = [strCoordinate componentsSeparatedByString:@","];

    double latitude = [components[0] doubleValue];
    double longitude = [components[1] doubleValue];

    // setup the map pin with all data and add to map view
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);

    mapPin.title = title;
    mapPin.coordinate = coordinate;

   // UIImage *image = [UIImage imageNamed:@"hover.9.png"];
   // [[self.mapView viewForAnnotation:mapPin] setImage:image];

    [self.mapView addAnnotation:mapPin];
}

enter image description here

我也想设置缩放比例。有些人可以帮我解决这个问题。

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];

    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
   // NSLog(@"%@", [self deviceLocation]);

    //View Area

    MKCoordinateRegion region = self.mapView.region;
    region.center.latitude = self.locationManager.location.coordinate.latitude;
    region.center.longitude = self.locationManager.location.coordinate.longitude;
    region.span.longitudeDelta = 0.015;
    region.span.longitudeDelta = 0.015;
    [mapView setRegion:region animated:YES];

    [self addAllPins];

}

2 个答案:

答案 0 :(得分:0)

我的代码可以帮到你。你可以自定义引脚注释

- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id < MKAnnotation >)annotation
    {
        static NSString *reuseId = @"StandardPin";

        MKAnnotationView *aView = (MKAnnotationView *)[sender
                                                             dequeueReusableAnnotationViewWithIdentifier:reuseId];

        if (aView == nil)
        {
            aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
            aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            aView.canShowCallout = YES;
        }
        aView.image = [UIImage imageNamed : @"Location_OnMap"];
        aView.annotation = annotation;
        aView.calloutOffset = CGPointMake(0, -5);
        aView.draggable = YES;
        aView.enabled = YES;
        return aView;   
    }

答案 1 :(得分:0)

您需要使用mapView Delegate Method

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if (annotation == mapView.userLocation)
    {
        MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MKPinAnnotationView"];
        if (pinView ==nil) {
            pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"MKPinAnnotationView"];
            pinView.animatesDrop = YES;
        }
        pinView.canShowCallout = YES;
        pinView.pinColor = MKPinAnnotationColorGreen;//if you want
        return pinView;
    }
    else
    {
        static NSString *viewId = @"MKAnnotationView";
        MKAnnotationView *annotationView = (MKAnnotationView*)
        [mapView dequeueReusableAnnotationViewWithIdentifier:viewId];

        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc]
                              initWithAnnotation:annotation reuseIdentifier:viewId];
        }

        annotationView.canShowCallout=YES;
        annotationView.image = [UIImage imageNamed:@"yourImage"];//set your image here
        return annotationView;
    }
}

2。)缩放

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(yourLocation.coordinate, 100, 100);
    MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
    [self.mapView setRegion:adjustedRegion animated:YES];

这会有所帮助。谢谢。