I am trying to change my pin colour to purple, when I do it I lose the title though. Code is:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBarHidden=YES;
//init the location manager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager requestWhenInUseAuthorization];
self.mapView.showsUserLocation=YES;
self.userGeoPoint=self.message[#"userLocation"];
self.pinView = [[MKPointAnnotation alloc] init];
self.pinView.title=self.message[#"fromUser"];
self.coord = CLLocationCoordinate2DMake(self.userGeoPoint.latitude, self.userGeoPoint.longitude);
self.pinView.coordinate=self.coord;
//use a slight delay for more dramtic zooming
[self performSelector:#selector(addPin) withObject:nil afterDelay:0.5];
}
-(void)addPin{
[self.mapView addAnnotation:self.pinView];
[self.mapView selectAnnotation:self.pinView animated:YES];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotationPoint
{
if ([annotationPoint isKindOfClass:[MKUserLocation class]])//keep the user as default
return nil;
static NSString *annotationIdentifier = #"annotationIdentifier";
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];
pinView.pinColor = MKPinAnnotationColorPurple;
//now we can throw an image in there
return pinView;
}
I tried setting the title property for MKPinAnnotation but there isn't one. Is there anyway I can get around this?
In viewForAnnotation, you need to set canShowCallout to YES (it's NO by default):
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;
A couple of unrelated points:
It looks like you have a property named pinView of type MKPointAnnotation that you are using to create the annotation object in viewDidLoad. Then in viewForAnnotation, you have a local variable also named pinView of type MKPinAnnotationView. Although there is no naming conflict here, it causes and implies some conceptual confusion. MKPointAnnotation is an annotation model class while MKPinAnnotationView is an annotation view class -- they are completely different things. It would be better to name the annotation property pin or userAnnotation for example.
This comment in viewForAnnotation:
//now we can throw an image in there
seems to imply that you could set a custom image in the view at this point. Setting a custom image in a MKPinAnnotationView is not recommended since that class is designed to display default pin images in one of three colors. To use a custom image, create a plain MKAnnotationView instead.
The pinColor property was deprecated in iOS 9. From iOS 9 onwards you should use pinTintColor which takes a UIColor rather than a MKPinAnnotationColor.
Old:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.pinColor = MKPinAnnotationColorPurple;
New:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.pinTintColor = [UI Color purpleColor];
There is more information available in the Apple docs.
I just upgrade to ios 10, apple changed the api.
pinColor no longer in use, instead, use tintColor
New:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.tintColor = [UIColor purpleColor];
Note that the pin title will be displayed when the user taps it:
If the value of this property is true, a standard callout bubble is shown when the user taps a selected annotation view. The callout uses the title and subtitle text from the associated annotation object.
( https://developer.apple.com/documentation/mapkit/mkannotationview/1452451-canshowcallout )
Related
I have an MKMapView that I add pins to. They load correctly with their relevant graphics but if I zoom in and zoom back out they loose their graphic and turn into a standard red pin with the only customisation being the pin name (even my disclosure indicator disappear).
So far to try and fix it I've tried:
Tried png’s, checked on faster device, Changed everything from MKPinAnnotation to MKAnnotation, returning to a normal MKAnnotation instead of my custom CBAnnotation, Various sample codes for loading custom pins, Lowered quality of map overlay in case it was a loading issue but still an issue.
- (void)addPins {
mapPinsArray = [[NSMutableArray alloc] init];
for (MapPoint *mappoint in mapPointsArray) {
CBAnnotation *annotation = [[CBAnnotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(mappoint.loclat, mappoint.loclong);
annotation.title = mappoint.stopAreaName;
annotation.mapPoint = mappoint;
[mapPinsArray addObject:annotation];
[self.myMapView addAnnotation:annotation];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(CBAnnotation *)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
//do nothing
return nil;
} else {
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"trailPoint"];
annotationView.canShowCallout = YES;
ButtonWithData *accessoryViewButton = [[ButtonWithData alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[accessoryViewButton setBackgroundImage:[UIImage imageNamed:#"right_arrow"] forState:UIControlStateNormal];
accessoryViewButton.buttonData = annotation.mapPoint;
[accessoryViewButton addTarget:self action:#selector(disclosureButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = accessoryViewButton;
if (![[NSUserDefaults standardUserDefaults] boolForKey:annotation.mapPoint.stopAnimalName]) {
annotationView.image = [annotation.mapPoint lockedPinImage];
} else {
annotationView.image = [annotation.mapPoint unlockedPinImage];
}
return annotationView;
}
}
Fixed my own problem. I subclassed MKMapView to GenericMapView so that my code was cleaner (removing showsUserLocation, zoom/scroll/rotateEnabled, showsCompass etc. from the actual View Controller) but this meant the delegate wasn't setting correctly and viewForAnnotation wasn't being called on the pins reload.
I've been searching for hours now to find a good tutorial on how to add annotations given a class that holds locations and images of an object (in my case, object is called as EVENT).
Check out what I've done so far, I'm calling this method setupAnnotations from viewDidLoad.
- (void)setupAnnotations {
_allEvents = [EventEntity MR_findAll];
for (EventEntity *event in _allEvents) {
AVEventAnnotationView *annotation = [[AVEventAnnotationView alloc] initWithOperator:event];
CLLocation *eventLocation = [[CLLocation alloc] initWithLatitude:[[event latitude] doubleValue] longitude:[[event longitude] doubleValue]];
annotation.coordinate = eventLocation.coordinate;
MKPlacemark *eventPlacemark = [[MKPlacemark alloc] initWithCoordinate:eventLocation.coordinate addressDictionary:nil];
MKMapItem *eventItem = [[MKMapItem alloc] initWithPlacemark:eventPlacemark];
// Im loading image from URL for or based on event object
UIImageView *imageContainer = [[UIImageView alloc] init];
[imageContainer sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",fullBannerImgPath,[event bannerImg]]]];
[_bannerImagesArray addObject:imageContainer];
BOOL hasAnnotation = NO;
for (AVEventAnnotationView *ann in self.mapView.annotations) {
if([ann isKindOfClass:[AVEventAnnotationView class]])
{
if([[annotation.eventEntity dataId] isEqualToString:[ann.eventEntity dataId]])
{
hasAnnotation = YES;
break;
}
}
}
if(!hasAnnotation)
[self.mapView addAnnotation:annotation];
}
}
And here's my mapview delegate:
- (MKAnnotationView *)mapView:(MKMapView *)mapViewIn viewForAnnotation:(id <MKAnnotation>)annotation {
if (mapViewIn != self.mapView || [annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString *annotationIdentifier = #"SPGooglePlacesAutocompleteAnnotation";
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
// STUCK HERE
annotationView.image = [UIImage imageNamed:#"LocationBlue"];
annotationView.frame = CGRectMake(0, 0, 80, 80);
annotationView.draggable = NO;
annotationView.canShowCallout = YES;
return annotationView;
}
I'm new to implementing MKMapView. I just read awhile ago the difference between annotation and annotation view.
Anyway, back to my main question or my main problem: How to put annotation/annotationview to mkmapview given I have an array of EVENTS with latitude, longitude and Images?
I don't know what is AVAnnotationView but I would say you don't need it. You just have to make your Event class conform to MKAnnotation protocol, that is add the property coordinate of type CLLocationCoordinate2D to the Event class. After this just add the events to the map with addAnnotations.
In your mapView:viewForAnnotation: you determine what is the annotation that is being displayed (what is the event that is being displayed) and so you get the image you want to display (you may use tags or any other mean) and set the image for annotation view.
Or you just say something like:
if event = annotation as? AVEventAnnotationView {
//add your image and do stuff
}
Below, I have created an Annotation for my map which works perfectly and shows up with the title and subtitle as it should.
But I wish to add a small image to the left of the annotation but can't figure out what I need to do to the below code to make it work.
// Annotation
CLLocationCoordinate2D poseLocation;
poseLocation.latitude = POSE_LATITUDE;
poseLocation.longitude = POSE_LONGITUDE;
Annotation *myAnnotation = [[Annotation alloc] init];
myAnnotation.coordinate = poseLocation;
myAnnotation.title = #"Pose Beauty Salon";
myAnnotation.subtitle = #"100, Moneyhaw Road";
[self.myMapView addAnnotation:myAnnotation];
You will need to set the delegate of your myMapView first and implement the viewForAnnotation delegate method.
There you will return MKAnnotationView instance which has a property leftCalloutAccessoryView:
The view to display on the left side of the standard callout bubble.
The default value of this property is nil. The left callout view is
typically used to display information about the annotation or to link
to custom information provided by your application. The height of your
view should be 32 pixels or less.
In the leftCalloutAccessoryView, you can assign your image there. For example:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.myMapView dequeueReusableAnnotationViewWithIdentifier:#"annotation"];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"annotation"];
UIImageView *imageView = //initialize your image view here
annotationView.leftCalloutAccessoryView = imageView;
}
annotationView.annotation = annotation;
return annotationView;
}
PS: Apparently you have asked similar question before here: Add image to the left of my annotations. I am not sure you need to post another question. Please try to implement this first.
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
myImage = [UIImage imageNamed:imagename];
CGRect cropRect = CGRectMake(0.0, 0.0,35.0, 35.0);
myImageView = [[UIImageView alloc] initWithFrame:cropRect];
myImageView.clipsToBounds = YES;
myImageView.image = myImage;
MyPin.leftCalloutAccessoryView =myImageView;
MyPin.highlighted=YES;
MyPin.canShowCallout=YES;
return MyPin;
}
It works for me , try this one
I have populated a couple of locations on MKMapView using MKPointAnnotation(s). On tapping annotations I am showing some options with UIActionSheet menu. The options have some delete functionality which would delete the selected annotation on map when user taps the delete option on UIActionSheet. The issue is that I am not able to determine which annotation point is clicked, I seem to have no reference to it.
The code that adds annotation point is:
while(looping array of locations)
{
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = {coord of my location}
annotationPoint.title = [anObject objectForKey:#"castTitle"];
annotationPoint.subtitle = [anObject objectForKey:#"storeName"];
[self.mainMapView addAnnotation:annotationPoint];
}
The code to show UIActionSheet on tapping annotation is:
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.pinColor = [self getAnnotationColor];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self action:#selector(showOptions:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
-(IBAction)showOptions:(id)sender
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(#"", #"") delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel", #"Cancel") destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(#"Delete", #"Delete"), nil];
[sheet showInView:[self.view window]];
}
You can also Create a class which inherits from MKPointAnnotation, and add an id property , and use your class :
#interface MyPointAnnotation : MKPointAnnotation
#property int pointId;
#end
and then use it in your view controller:
create annotation:
MyPointAnnotation *myAnnotation = [[MyPointAnnotation alloc]init];
myAnnotation.coordinate= someCoordinates;
[myAnnotation setTitle:#"i am annotation with id"];
myAnnotation.pointId = 1;
[self.mapView addAnnotation:myAnnotation];
if you have an array of coordinates you can loop and create annotations.
and you can even customize annotation view by its id:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *view=(MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:#"reuseme"];
if (!view) {
view=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"reuseme"];
}
if (((MyPointAnnotation *)annotation).pointId == 1)
{
//the annotation with id 1.
}
return view;
}
Seems like there are two approaches.
If only one annotation can be selected at a time, you could access the -selectedAnnotations property of the enclosing MKMapView.
Another approach is to inspect sender in showOptions:, which is a reference to the UIButton that triggered the action. Find out its enclosing MKAnnotationView, which will give you the associated -annotation. You could then either stash this as an ivar or (my preferred approach) use some runtime magic - in the form of objc_setAssociatedObject(), declared in <objc/runtime.h> - to attach a reference to the annotation to the action sheet, allowing easy retrieval in its delegate callback.
(You could actually do this back in the button creation phase if you wanted, and attach a reference to the annotation to the UIButton, which can be picked up directly in showOptions: and reattached to the action sheet.
But [MKMapView selectedAnnotations] I would think is the easier way to go if it suits your needs.
I have the following method in my MapViewController:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"MapVC"];
if (!annotationView) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"MapVC"];
annotationView.canShowCallout = YES;
annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// could put a rightCalloutAccessoryView here
} else {
annotationView.annotation = annotation;
[(UIImageView *)annotationView.leftCalloutAccessoryView setImage:nil];
}
return annotationView;
}
I believe it's properly set up, but when my map shows my annotations with title and subtitle properly, but they don't show the detail disclosure button, am I missing something?
Another thing is that when debugging this method is never called, yet the annotation view shows up with title and subtitle.
Most likely the map view's delegate is not set.
If the delegate is not set or if you don't implement the viewForAnnotation method, the map view will create a default annotation view which is a red pin with a callout containing only the title and subtitle (unless the title is blank in which case you will get a pin but no callout).
Either connect the map view's delegate outlet to File's Owner or in code add this (eg. in viewDidLoad before the annotation is added):
mapView.delegate = self;
Also, if you're not using ARC, add autorelease to the alloc lines to avoid a memory leak.