简体   繁体   中英

How to add annotation pin as custom image in Mapview ios 8

I am using mapview and want to add custom image to show the location in map view , how to add image i am not able to add. this code i have used.

-(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];
}

在此处输入图片说明

i want to set zoom in scale also . can some help me to solve this.

-(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];

}

My code is help you.You can put custom pin annotation

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

You need to use 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.) to zoom

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

It will help.Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM