MKMapview place pin at location (long/lat)

后端 未结 6 1702
说谎
说谎 2021-02-01 06:31

I have latitude and long values and I need to be able to drop a pin at this location.

Can anybody provide some advice on how to go about this?

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 07:33

    Please use this code. its working fine.
    -(void)addAllPinsOnMapView
    {
    MKCoordinateRegion region = mapViewOffer.region;
    region.center = CLLocationCoordinate2DMake(23.0225, 72.5714);
    region.span.longitudeDelta= 0.1f;
    region.span.latitudeDelta= 0.1f;
    [mapViewOffer setRegion:region animated:YES];
    mapViewOffer.delegate=self;
    
    MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(23.0225, 72.5714);
    mapPin.title = @"Title";
    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);
    }
    
    - (MKAnnotationView *)mapView:(MKMapView *)theMapView 
    viewForAnnotation:(id )annotation
    {
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        ((MKUserLocation *)annotation).title = @"Current Location";
        return nil;
    }
    else
    {
        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;
        pinView.rightCalloutAccessoryView.tag=1;
        return pinView;
    }
    }
    - (MKPinAnnotationView*)myMap:(MKMapView*)myMap viewForAnnotation:
    (id)annotation{
    
    MKPinAnnotationView *pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPin"];
    
    UIImage *icon = [UIImage imageNamed:@"bustour.png"];
    UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(8,0,32,37)];
    
    if(icon == nil)
        NSLog(@"image: ");
    else
        NSLog(@"image: %@", (NSString*)icon.description);
    
    [iconView setImage:icon];
    [pin addSubview:iconView];
    pin.canShowCallout = YES;
    pin.pinColor = MKPinAnnotationColorPurple;
    
    return pin;
    }
    
    - (IBAction)btnLocateMe:(UIButton *)sender
    {
    [mapViewOffer setCenterCoordinate:mapViewOffer.userLocation.location.coordinate animated:YES];
    }
    

提交回复
热议问题