试图通过 xml 解析来解析纬度和经度并注释我在字典中的所有位置

Posted

技术标签:

【中文标题】试图通过 xml 解析来解析纬度和经度并注释我在字典中的所有位置【英文标题】:trying to parsing latitude and longitude through xml parsing and annotate all locations that i've in my dictionary 【发布时间】:2017-01-06 21:56:39 【问题描述】:

这是我的 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

我想使用 for 循环为 xml 字典中的所有 lat 和 lon 值创建 CLLocationCoordinate2D 对象。 并且还注释了所有位置。 都这样

============================= 这是我声明所有 obj 的 .h 文件。

 @interfaceViewController:UIViewController<NSXMLParserDelegate,CLLocationManagerDele gate,MKMapViewDelegate>
    NSMutableArray *locSMutArr;
    NSMutableDictionary *locMutDic;
    NSMutableString *refString;
    NSMutableArray *mutLatArr;



@property (weak, nonatomic) IBOutlet MKMapView *mapOutlet;

@end

【问题讨论】:

我的回答对您有帮助吗? 【参考方案1】:

所以这是适合我的代码

import "ViewController.h"


@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *locations;
@property(nonatomic,strong)NSMutableDictionary *location;
@property(nonatomic,strong)NSString *openElement;
@end

@implementation ViewController

- (void)viewDidLoad 
    [super viewDidLoad];
    _locations = [[NSMutableArray alloc]init];

    [self parseData];
    [self logData];
    [self setupMap];
    // Do any additional setup after loading the view, typically from a nib.



-(void)viewDidAppear:(BOOL)animated
    [super viewDidAppear:animated];


-(void)logData
    NSLog(@"%@",_locations);


-(void)parseData
    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];

    NSData *data = [[NSData alloc]initWithContentsOfFile:path];
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
    parser.delegate = self;
    [parser parse];



- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict
    if([elementName isEqualToString:@"location"])
        _location = [[NSMutableDictionary alloc]init];
    else if([elementName isEqualToString:@"locations"])
        NSLog(@"Start");
    else
        _openElement = elementName;



- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    if(![string containsString:@"\n"])
        [_location setObject:string forKey:_openElement];


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName
    if([elementName isEqualToString:@"location"])
       [_locations addObject:_location];


- (void)parser:(NSXMLParser *)parser foundAttributeDeclarationWithName:(NSString *)attributeName forElement:(NSString *)elementName type:(nullable NSString *)type defaultValue:(nullable NSString *)defaultValue
    NSLog(@"%@",attributeName);


- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


-(void)setupMap
    for(NSMutableDictionary *dict in _locations)
        CLLocationCoordinate2D CLLocObj;
        CLLocObj.latitude=[(NSString*)[dict valueForKey:@"latitude"] floatValue];
        CLLocObj.longitude=[(NSString*)[dict valueForKey:@"longitude"] floatValue];
        MKCoordinateRegion viewRegion=MKCoordinateRegionMakeWithDistance(CLLocObj, 100000, 100000);
        [_mapView setRegion:viewRegion];
        MKPointAnnotation *annotaionObj=[[MKPointAnnotation alloc]init];
        [annotaionObj setCoordinate:CLLocObj];
        [annotaionObj setTitle:[dict valueForKey:@"title"]];
        [_mapView addAnnotation:annotaionObj];

    

这是我的 NSMutableArray 与 NSMutableDictionary 的输出

(
        
        latitude = "23.424076";
        longitude = "53.847818";
        title = "United Arab Emirates";
    ,
        
        latitude = "33.93911";
        longitude = "67.709953";
        title = Afghanistan;
    ,
        
        latitude = "-25.274398";
        longitude = "133.775136";
        title = Australia;
    ,
        
        latitude = "23.684994";
        longitude = "90.356331";
        title = Bangladesh;
    
)

【讨论】:

以上是关于试图通过 xml 解析来解析纬度和经度并注释我在字典中的所有位置的主要内容,如果未能解决你的问题,请参考以下文章

快速拟合注释

如何获得当前的纬度和经度?

解析经度和纬度以在谷歌地图上再次使用它[关闭]

在自定义纬度/经度处创建新的 CLLocationCoordinate2D

在 Java 中平滑经度纬度点列表

通过纬度和经度从特定半径获取用户[重复]