Longer Subtitles in Mapview Annotations (Swift)

Longer subtitles in MapView annotations (swift)

I figured it out, I added a label in viewForAnnotation and it just worked

¯\_(ツ)_/¯

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}

let reuseId = "pin"

var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
}
else {
pinView!.annotation = annotation
}

//THIS IS THE GOOD BIT
let subtitleView = UILabel()
subtitleView.font = subtitleView.font.fontWithSize(12)
subtitleView.numberOfLines = 0
subtitleView.text = annotation.subtitle!
pinView!.detailCalloutAccessoryView = subtitleView

return pinView
}

How to display 2 lines of text for subtitle of MKAnnotation and change the image for the button on the right?

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];

// Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 23, 23);
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

[button setImage:[UIImage imageNamed:yourImageName] forState:UIControlStateNormal];
[advertButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];

annView.rightCalloutAccessoryView = button;

// Image and two labels
UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,23,23)];
[leftCAV addSubview : yourImageView];
[leftCAV addSubview : yourFirstLabel];
[leftCAV addSubview : yourSecondLabel];
annotationView.leftCalloutAccessoryView = leftCAV;

annotationView.canShowCallout = YES;

return pin;
}

UPDATE

The default style for annotations only supports the title and subtitle. Neither title nor subtitle can include line breaks. You cannot do this without subclassing.

To use a custom view review Apple's sample code:

http://developer.apple.com/library/ios/#samplecode/WeatherMap/Introduction/Intro.html

I also think there is a problem in your code

UILabel *l1=[[UILabel alloc] init];
l1.frame=CGRectMake(0, 15, 50, 50);
l1.text=@"First line of subtitle";
l1.font=[UIFont fontWithName:@"Arial Rounded MT Bold" size:(10.0)];

UILabel *l2=[[UILabel alloc] init];
l2.frame=CGRectMake(0, 30, 50, 50);
l2.text=@"Second line of subtitle";
l2.font=[UIFont fontWithName:@"Arial Rounded MT Bold" size:(10.0)];
[leftCAV addSubview : l1];
[leftCAV addSubview : l2];

l1 has a frame (0, 15, 50, 50) and l2 has (0, 30, 50, 50). Wont these two overlap? I mean l1 will start from y=15 and its height is 50. so when l2 starts from 30 it may overlap.. Can you pls check by changing the frames

How to add title and subtitle to my annotations in swift 3?

You need set canShowCallout true to show title and subtitle.

Try the following code.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "myAnnotation") as? MKPinAnnotationView

if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotation")
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}

if let annotation = annotation as? MyPointAnnotation {
if #available(iOS 9.0, *) {
annotationView?.pinTintColor = annotation.pinTintColor
} else {
// Fallback on earlier versions
}
}

return annotationView
}

Add extra detail to MKPointAnnotation other than title and subtitle

make your own annotation, new file or class

import MapKit

class MyAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var EXTRA_INFORMATION: String?
var title: String?

init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
}
}

and use it instead of normal MKPointAnnotation

var zoopin = MyAnnotation()
zoopin.coordinate = zoo
zoopin.title = "The zoo"
zoopin.subtitle = "hello this is the zoo"
zoopin.EXTRA_INFORMATION = "that is your new extra info that you wanted to add?"
mapView.addAnnotation(zoopin)

Setting the title and subtitle on a Map Annotation view

Use MKPointAnnotation instead.

Sample Code :

CLPlacemark *topresult = [placemarks objectAtIndex:0];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = topresult.location.coordinate;
annotation.title = self.business.businessName;
annotation.subtitle = self.business.phoneNumber;
[self.mapView addAnnotation:annotation];

Swift Annotation subtitle height and width

in iOS 9 we have a new property named detailCalloutAccessoryView

You can create a view and set as

annotationView.detailCalloutAccessoryView = tempView 

Please check the link to get more details

MapKit iOS 9 detailCalloutAccessoryView usage

Update MapKit annotation subtitle and image

I am sorry if I haven't finally understood your situation quite well, but I feel like I am moving towards solving a similar problem so I'll try to share my thoughts with you. I will use your words as the guides for my response:

"you have to wait for those 10 seconds before being authorized to open the map"

Do you know about the following <MKOverlayView> protocol methods (see http://developer.apple.com/library/IOs/#documentation/MapKit/Reference/MKOverlayView_class/Reference/Reference.html):

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
- (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale

(Assuming that you have 5000 pins on entire world map, not on every fx 1km, i.e. not SO LARGE amount of pins) You can split your five thousands into the portions, so each portion of pins is to be populated by the corresponding MKMapRect region which is being drawn on the map. So your map is populated with pins/annotations, by demand, only for particular region at any given time. Depending on your situation this could bring a huge speed up.

This is a very good demonstration of working with MKMapRect tiles: https://github.com/mtigas/iOS-MapLayerDemo - populate with information only particular pieces of map. Also you may google something like "Map tiles 256px" to understand how the maps are being drawn.

"I understand that you have to remove all the annotations, and recreate them all in order to do that."

Why recreate? You do have an access to all the annotations that are already there on your map - why not just iterate through your associated annotations classes and update theirs subtitles?

Something like:

  • iPad Mapkit - Change title of "Current Location"
  • Annotation callout title does not change

I hope that my points do address the scope of your question/situation.

Not updating the annotation title and subtitle in mkMapView in iphone at appropriate time

i startUpdatingLocation my locationManager in loadView rather than viewDidLoad and it works fine for me...



Related Topics



Leave a reply



Submit