![](/img/trans.png)
[英]How to use Google Maps on iOS using Xamarin.Forms.Maps?
[英]How to set background color and change font color on Label in xamarin.forms.maps (iOS)?
因為我未能在 iOS 項目上xamarin.forms.maps
上的引腳上的信息窗口中再插入兩行,所以我設法將它們放在信息窗口上,如下所示:
在 android 項目中,我有兩個擴展名為 .axml 的文件,其中我添加了兩個<TextView/>
,因此我設法在 InfoWindow 中的引腳上添加了另外兩行。
但是在這里我沒有(或不知道在哪里)在 pin 窗口本身中添加另外兩個字段,因此以這種方式添加它們:
1.我使用以下代碼添加來自數據庫的對象:
protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
MKAnnotationView annotationView = null;
if (annotation is MKUserLocation)
return null;
var customPin = GetCustomPin(annotation as MKPointAnnotation);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
annotationView = mapView.DequeueReusableAnnotation(customPin.Name);
if (annotationView == null)
{
annotationView = new CustomMKAnnotationView(annotation, customPin.Name);
//annotationView = new CustomMKAnnotationView(annotation, customPin.AlertLevel.ToString());
annotationView.Image = UIImage.FromFile("pin.png");
annotationView.CalloutOffset = new CGPoint(0, 0);
//annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("monkey.png"));
//annotationView.RightCalloutAccessoryView = UIButton.FromType(UIButtonType.DetailDisclosure);
((CustomMKAnnotationView)annotationView).Name = customPin.Name;
((CustomMKAnnotationView)annotationView).Url = customPin.Url;
//Add First Line
((CustomMKAnnotationView)annotationView).AlertLevel = customPin.AlertLevel;
//Add Second Line
((CustomMKAnnotationView)annotationView).CodeNum = customPin.CodeNum;
}
annotationView.CanShowCallout = true;
return annotationView;
}
2. 我用以下代碼顯示對象:
void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
CustomMKAnnotationView customView = e.View as CustomMKAnnotationView;
customPinView = new UIView();
if (customView.Name.Equals("Xamarin"))
{
customPinView.Frame = new CGRect(0, 0, 200, 84);
//var image = new UIImageView(new CGRect(0, 0, 200, 84));
//image.Image = UIImage.FromFile("xamarin.png");
//customPinView.AddSubview(image);
customPinView.Center = new CGPoint(0, -(e.View.Frame.Height + 75));
//the first text to enter in info window
var labelAlertLevel = new UILabel(new CGRect(60, 0, 200, 84));
labelAlertLevel.Text = "Код на трвога (1-4): " + customView.AlertLevel.ToString();
customPinView.AddSubview(labelAlertLevel);
//the second text to enter in info window
var labelCodeNum = new UILabel(new CGRect(60, 20, 200, 84));
labelCodeNum.Text = "Код на станция: " + customView.CodeNum.ToString();
customPinView.AddSubview(labelCodeNum);
e.View.AddSubview(customPinView);
}
}
基本上我想把它們放在 pin 窗口中,但如果這是不可能的,我該如何設置它們的背景和字體顏色?
我用這個例子。
以下代碼包括信息的正常顯示、自定義背景顏色和字體顏色,希望對你有幫助。
protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
MKAnnotationView annotationView = null;
if (annotation is MKUserLocation)
return null;
var customPin = GetCustomPin(annotation as MKPointAnnotation);
if (customPin == null)
{
return null;
}
annotationView = mapView.DequeueReusableAnnotation(customPin.Name);
if (annotationView == null)
{
annotationView = new CustomMKAnnotationView(annotation, customPin.Name);
annotationView.Image = UIImage.FromFile("pin.png");
annotationView.CalloutOffset = new CGPoint(0, 0);
((CustomMKAnnotationView)annotationView).Name = customPin.Name;
((CustomMKAnnotationView)annotationView).Url = customPin.Url;
((CustomMKAnnotationView)annotationView).Description1 = customPin.Description1;
((CustomMKAnnotationView)annotationView).Description2 = customPin.Description2;
((CustomMKAnnotationView)annotationView).Description3 = customPin.Description3;
annotationView.CanShowCallout = true;
}
configureDetailView(annotationView);
return annotationView;
}
void configureDetailView(MKAnnotationView annotationView)
{
int width = 100;
int height = 50;
var snapshotView = new UIView();
snapshotView.TranslatesAutoresizingMaskIntoConstraints = false;
NSDictionary views = NSDictionary.FromObjectAndKey(snapshotView, new NSString("snapshotView"));
snapshotView.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:[snapshotView(200)]", new NSLayoutFormatOptions(), null, views));
snapshotView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:[snapshotView(50)]", new NSLayoutFormatOptions(), null, views));
var options = new MKMapSnapshotOptions();
options.Size = new CGSize(width, height);
options.MapType = MKMapType.SatelliteFlyover;
options.Camera = MKMapCamera.CameraLookingAtCenterCoordinate(annotationView.Annotation.Coordinate, 250, 65, 0);
var snapshotter = new MKMapSnapshotter(options);
snapshotter.Start((snapshot, error) =>
{
if (snapshot != null)
{
UILabel label = new UILabel();
UILabel label2 = new UILabel();
UILabel label3 = new UILabel();
label.Text = ((CustomMKAnnotationView)annotationView).Description1;
label2.Text = ((CustomMKAnnotationView)annotationView).Description2;
label3.Text = ((CustomMKAnnotationView)annotationView).Description3;
label2.BackgroundColor = UIColor.Blue;
label3.TextColor = UIColor.Red;
label.Frame = new CGRect(50, 0, 100, 15);
label2.Frame = new CGRect(50, 15, 100, 15);
label3.Frame = new CGRect(50, 30, 100, 15);
snapshotView.AddSubviews(label, label2, label3);
}
});
annotationView.DetailCalloutAccessoryView = snapshotView;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.