注釈のピンアイコンを動的テキストラベルで置き換えることはできますか?アイコンピンを注釈のテキストラベルで置き換えますか?
多分、CSSを使用するか、画像を動的に作成しますか?
たとえば、JavaScriptを使用したGoogle Maps APIでは、ラベルはCSSで行われます。
注釈のピンアイコンを動的テキストラベルで置き換えることはできますか?アイコンピンを注釈のテキストラベルで置き換えますか?
多分、CSSを使用するか、画像を動的に作成しますか?
たとえば、JavaScriptを使用したGoogle Maps APIでは、ラベルはCSSで行われます。
はい、可能です。
iOS MapKitでは、viewForAnnotation
デリゲートメソッドを実装し、UILabel
を追加してMKAnnotationView
を返す必要があります。例えば
:
-(MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *reuseId = @"reuseid";
MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
av = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease];
lbl.backgroundColor = [UIColor blackColor];
lbl.textColor = [UIColor whiteColor];
lbl.alpha = 0.5;
lbl.tag = 42;
[av addSubview:lbl];
//Following lets the callout still work if you tap on the label...
av.canShowCallout = YES;
av.frame = lbl.frame;
}
else
{
av.annotation = annotation;
}
UILabel *lbl = (UILabel *)[av viewWithTag:42];
lbl.text = annotation.title;
return av;
}
マップビューのdelegate
プロパティが設定されているそれ以外の場合は、このデリゲートメソッドが呼び出されず、かわりにデフォルトの赤いピンを取得しますことを確認してください。
上記のAnnaのコメントに記載されているデリゲートメソッドのSwift 3のバリエーションです。あなたのクラスがMKMapViewDelegateに準拠し、mapViewのデリゲートがviewDidLoad()でselfに設定されていることを確認してください。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = "reuseid"
var av = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if av == nil {
av = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
lbl.backgroundColor = .black
lbl.textColor = .white
lbl.alpha = 0.5
lbl.tag = 42
av?.addSubview(lbl)
av?.canShowCallout = true
av?.frame = lbl.frame
}
else {
av?.annotation = annotation
}
let lbl = av?.viewWithTag(42) as! UILabel
lbl.text = annotation.title!
return av
}
こんにちは、もう少し詳しく説明してください。あなたが試したことのいくつかのコードをしてください。 –