2011-09-20 98 views
0

我有以下代码:添加注释值

MKCoordinateRegion region; 
MKCoordinateSpan span; 
span.latitudeDelta=0.0005; 
span.longitudeDelta=0.0005; 

CLLocationCoordinate2D location = mapView.userLocation.coordinate; 

for (int i = 0; i < [appDelegate.markers count]; i++) { 
    marker *aMarker = [appDelegate.markers objectAtIndex:i]; 


location.latitude = [[aMarker.lat objectAtIndex:i] floatValue]; 
location.longitude =[[aMarker.lng objectAtIndex:i] floatValue]; 

region.span=span; 
region.center=location; 

if(addAnnotation != nil) 
{ 
[mapView removeAnnotation:addAnnotation]; 
[addAnnotation release]; 
addAnnotation = nil; 
} 

addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location]; 
[mapView addAnnotation:addAnnotation]; 
} 

我在XMLparser类解析的经度和纬度。现在我想在Map上的buttonclick事件上添加注释。有人可以纠正我的代码吗?

+0

我在尝试这么多后没有在地图上找到注释... –

+1

当你运行它时会发生什么?为什么使用索引'i'来访问'aMarker'对象中的'markers'数组_和'lat'和'lng'?为什么你将'location'设置为'userLocation',然后用'markers'的值覆盖它?你对“地区”做了什么?你应该在调用mapView addAnnotation之前记录'location'的值。另外,'addAnnotation'对于注释变量来说是一个非常混乱的名字。 – Anna

+0

你解决了你的问题吗?如果不是,告诉我什么是位置,它是否是Cllocation点数组?只有当你的位置是一个CLLocation数组时,你才会得到注释。 – iCoder4777

回答

0

警告NSString may not respond to -objectAtIndex装置latlngNSString对象(其不具有objectAtIndex方法)。

你并不需要调用objectAtIndexlatlng --they是在特定对象,你刚刚从阵列中检索到的值。 (顺便说一下,当你在编译编译时得到警告 - 而不是当你在你的注释中“运行”它时。其次,不要忽略编译器警告。在这种情况下,当你运行代码你会得到一个异常:NSInvalidArgumentException - unrecognized selector。)

另一个问题是循环删除之前添加的注释添加当前之前。如果你想把所有的标记添加到地图上,这是没有意义的。目前,它最终只会将最后一个标记添加到地图中。如果你真的只想添加最后一个标记,那么你不需要遍历数组(只需直接从数组中获取最后一个标记并从中创建一个注释)。

相反,如果你想所有标记添加到地图,循环应该是这样的:

for (int i = 0; i < [appDelegate.markers count]; i++) 
{ 
    marker *aMarker = [appDelegate.markers objectAtIndex:i]; 

    location.latitude = [aMarker.lat floatValue]; 
    location.longitude =[aMarker.lng floatValue]; 

    AddressAnnotation *addrAnnot = [[AddressAnnotation alloc] initWithCoordinate:location]; 
    [mapView addAnnotation:addrAnnot]; 
    [addrAnnot release]; 
} 

目前尚不清楚你与region做什么。如果您尝试设置区域以便地图视图显示所有注释,请查看此问题的答案:iOS MKMapView zoom to show all markers

+0

它对我来说就像魔术一样。感谢您所做的更正安娜 –

+0

嗨安娜,我正在更新iOS6的这个应用程序,它正在崩溃在上面建议的方法。声明如下nsarray在枚举错误时发生了变异。你能否建议我在上述方法中进行调整 –