#iphone #mkmapview #mkannotation
#iPhone #mkmapview #mkannotation
Вопрос:
Я смотрю на пример MapCallouts от Apple для аннотаций к карте и выносок (== пузырьки, которые появляются при нажатии на значок). У каждой аннотации там есть координаты, заголовок и субтитры. Я хотел бы отобразить субтитр в 2 строки, я пытался с:
- (NSString *)subtitle
{
return @"Founded: June 29, 1776nSecond line text";
}
но текст «Текст второй строки» остается в одной строке и делает пузырь шире.Я понимаю это:
Я также хотел бы изменить изображение кнопки на одно из моих собственных, код, который устанавливает кнопку, в настоящее время выглядит следующим образом:
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
Есть идеи?
РЕДАКТИРОВАТЬ: Я попробовал совет 7KV7. Смена кнопки прошла успешно, но я все еще не могу получить субтитры в 2 строки. Мой код:
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
// Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 23, 23);
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//UIEdgeInsets titleInsets = UIEdgeInsetsMake(7.0, -20.0, 7.0, 7.0);
//button.titleEdgeInsets = titleInsets;
[button setImage:[UIImage imageNamed:@"ic_phone_default.png"] forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:@"ic_phone_selected.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"ic_phone_selected.png"] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = button;
//two labels
UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,50)];
//[leftCAV addSubview : button];
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];
customPinView.leftCalloutAccessoryView = leftCAV;
//customPinView.
customPinView.canShowCallout = YES;
return customPinView;
Я понимаю это:
Комментарии:
1. вы нашли решение этой проблемы?
Ответ №1:
пожалуйста, используйте это
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[CustomAnnotation class]]) {
CustomAnnotation *customAnnotation = (CustomAnnotation *) annotation;
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];
if (annotationView == nil)
annotationView = customAnnotation.annotationView;
else
annotationView.annotation = annotation;
//Adding multiline subtitle code
UILabel *subTitlelbl = [[UILabel alloc]init];
subTitlelbl.text = @"sri ganganagar this is my home twon.sri ganganagar this is my home twon.sri ganganagar this is my home twon. ";
annotationView.detailCalloutAccessoryView = subTitlelbl;
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150];
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
[subTitlelbl setNumberOfLines:0];
[subTitlelbl addConstraint:width];
[subTitlelbl addConstraint:height];
return annotationView;
} else
return nil;
}
Ответ №2:
- (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;
}
Обновить
Стиль аннотаций по умолчанию поддерживает только заголовок и подзаголовок. Ни заголовок, ни субтитр не могут содержать разрывы строк. Вы не можете сделать это без создания подклассов.
Чтобы использовать пользовательский вид, просмотрите пример кода Apple:
http://developer.apple.com/library/ios/#samplecode/WeatherMap/Introduction/Intro.html
Я также думаю, что в вашем коде есть проблема
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 есть рамка (0, 15, 50, 50), а у l2 есть (0, 30, 50, 50). Эти две строки не будут перекрываться? Я имею в виду, что l1 будет начинаться с y = 15, а его высота равна 50. итак, когда l2 начинается с 30, это может перекрываться .. Можете ли вы, пожалуйста, проверить, изменив рамки
Ответ №3:
Вот как я достиг этого, не создавая подклассов для представления аннотаций или используя недокументированные API:
MKAnnotationView* pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
// Add a custom view to the the callout to allow mutli-line text
UIView *calloutView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 250, 250)];
UIView *calloutViewHeight = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 1, 250)]; // The reason for this will become apparent below
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 21)];
[title setText:<<your single-line title text>>];
[calloutView addSubview:title];
UILabel *subtitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 250, 250)];
[subtitle setNumberOfLines:0];
[subtitle setText:<<your multi-line title text>>];
CGSize subtitleSize = [subtitle sizeThatFits:CGSizeMake(250, 250)];
[subtitle setFrame:CGRectMake(10, 35, 250, subtitleSize.height)];
[calloutView addSubview:subtitle];
[calloutView setFrame:CGRectMake(0, 10, 250, 35 subtitleSize.height 10)];
[calloutViewHeight setFrame:CGRectMake(0, 0, 1, 35 subtitleSize.height 10)];
pinView.leftCalloutAccessoryView = calloutView;
pinView.rightCalloutAccessoryView = calloutViewHeight; // The height of the callout is calculated by the right accessory *not* the left, so this fake view is required
Вы можете настроить шрифт / цвет текста, настроив title
и subtitle
соответственно.
Конечно, это своего рода взлом, но он достигает желаемых результатов без создания подклассов и позволяет вам настраивать любые дополнительные кнопки / изображения, которые вы хотели бы добавить calloutView
(и вы все еще можете размещать кнопки в виде правого аксессуара, если хотите, при условии сохранения высоты).
Ответ №4:
Две строки в выноске невозможно выполнить, не обращаясь к неподдерживаемым API. Если вы это сделаете, ваше приложение будет отклонено.
Если у вас iPad, вы можете увидеть пример настраиваемой выноски. Их выноска преобразуется во всплывающее окно, которое привязывается к местоположению на карте таким же образом, как и выноска. Так что, возможно, в следующей версии iOS Apple расширит callout API. Пока вы застряли с однострочными субтитрами.
Если вы действительно хотите это сделать, вам придется создать свой собственный класс выноски и привязать его к карте при прокрутке. Это, вероятно, было бы сложно.
Другая идея заключается в подделке выноски. Создайте пользовательский вид аннотации, содержащий 2 вида. Первый — это пин-код карты. Вторая — это выноска. Когда pin-код получает касание, вы можете отобразить свой вид выноски. Анимация «pop» будет отсутствовать, но вы можете вставить столько строк, сколько захотите.
Я никогда не пробовал этого, и звучит так, как будто нужно разобраться во множестве деталей, но это может дать вам начало.
Ответ №5:
Для настройки кнопки вы можете использовать приведенный ниже код
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *x=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"someIdentifier"];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
//set the target, title, image etc.
[x setRightCalloutAccessoryView:button];
return [x autorelease];
}
Комментарии:
1. мне это не помогло, но я проголосовал за это, потому что у меня появилась другая идея с этим ответом
Ответ №6:
Вы не сможете сделать это с помощью общедоступного API и должны будете переопределить свой собственный пользовательский вид аннотации.
Вы можете взглянуть на этот код, который переопределяет вид аннотации, но имитирует стандартный для iOS, и затем вы можете дополнить его по своему усмотрению :
http://blog.asolutions.com/2010/09/building-custom-map-annotation-callouts-part-1/
Ответ №7:
Это решение устанавливает «выровненный» текст, надеюсь, будет полезным:
Swift 3:
import MapKit
extension MKAnnotationView {
func setCustomLines(customLines lines: [String]) {
if lines.count <= 0 {
return
}
// get longest line
var longest = NSString()
if let max = lines.max(by: { $1.characters.count > $0.characters.count }) {
longest = max as NSString
}
else {
fatalError("Can't get longest line.")
}
// get longest text width
let font = UIFont.systemFont(ofSize: 15.0)
let width = (longest.size(attributes: [NSFontAttributeName: font])).width
// get text from lines
var text = lines.first
for i in 1..<lines.count {
text = text! " " lines[i]
}
// label
let label = UILabel()
label.text = text
label.font = font
label.numberOfLines = lines.count
label.textAlignment = .justified
// set width of label
let con = NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: width)
label.addConstraint(con)
self.detailCalloutAccessoryView = label
}
}
Ответ №8:
Это можно сделать за 9 шагов.
Я создал ярлык и добавил его в annotationView?.detailCalloutAccessoryView
свойство (шаг 5). Я также установил текст метки на annotation.subtitle
текст (шаг 2 и шаг 4). Я устанавливаю метку .numberOfLines = 0
. Это заставит субтитр расширяться на сколько угодно # строк (установите значение 2, если вам нужно только 2).
Для кнопки я создал UIButton и установил его тип на .detailDisclosure
(шаг 7). Затем я устанавливаю для свойства изображения кнопки пользовательское изображение, которое я хочу использовать (шаг 8). И, наконец, я устанавливаю для кнопки .rightCalloutAccessoryView
свойство annotationView (шаг 9).
Я также изменил .calloutOffset
свойство annotationView (шаг 6). Я получил смещение от Рэя вендерлиха
Шаги приведены в комментариях над кодом
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
let reuseIdentifier = "reuseIdentifier"
var annotationView = mapView.mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
// 1. set the annotationView's canShowCallout property to true
annotationView?.canShowCallout = true
// 2. get the subtitle text from the annontation's subtitle property
let subtitleText = annotation.subtitle ?? "you have no subtitle"
// 3. create a label for the subtitle text
let subtitleLabel = UILabel()
subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
// 4. set the subtitle's text to the label's text property and number of lines to 0
subtitleLabel.text = subtitleText
subtitleLabel.numberOfLines = 0
// 5. set the annotation's detailCalloutAccessoryView property to the subtitleLabel
annotationView?.detailCalloutAccessoryView = subtitleLabel
// 6. offset the annotationView's .calloutOffset property
annotationView?.calloutOffset = CGPoint(x: -5, y: 5)
// 7. create a button and MAKE SURE to set it's type to .detailDisclosure or it won't appear
let button = UIButton(type: .detailDisclosure)
// 8. set the button's image to whatever custom UIImage your using
button.setImage(UIImage(named: "yourCustomImage"), for: .normal)
// 9. set the button to the annotationView's .rightCalloutAccessoryView property
annotationView?.rightCalloutAccessoryView = button
} else {
annotationView!.annotation = annotation
}
return annotationView
}