I would like to emulate the 'Drop Pin' feature in the Maps application. I have a mapview in my controller that I am able to add a MKPlacemark to. It doesn't respond to user action though. Can I emulate the dropped pin with stock classes or do I need to subclass an MKAnnotation View?
EDIT2:
Here's the code I'm trying, which I think should work. It drops the pin, and I can change the color but it can't be moved.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id
<MKAnnotation>)annotation
{
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:nil];
[pinView setDraggable:YES];
[pinView setAnimatesDrop:YES];
[pinView setPinColor:MKPinAnnotationColorGreen];
return pinView;
}
Here ya go: (iPhone) how to implement draggable pins using OS 4.0 MapKit?
With iOS 4 it's natively possible. Did you add an custom annotationView or a MKPinAnnotationView?
Look a the "draggable" property in MKAnnotationView.
Related
I have MKMapView with MKPlacemark on it. When showing the map, I'm showing place mark's title.
Everything is fine until now.
I want to disable hiding title when user touches it.
I tried to add
myMapView.userInteractionEnabled = NO;
myMapView.multipleTouchEnabled = NO;
Which helps, but completely disables interaction with map. I want to leave a possibility to zoom in/out and moving the map.
Instead of creating PIN, create your custom annotation. In custom annotation create the view with all the info that you want to present to user, and disable the place mark. By that you are not suppose to handle taps but you will show all the static/dynamic info attached to one lattitude/longitude.
The following code did the trick
for (UIGestureRecognizer *g in [myMapView gestureRecognizers])
[myMapView removeGestureRecognizer:g];
In MapKit Delegate method try bellow code
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *defaultPinID = #"com.invasivecode.pin";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[yourMapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
if (annotation == yourMapview.userLocation)
return nil;
pinView.pinColor = MKPinAnnotationColorRed;
pinView.userInteractionEnabled = NO;
//pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
return pinView;
}
here if you use your placemark with this logic may its work....
hope,this help you...
:)
I'm trying to put a little button in my AnnotationViews to show some info about some places on a map. Sounds super basic? Well. I must be an idiot then...
I've looked at multiple examples, searched the whole internet, and I have ripped of 95% of my hair. It just doesn't work!
-(MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)theAnnotation;
{
MKAnnotationView * annotationView = nil;
if([theAnnotation class] == [MyAnnotation class])
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:theAnnotation reuseIdentifier:#"annotation"];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.enabled = YES;
}
return annotationView;
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
{
NSLog(#"push %#", control);
}
Can some one please tell me why calloutAccessoryControlTapped never gets called?
I get a nice and pretty annotation, but when I tap the blue button it just closes. I have tried adding a target to the button, nothing.
Please help me!
PS: I know I should reuse old annotation view and stuff, but i stripped all code that wasn't necessary to be sure that there wasn't something else that was screwing thins up.
Make sure you have correctly set the delegate responding to the MKMapViewDelegate Protocol.
Also, make sure that all the views and superviews have userInterActionEnabled set to YES.
i tryed to make a customized pin on the map with green color and with a button.
but it doesnt work. I have a class named Placemark that implement mkannotation protocol.
This is my method that supposed to show a green pin but it doesnt:
- (MKAnnotationView *) map:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
i also want to specified that my MKMapview is called "map" and its an IBOutlet.
thanks
The method name must be mapView:viewForAnnotation:. The map view looks specifically for that name. You can change the internal parameter variable names but not the parts before the colons.
Try this:
- (MKAnnotationView *) mapView:(MKMapView *)map
viewForAnnotation:(id <MKAnnotation>) annotation
With that misspelling, the map view doesn't call your viewForAnnotation method and must be putting a default red pin on the map.
I have tried it both on an actual device (iPad) and the iPhone simulator (ios 4)
I see the map but no Apple headquarters (blue pin) even if I zoom in.
In my OnViewLoad function I have:
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.showsUserLocation=TRUE;
mapView.mapType=MKMapTypeHybrid;
[self.view insertSubview:mapView atIndex:0];
In your -mapView:viewForAnnotation: method, return nil if the annotation is a userLocation object:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isMemberOfClass:[MKUserLocation class]]) {
return nil;
}
// your normal code
}
This is needed to make sure, the iOS default implementation is used to show the blue userLocation dot.
I have implemented one map application in which i have display pin animation for current location.When i click on the pin at that time annotation view will open. But i want to display
annotation view without clicking on pin.Is it possible if possible then please give me idea about that.
Thanks in advance.
Try out:
It seems that [mapView selectAnnotation:annotation animated:YES] works too.
Hope this helps.
You just need to find your MKAnnotationView instance you want to select and call -setSelected:animated:.
For example, you can loop your annotations of an MKMapView like this:
for (YOURCLASSHERE *a in mapView.annotations) {
// Your current position (if shown) is an annotation as well. Thus check for the right class!
if ([a isKindOfClass:[YOURCLASSHERE class]]) {
// insert some smart if statement here, if you have multiple annotations!
[[mapView viewForAnnotation:a] setSelected:YES animated:YES];
}
}
YOURCLASSHERE is your class which implements the MKAnnotation protocol.
Of course the loop is superfluous if you already know your annotationView.
We can show the annotation with out clicking on it. Use the following code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *identifier = #"Pin";
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
pin.canShowCallout = YES;
[pin setSelected:YES animated:NO];
return [pin autorelease];
}