【问题标题】:How to add annotation pin as custom image in Mapview ios 8如何在 Mapview ios 8 中添加注释引脚作为自定义图像
【发布时间】:2015-09-16 10:21:34
【问题描述】:

我正在使用 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];
}

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

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

}

【问题讨论】:

标签: ios objective-c iphone mkmapview mkannotation


【解决方案1】:

我的代码对你有帮助。你可以放自定义引脚注释

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

【讨论】:

    【解决方案2】:

    你需要使用mapView的委托方法

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

    会有帮助的。谢谢。

    【讨论】:

    • 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];
    猜你喜欢
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多