adhg
adhg

Reputation: 10863

Drop a pin on MKMapView

iPhone newbie is here coming from Java. So my objective at this stage is to allow the user to 'drop a pin' on the map. My initialization of the map looks like this:

- (void)viewDidLoad {
    [super viewDidLoad];
     NSLog(@"your view did load, I'm going to initizlie the map by your location");
     CLLocationCoordinate2D location = theMap.userLocation.coordinate;
     NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);

     MKCoordinateRegion region;
     MKCoordinateSpan span;

     NSLog(@"coordinates: %f %f",location.latitude,location.longitude);
     if (TARGET_IPHONE_SIMULATOR) {
         NSLog(@"You're using the simulator:");
         location.latitude  =  40.8761620;
         location.longitude = -73.782596;
     } else {
         location.latitude  =  theMap.userLocation.location.coordinate.latitude;
         location.longitude =  theMap.userLocation.location.coordinate.longitude;
     }

     span.latitudeDelta = 0.001;
     span.longitudeDelta = 0.002;

     region.span = span;
     region.center = location;

     [theMap setRegion:region animated:YES];
     [theMap regionThatFits:region];
     [theMap setMapType:MKMapTypeSatellite]; 
     [theMap setZoomEnabled:YES];
     [theMap setScrollEnabled:YES];
     [theMap setShowsUserLocation:YES];
}

For the requested pin drop I have

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil;
    if (annotation != theMap.userLocation) {
        static NSString *defaultPinID = @"aPin";
        pinView = (MKPinAnnotationView *)[theMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if (pinView == nil)
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    } else {
    }
    pinView.pinColor = MKPinAnnotationColorRed;
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;
    return pinView;
}

I'm not sure I fully understand how this map (theMap) works for pins in viewForAnnotation? I mean, what action the user does will activate the viewForAnnotation method? This code doesn't work and I'm not sure why.

I'm using the simulator so I'm not sure if there's a button I should press or Alt click it?

Upvotes: 2

Views: 2226

Answers (2)

adhg
adhg

Reputation: 10863

ok, after a while, I understood what went wrong:

theMap.delegate = (id) self;

in the constructor was missing. Once I did that any action by end user will activate other methods (protocols) of the map.

Upvotes: 1

Caleb
Caleb

Reputation: 124997

I'm not sure I fully understand how this map (theMap) works for pins in viewForAnnotation?

MKPinAnnotationView is just another kind of annotation view -- that is, you add an annotation (an object conforming to the MKAnnotation protocol) to the map. When the map wants to display the annotation (maybe because the user scrolled the map so that the annotation is in view), it asks you for a view to use to represent the annotation. At that point, your mapView:viewForAnnotation: method can fetch or create a pin annotation view and return that. The user doesn't do anything directly to trigger mapView:viewForAnnotation:, except for scrolling or zooming.

If you want to the user to be able to drop a pin, that's a different thing. You'll need to provide a view (possibly even a MKPinAnnotationView) that they can drag around. When they indicate that they want to drop the pin (perhaps by lifting their finger), you remove the view and add an appropriate annotation at that point. Then the map view will ask you for a view to represent the annotation by calling its delegate's mapView:viewForAnnotation: method.

This code doesn't work and I'm not sure why.

Have you added any annotations to the map? If so, are you looking at the part of the map where they should be displayed?

I'm guessing that you're looking at the animatesDrop property and expecting it to do the entire user pin-dropping interaction. It doesn't do that. Setting that property to YES merely animates the pin as it appears on the map.

Upvotes: 2

Related Questions