添加注释地图视图-Xamarin
我正在尝试用我的表视图添加注释。添加了初始注释,但每当我单击表视图调用其他位置时。它给了我一个错误(nullexception)。 有人能帮我吗添加注释地图视图-Xamarin,xamarin,Xamarin,我正在尝试用我的表视图添加注释。添加了初始注释,但每当我单击表视图调用其他位置时。它给了我一个错误(nullexception)。 有人能帮我吗 using System; using System.Drawing; using MonoTouch.Foundation; using MonoTouch.UIKit; using MonoTouch.MapKit; using MonoTouch.CoreLocation; namespace FieldMobileApp { publ
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.MapKit;
using MonoTouch.CoreLocation;
namespace FieldMobileApp
{
public partial class MainRightViewController : UIViewController
{
UITableView table;
MKMapView mapView;
BasicMapAnnotation annotation = new BasicMapAnnotation (new CLLocationCoordinate2D (48.857, 2.351), "Paris", "City of Light");
public MainRightViewController () : base ("MainRightViewController", null)
{
this.Title ="Right View Controller";
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
string[] tableItems = new string[] { "Harris", "Lubbock", "Galveston", "College Station", "Sugar Land", "Tulsa","Meyerland","Pearland","Linkwood","Valley Hills","Simav","Bursa"};
double[] lat = new double[] { 42, 36, 25, 18, 19, 58, 62, 10, 5, 6, 44, 55, 62 };
double [] lng= new double[] { -42, -36, -25, -18, -19, -58, -62, -10, -5, -6, -44, -55, -62 };
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
table = new UITableView (new RectangleF(80,80,250,500));
table.AutoresizingMask = UIViewAutoresizing.All;
table.Source = new TableSource (tableItems);
Add(table);
mapView = new MKMapView(new RectangleF (538,78,408,639));
mapView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
View.AddSubview(mapView);
// create our location and zoom for los angeles
var coords = new CLLocationCoordinate2D(48.857, 2.351);
// add a basic annotation
//var annotation = new BasicMapAnnotation (new CLLocationCoordinate2D (48.857, 2.351), "Paris", "City of Light");
mapView.AddAnnotation (annotation);
// Perform any additional setup after loading the view, typically from a nib.
}
public void ciz(int i)
{
var annotation = new BasicMapAnnotation (new CLLocationCoordinate2D (lat[i], lng[i]), "Tayfun Place", "City of Knowledge");
Console.WriteLine ("{0},{1}", lat [i], lng [i]);
// following line gives me an error, null exception!
mapView.AddAnnotation (annotation);
}
public class TableSource : UITableViewSource {
protected string[] tableItems;
protected string cellIdentifier = "TableCell";
public MainRightViewController parentController= new MainRightViewController();
public TableSource (string[] items)
{
tableItems = items;
}
/// <summary>
/// Called by the TableView to determine how many cells to create for that particular section.
/// </summary>
public override int RowsInSection (UITableView tableview, int section)
{
return tableItems.Length;
}
/// <summary>
/// Called when a row is touched
/// </summary>
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
new UIAlertView("Row Selected"
, tableItems[indexPath.Row], null, "OK", null).Show();
tableView.DeselectRow (indexPath, true);
}
/// <summary>
/// Called by the TableView to get the actual UITableViewCell to render for the particular row
/// </summary>
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
// request a recycled cell to save memory
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
// if there are no cells to reuse, create a new one
if (cell == null)
cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
cell.TextLabel.Text = tableItems[indexPath.Row];
return cell;
}
}
protected class BasicMapAnnotation : MKAnnotation
{
/// <summary>
/// The location of the annotation
/// </summary>
public override CLLocationCoordinate2D Coordinate { get; set; }
protected string title;
protected string subtitle;
/// <summary>
/// The title text
/// </summary>
public override string Title
{ get { return title; } }
/// <summary>
/// The subtitle text
/// </summary>
public override string Subtitle
{ get { return subtitle; } }
public BasicMapAnnotation (CLLocationCoordinate2D coordinate, string title, string subTitle)
: base()
{
this.Coordinate = coordinate;
this.title = title;
this.subtitle = subTitle;
}
}
}
}
使用系统;
使用系统图;
使用单调的基础;
使用MonoTouch.UIKit;
使用MonoTouch.MapKit;
使用MonoTouch.CoreLocation;
命名空间字段移动EAPP
{
公共部分类MainRightViewController:UIViewController
{
UITableView表;
MKMapView mapView;
BASICAMANNOTATION注释=新BASICAMANNOTATION(新CLLocationCoordinate2D(48.857,2.351),“巴黎”、“光明之城”);
public MainRightViewController():基(“MainRightViewController”,null)
{
this.Title=“右视图控制器”;
}
public override void direceivememorywarning()
{
//如果视图没有superview,则释放该视图。
base.DidReceiveMemoryWarning();
//释放所有未使用的缓存数据、图像等。
}
字符串[]tableItems=新字符串[]{“Harris”、“Lubbock”、“Galveston”、“College Station”、“Sugar Land”、“Tulsa”、“Meyerland”、“Pearland”、“Linkwood”、“Valley Hills”、“Simav”、“Bursa”};
double[]lat=新的double[]{42,36,25,18,19,58,62,10,5,6,44,55,62};
双[]液化天然气=新的双[]{42,-36,-25,-18,-19,-58,-62,-10,-5,-6,-44,-55,-62};
公共覆盖无效ViewDidLoad()
{
base.ViewDidLoad();
表=新UITableView(新矩形F(80,80250500));
table.AutoresizingMask=ui视图autoresizing.All;
table.Source=新表源(tableItems);
增加(表);
mapView=新的MKMapView(新矩形F(538,78408639));
mapView.AutoresizingMask=UIViewAutoresizing.FlexibleDimensions;
视图。添加子视图(mapView);
//为洛杉矶创建我们的位置和缩放
var coords=新的CLLocationCoordinate2D(48.857,2.351);
//添加基本注释
//var注释=新的BasicPannotation(新的CLLocationCoordinate2D(48.857,2.351),“巴黎”,“光明之城”);
mapView.AddAnnotation(注释);
//加载视图后,通常从nib执行任何附加设置。
}
公共空区(国际一)
{
var注释=新的BasicPannotation(新的CLLocationCoordinate2D(纬度[i],液化天然气[i]),“泰芬广场”,“知识之城”);
Console.WriteLine(“{0},{1}”,lat[i],lng[i]);
//下面的行给了我一个错误,null异常!
mapView.AddAnnotation(注释);
}
公共类表源:UITableViewSource{
受保护的字符串[]表项;
受保护的字符串cellIdentifier=“TableCell”;
public MainRightViewController parentController=新的MainRightViewController();
公共表源(字符串[]项)
{
tableItems=项目;
}
///
///由TableView调用以确定要为该特定部分创建多少单元格。
///
公共覆盖int RowsInSection(UITableView表格视图,int section)
{
返回表格项目。长度;
}
///
///在触摸行时调用
///
public override void RowSelected(UITableView tableView,NSIndexPath indexPath)
{
新建UIAlertView(“选定行”
,tableItems[indexath.Row],null,“确定”,null.Show();
tableView.row(indexPath,true);
}
///
///由TableView调用以获取要为特定行渲染的实际UITableViewCell
///
公共覆盖UITableViewCell GetCell(UITababVIEW TabLVIEW,MyLux.Posial.NSIXDEXPATH索引路径)
{
//请求回收单元以节省内存
UITableViewCell单元=tableView.DequeueReusableCell(单元标识符);
//如果没有可重用的单元,请创建一个新单元
if(单元格==null)
单元格=新的UITableViewCell(UITableViewCellStyle.Default,cellIdentifier);
cell.textlab.Text=tableItems[indexPath.Row];
返回单元;
}
}
受保护类BasicManotation:MKAnnotation
{
///
///注释的位置
///
公共重写CLLocationCoordinate2D坐标{get;set;}
受保护的字符串标题;
保护字符串字幕;
///
///标题文本
///
公共重写字符串标题
{获取{返回标题;}}
///
///字幕文本
///
公共覆盖字符串字幕
{获取{返回字幕;}}
公共基本符号(CLLocationCoordinate2D坐标、字符串标题、字符串副标题)
:base()
{
这个。坐标=坐标;
this.title=标题;
this.subtitle=副标题;
}
}
}
}
您不应该在TableSource实例中创建新的MainRightViewController,而是将调用它的实例传递给它
public partial class MainRightViewController : UIViewController
{
(...)
public override void ViewDidLoad ()
{
(...)
table.Source = new TableSource (tableItems, this);
在UITableViewSource中:
public class TableSource : UITableViewSource {
public MainRightViewController parentController;
public TableSource (string[] items, MainRightViewController parentController)
{
tableItems = items;
this.parentController = parentController
}
我希望这能帮助您继续。发布完整的堆栈跟踪。。。尽管我大概10个月后才回答;-)