我正在尝试从plist加载注释并在地图上显示,但遇到问题:
a)动态分配坐标,和
b)在地图上显示它们。
当我在物理上分配纬度和长度时:
tempCoordinate.latitude = 53.381164;
tempCoordinate.longitude = -1.471798;
地图是用注释绘制的,但它似乎将它们全部绘制在同一个地方?
我一直试图解决这个问题很久了,还有更好的方法吗?任何帮助将不胜感激。我开始考虑手动将它们全部输入为对象。
这是我的plist ....
我想制作两种类型的注释,每种注释都创建为对象,然后添加到单独的数组中,以便我可以在地图视图上切换它们。
这是我的annotations.h课程:
@property (copy) NSString *streetName;
@property (copy) NSString *bays;
@property (copy) NSString *type;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithAnnotationName:(NSString *)newStreetName
theBays:(NSString *)newBays
theType:(NSString *)newType
theCoordinate:(CLLocationCoordinate2D)newCoordinate;
这里是annotations.m:
if ((self = [super init])) {
type = [newType copy];
streetName = [newStreetName copy];
bays = [newBays copy];
coordinate = newCoordinate;
}
这是mapView.h
parkingArray1 = [[NSMutableArray alloc] init];
//NSMutableArray *testArr = [[NSMutableArray alloc] init];
//path of plist
NSString *fullPathToPList=[[NSBundle mainBundle] pathForResource:@"AnnotationList" ofType:@"plist"];
//temp values for dictionary enumeration and creating annotation object
NSDictionary *plistDict, *BlueBadgeDict, *BlueBadgeContentsDict;
plistDict = [NSDictionary dictionaryWithContentsOfFile: fullPathToPList];
//enumerate through BlueBadge dictionary
BlueBadgeDict = [plistDict valueForKey: @"BlueBadge"];
for (NSString *firstLevelString in BlueBadgeDict){
BlueBadgeContentsDict = [BlueBadgeDict valueForKey:firstLevelString];
NSString *tempStreetName, *tempBays, *tempType, *tempLat, *tempLong;
CLLocationCoordinate2D tempCoordinate;
for (NSString *secondLevelString in BlueBadgeContentsDict){
//assign temp values from dict to string
if ([secondLevelString isEqualToString:@"StreetName"])
tempStreetName = [BlueBadgeContentsDict valueForKey:secondLevelString];
else if ([secondLevelString isEqualToString:@"Bays"])
tempBays = [BlueBadgeContentsDict valueForKey:secondLevelString];
else if ([secondLevelString isEqualToString:@"Longitude"])
tempLong = [BlueBadgeContentsDict valueForKey:secondLevelString];
else if ([secondLevelString isEqualToString:@"Latitude"])
tempLat = [BlueBadgeContentsDict valueForKey:secondLevelString];
}
//pass plist root value
tempType = firstLevelString;
tempCoordinate.latitude = [tempLat doubleValue];
tempCoordinate.longitude = [tempLong doubleValue];
//create object
Annotation *tempAnnotation = [[Annotation alloc] initWithAnnotationName:tempStreetName theBays:tempBays theType:tempType theCoordinate:tempCoordinate];
//add the object to the mutable array
[parkingArray1 addObject:tempAnnotation];
[tempAnnotation release];
}
//display array on map
[self.mapView addAnnotations:parkingArray1];
这是xml代码
<dict>
<key>BlueBadge</key>
<dict>
<key>BB1</key>
<dict>
<key>Bays</key>
<string>4</string>
<key>Latitude</key>
<string>-1.466822</string>
<key>Longitude</key>
<string>53.37725</string>
<key>StreetName</key>
<string>Arundel Lane</string>
</dict>
<key>BB2</key>
<dict>
<key>Bays</key>
<string>5</string>
<key>Latitude</key>
<string>-1.471994</string>
<key>Longitude</key>
<string>53.381071</string>
<key>StreetName</key>
<string>Balm Green</string>
</dict>
<key>BB3</key>
<dict>
<key>Bays</key>
<string>1</string>
<key>Latitude</key>
<string>-1.466739</string>
<key>Longitude</key>
<string>53.374164</string>
<key>StreetName</key>
<string>Brittain Street</string>
</dict>
</dict>
</dict>
答案 0 :(得分:1)
代码似乎没问题。
我看到的唯一问题是plist中的坐标是向后的。
例如,BB1位于纬度-1和经度53位于印度洋的某个地方。
如果停车场实际上在英国谢菲尔德,请翻转plist中的坐标。
答案 1 :(得分:0)
我可以看到代码中的错误。
1你需要像这样使用for循环中的数组
NSArray *array = [BlueBadgeContentsDict allKeys];
for(NSString *secondLevelString in array)
{
.....
}
2而不是这个
tempCoordinate.longitude = [tempLat doubleValue];
你需要这样做
tempCoordinate.longitude = [tempLong doubleValue];