0
它我的XML文件中,我已經數據通過XML解析試圖分析緯度和經度和註釋,我在我的字典裏已經
<?xml version="1.0" encoding="UTF-8"?>
<locations>
<location id="1">
<title>United Arab Emirates</title>
<latitude>23.424076</latitude>
<longitude>53.847818</longitude>
</location>
<location id="2">
<title>Afghanistan</title>
<latitude>33.93911</latitude>
<longitude>67.709953</longitude>
</location>
<location id="3">
<title>Australia</title>
<latitude>-25.274398</latitude>
<longitude>133.775136</longitude>
</location>
<location id="4">
<title>Bangladesh</title>
<latitude>23.684994</latitude>
<longitude>90.356331</longitude>
</location>
</locations>
所有位置========== ===================== 在於其我.m文件我寫的代碼
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self dataParsing];
NSLog(@"%@",locSMutArr);
[self setUpMapMethod];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)dataParsing{
NSString *strFile=[[NSBundle mainBundle] pathForResource:@"xmlPractise1" ofType:@"xml"];
NSData *dataObj=[NSData dataWithContentsOfFile:strFile];
NSXMLParser *parseObj=[[NSXMLParser alloc]initWithData:dataObj];
parseObj.delegate=self;
[parseObj parse];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - NSXMLParser Delegate
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict{
if ([elementName isEqualToString:@"locations"]) {
locSMutArr=[[NSMutableArray alloc]init];
}
else if([elementName isEqualToString:@"location"]){
locMutDic=[[NSMutableDictionary alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if (refString==nil) {
refString=[[NSMutableString alloc]init];
}
else{
refString=[NSMutableString stringWithFormat:@"%@%@",refString,string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{
if ([elementName isEqualToString:@"latitude"] || [elementName isEqualToString:@"longitude"]) {
[locMutDic setValue:refString forKey:elementName];
}
else if ([elementName isEqualToString:@"location"]){
[locSMutArr addObject:locMutDic];
}
refString=nil;
}
-(void)setUpMapMethod{
for (int i=0; i<4; i++) {
CLLocationCoordinate2D CLLocObj;
CLLocObj.latitude=[locSMutArr indexOfObject:[locMutDic valueForKey:@"latitude"]];
CLLocObj.longitude=[locSMutArr indexOfObject:[locMutDic valueForKey:@"longitude"]];
MKCoordinateRegion viewRegion=MKCoordinateRegionMakeWithDistance(CLLocObj, 100, 100);
[_mapOutlet setRegion:viewRegion];
MKPointAnnotation *annotaionObj=[[MKPointAnnotation alloc]init];
[annotaionObj setCoordinate:CLLocObj];
[_mapOutlet addAnnotation:annotaionObj];
}
}
@end
我想使用循環創建CLLocationCoordinate2D對象,用於xml字典中的lat和lon的所有值。 也可以在那裏註釋所有位置。 全部
============================ 它是我的.h文件,我聲明瞭所有的objs。
@interfaceViewController:UIViewController<NSXMLParserDelegate,CLLocationManagerDele gate,MKMapViewDelegate>{
NSMutableArray *locSMutArr;
NSMutableDictionary *locMutDic;
NSMutableString *refString;
NSMutableArray *mutLatArr;
}
@property (weak, nonatomic) IBOutlet MKMapView *mapOutlet;
@end
產量是我對你的答案有幫助? – Gulliva