如何使用MKPointAnnotation向MKMapView添加注释?

时间:2021-03-01 19:37:17

I am trying to figure out how to add an instance of MKAnnotation to a MKMapView. I can't figure out what I am doing wrong...everything seems to go fine until I actually try and add the annotation to the mapView. Then I receive a SIGABRT error. Here is my code:

我试图弄清楚如何将MKAnnotation的实例添加到MKMapView。我无法弄清楚我做错了什么......一切似乎都没问题,直到我真的尝试将注释添加到mapView。然后我收到SIGABRT错误。这是我的代码:

lon = [[attributeDict objectForKey:@"long"] doubleValue];
lat = [[attributeDict objectForKey:@"lat"] doubleValue];
 MKPointAnnotation *point;
 CLLocation *theLocation = [[CLLocation alloc]initWithLatitude:lat longitude:lon];
 CLLocationCoordinate2D location;
 location.latitude = lat;
 location.longitude = lon;
 [point setCoordinate:(location)];
 [point setTitle:businessName];

 //ITS RIGHT HERE THAT I GET THE ERROR
 [theMap addAnnotation:point];

Do you have to set the region of the map first or something?

你必须首先设置地图的区域吗?

1 个解决方案

#1


14  

You need to alloc and init point:

你需要alloc和init point:

MKPointAnnotation *point = [[MKPointAnnotation alloc] init];

#1


14  

You need to alloc and init point:

你需要alloc和init point:

MKPointAnnotation *point = [[MKPointAnnotation alloc] init];