Xcode6 IOS8 mapkit pins not showing - xcode6

i wondering if someone may be able to help me. I am new to xcode and im trying to build a basic app. i have followed this tutorial
http://rshankar.com/how-to-add-annotation-to-mapview-in-ios/
i have copied the source code directly, i don't get any errors or warnings, however when running the app the pin locations do not show. Im not sure if they are there ( just invisible ) or whether they are not showing at all. I can't seem to find the issue.
Would anyone be able to suggest what the problem could be?
Thanks in advance.

actually managed to fix this, turns out the coordinates were round the wrong way!!!! Noob error!! I would like to change the callout on the pins if anyone could shed some light on where to change the code on the tutorial.
Thanks

To change the callout of the pins:
make the class that has your map view implement the <MKMapViewDelegate> protocol
set the delegate of your map view to that class (e.g. mapView.delegate = self;)
implement MKMapViewDelegate's (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation method in that class
something like this:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// If it's the user location, just return nil
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle custom annotations
if ([annotation isKindOfClass:[NAME_OF_CLASS_IMPLEMENTING_MKANNOTATION_PROTOCOL class]])
{
// Try to dequeue an existing annotation view first
MKAnnotationView *annotationView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"AnnotationViewIdentifier"];
if (!annotationView)
{
// If an existing pin view was not available, create one
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"AnnotationViewIdentifier"];
annotationView.canShowCallout = YES;
// set callout
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = rightButton;
}
else
{
annotationView.annotation = annotation;
}
return annotationView;
}
return nil;
}
On your map pins (which should be made from a class that implements the MKAnnotation protocol), you should be able to set the title and subtitle properties.

Related

Can't select an annotation?

I have a single annotation on a map view. I can select it programmaticly, but the I tap it nothing happens. Could you help me? Did anyone encounter similar problem? Here is mehod for setting up anotations:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"MapVC"];
if (!aView) {
aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"MapVC"];
aView.canShowCallout = YES;
aView.draggable=YES;
aView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
// could put a rightCalloutAccessoryView here
}
aView.annotation = annotation;
[(UIImageView *)aView.leftCalloutAccessoryView setImage:nil];
return aView;
}
And adding them to map view:
- (void)updateMapView
{
if (self.mapView.annotations) [self.mapView removeAnnotations:self.mapView.annotations];
if (self.annotation) [self.mapView addAnnotation:self.annotation];
}
And mehod reacting to pressing of annotations:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView
{
NSLog(#"did select annotation");
}
By the way, method [self.mapView selectAnnotation:annotation] works, but doesn't put up a callout(i checked it with breakpoint). While just taping annotation doesn't(again cheked through breakpoints).
If an annotation's title is nil or blank, the callout will not show (even if everything else is set properly including canShowCallout).
When you tap on an annotation, the didSelectAnnotationView delegate method will get called and if the annotation has a non-blank title, the callout will be displayed.
Regarding your question in the comments:
...is it right I have a seperate class to wrap all my data in to, my
annotation class contains an instance of that data class?
There's nothing wrong with this.
If you want to keep map-related logic separate from the base class, that's fine and probably a good idea for a complex app where the base data class may be used for more than just annotations.
If your app is very simple and the data is only used for annotations, you could keep things very simple and combine the two but it's not a requirement.
As long as you stick to using direct references instead of trying to, for example, use array indexes or view/button tags to link back to some data object from the annotation, the "right" class implementation depends on what works for your app.
Try setting canShowCallout property of the MKAnnotationView to YES in case you didn't.

Changing an MKAnnotationView custom image?

I know you can create a custom annotation view using something like:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *annotationView = [[[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomAnnotation"] autorelease];
annotationView.image = [UIImage imageNamed:#"customPin.png"];
return annotationView;
}
.. but how do I change the image in other parts of my code.. (after it has been created with the above)?
You probably don't need the answer anymore, but still, the question is unanswered. What I usually do is add a property to the annotation, telling which image should be used. It can be a BOOL, a UIImage, or pretty much whatever you like.
In viewForAnnotation, I check for that value and set the appropriate image.
Whenever I want to update the image, I change the property's value, and I remove and add the annotation :
[theMapView removeAnnotation: myAnnotation];
[theMapView addAnnotation: myAnnotation];
That way, the annotation is re-drawn.

didAddAnnotationViews not working on MKMapView

I've been playing around with the MKMapView and trying get my head around how the MKMapViewDelegate system works. So far I have no luck in getting the didAddAnnotationViews to get called when the current location marker is added.
I have set my app delegate to implement MKMapViewDelegate, I have an Outlet to the MapView in my xib and have set the delegate property of the MapView to be self, as in the app delegate instance. I have implemented didAddAnnotationViews in the app delegate which I simply NSLog any calls to it as shown below. The map is set to show current location which it does and adds the blue pin annotation on startup, but for some reason didAddAnnotationViews is not being hit.
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
NSLog(#"Annotation added!");
}
Any ideas what I might have missed?
I came across the same issue in BNR. Here is what I ended up using:
// Tell MKMapView to zoom to current location when found
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"didUpdateUserLocation just got called!");
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 250, 250);
[mapView setRegion:region animated:YES];
}
mapView:didAddAnnotations: is only called in response to addAnnotation: or addAnnotations:. The users location pin will not trigger this delegate method.
Just wanted to confirm that I was able to get this working using
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 250,250);
[mv setRegion:region animated:YES];
}
Make sure you are using
mapView.delegate = self;
or
[mapView setDelegate:self];

ViewForAnnotation is not being called

I am trying to make a map, where I can see my current location, and see what the street is called.
so far, I am able to put a pin on my map, but for some reason, I am not getting the callout.
and I have put a NSLog in my viewForAnnotation method, but it is not being called, so i wasn't able to test it.
can someone help me?
-(void)lat:(float)lat lon:(float)lon
{
CLLocationCoordinate2D location;
location.latitude = lat;
location.longitude = lon;
NSLog(#"Latitude: %f, Longitude: %f",location.latitude, location.longitude);
//One location is obtained.. just zoom to that location
MKCoordinateRegion region;
region.center=location;
//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta=.005f;
span.longitudeDelta=.005f;
region.span=span;
[map setRegion:region animated:TRUE];
//MKReverseGeocoder *geocoder=[[MKReverseGeocoder alloc] initWithCoordinate:location];
//geocoder.delegate=self;
//[geocoder start];
if (cPlacemark != nil) {
[map removeAnnotation:cPlacemark];
}
cPlacemark=[[CustomPlacemark alloc] initWithCoordinate:location];
cPlacemark.title = mPlacemark.thoroughfare;
cPlacemark.subtitle = mPlacemark.locality;
[map addAnnotation:cPlacemark];
[cPlacemark release];
[mLocationManager stopUpdatingLocation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// try to dequeue an existing pin view first
if ([annotation isKindOfClass:[CustomPlacemark class]]){
MKPinAnnotationView *pinView=(MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:#"customIdentifier"];
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView* cPinAnnoView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:#"customIdentifier"] autorelease];
cPinAnnoView.pinColor = MKPinAnnotationColorPurple;
cPinAnnoView.animatesDrop = YES;
cPinAnnoView.canShowCallout = YES;
// Add button
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[leftButton addTarget:self action:#selector(annotationViewClick:) forControlEvents:UIControlEventTouchUpInside];
cPinAnnoView.leftCalloutAccessoryView = leftButton;
} else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
Right now I have customized my viewForAnnotation to be like this.
But I still can't get a callout from my pin and the pin remains red.
But it should be purple of nothing at all
I had the same problem which was not setting the MapView delegate to the File Owner.
Open your nib
Right click on the MapView
Drag the delegate to the File's Owner
I had the same problem, as you mentioned. The delegate had been set to ViewController, but the viewForAnnotation selector was not being called. After some checks, I realized if you do not call addAnotation in the main thread, mapView would not call viewForAnnotation, so following update resolved my problem:
Before:
[_mMapView addAnnotation:marker];
After:
dispatch_async(dispatch_get_main_queue(), ^{
[_mMapView addAnnotation:marker];
});
In order to get the viewForAnnotation to be called, add mapView.delegate=self; to e.g. the viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
mapView.delegate=self;
}
Could it be that your annotation has been added outside the current view area of the MKMapView?
For storyboard, Ctl drag the MKMapView to the orange circle on the bottom bar of ViewController, and select delegate.
This will solve the problem.
As vatrif mentioned in the comments, you must set your delegate BEFORE adding annotations to your MKMapView object.
Others have already explained, odds are high you have not connected your mapview delegate to your controller. Its the first thing to check
i have been working in ios 9 Mapview related app and I experienced the same problem.
somehow I solved my problem, in my case im resizing the mapview.
I added delegate after i resize the mapview. it works now perfectly.!
After having set the delegate for the mapview if still the viewforannotation not getting called then this is something which you have missed - set the self.mapView.showsUserLocation to YES, in interface builder you can tick the shows userLocation option in attributes inspector.

How to differentiate between annotation pins?

I have three pins on a mapview. I'd like to give each one a different color. In the delegate method viewForAnnotation, I'm doing this:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"anAddress"];
annView.pinColor = MKPinAnnotationColorGreen;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
I was thinking to create an array of MKPinAnnotationViews but how can I get the correct one since the delegate method isn't indexed to anything?
To distinguish between UIView, you can use the tag property and its corresponding viewWithTag: method.
However, in your context, I would recommend adding the color to your annotation class. Then you can ensure that you don't reuse the same caller for multiple pins.