這是從美團弄得xml文件,地區和經緯度。 你點了地區以後 , 就可以查看經緯度 ,因為筆者懶, 有現成的文本框 , 所有偷懶了。 下麵是一些枯燥的代碼了 。 ...
這是從美團弄得xml文件,地區和經緯度。
你點了地區以後 , 就可以查看經緯度 ,因為筆者懶, 有現成的文本框 , 所有偷懶了。
下麵是一些枯燥的代碼了 。
#import <UIKit/UIKit.h> #import "RootTableViewController.h" @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window.rootViewController=[[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStyleGrouped]]; return YES; }
#import <UIKit/UIKit.h> #import "SecondViewController.h" @interface RootTableViewController : UITableViewController<NSXMLParserDelegate> @property(strong,nonatomic)NSMutableArray *arr; @property(strong,nonatomic)NSMutableDictionary *dic1; @property(strong,nonatomic)NSString *str; @property(strong,nonatomic)NSMutableArray *arrname; @end
#import "RootTableViewController.h" @interface RootTableViewController () @end @implementation RootTableViewController - (void)viewDidLoad { [super viewDidLoad]; self.title=@"城市列表"; self.arrname=[NSMutableArray array]; NSURL *url=[NSURL URLWithString:@"http://www.meituan.com/api/v1/divisions?mtt=1.help%2Fapi.0.0.im7coqq1"]; NSData *data=[NSData dataWithContentsOfURL:url]; NSXMLParser *parser=[[NSXMLParser alloc]initWithData:data]; parser.delegate=self; BOOL bol=[parser parse]; NSLog(@"%d",bol); [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"]; } /** * 文檔開始解析 * * @param parser parser */ - (void)parserDidStartDocument:(NSXMLParser *)parser { self.arr=[NSMutableArray array]; } /** * 解析完畢 * * @param parser parser */ - (void)parserDidEndDocument:(NSXMLParser *)parser { NSLog(@"%@",self.arr); } /** * 文檔元素 解析開始 * * @param parser 解析的對象 * @param elementName 元素的名稱 * @param namespaceURI 命名空間 * @param qName * @param attributeDict 屬性的字典 */ -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict { if ([elementName isEqualToString:@"division"]) { self.dic1=[NSMutableDictionary dictionary]; [self.dic1 setDictionary:attributeDict]; } } /** * 文檔中元素 解析結束 * * @param parser 解析的對象 * @param elementName 元素的名稱 * @param namespaceURI 命名空間 * @param qName 屬性的字典 */ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName { if ([elementName isEqualToString:@"name"]||[elementName isEqualToString:@"latitude"]||[elementName isEqualToString:@"longitude"]) { [self.dic1 setObject:self.str forKey:elementName]; } else if ([elementName isEqualToString:@"division"]) { [self.arr addObject:self.dic1]; } } /** * 解析文件元素的內容 * * @param parser 解析對象 * @param string 顯示的文本內容 */ - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { self.str=string; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath]; for (NSDictionary *dic in self.arr) { [self.arrname addObject:dic[@"name"]]; } cell.textLabel.text=self.arrname[indexPath.row]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%@",self.arr[indexPath.row]); SecondViewController *secondvc=[[SecondViewController alloc] init]; secondvc.strweidu=self.arr[indexPath.row][@"latitude"]; secondvc.strjindu=self.arr[indexPath.row][@"longitude"]; [self presentViewController:secondvc animated:YES completion:nil]; } @end
#import "ViewController.h" #import "RootTableViewController.h" @interface SecondViewController : ViewController @property(strong,nonatomic)UILabel *lblname; @property(strong,nonatomic)UILabel *lblpwd; @property(strong,nonatomic)UITextField *name; @property(strong,nonatomic)UITextField *pwd; @property(strong,nonatomic)UIButton *buton; @property(strong,nonatomic)NSString *strweidu; @property(strong,nonatomic)NSString *strjindu; @end
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; UIImageView *image=[[UIImageView alloc]initWithFrame:self.view.frame ]; [image setImage:[UIImage imageNamed:@"15EADA084F41FF349CED23058FD34D0E"]]; [self.view addSubview:image]; self.lblname=[[UILabel alloc]initWithFrame:CGRectMake(50, 200, 100, 50)]; self.lblname.text=@"精度"; [self.view addSubview:self.lblname]; self.lblpwd=[[UILabel alloc]initWithFrame:CGRectMake(50, 330, 100, 50)]; self.lblpwd.text=@"維度"; [self.view addSubview:self.lblpwd]; self.name=[[UITextField alloc] initWithFrame:CGRectMake(150, 200, 200, 50)]; [self.view addSubview:self.name]; self.name.text =self.strweidu; self.name.borderStyle=UITextBorderStyleRoundedRect; self.pwd=[[UITextField alloc] initWithFrame:CGRectMake(150, 330, 200, 50)]; [self.view addSubview:self.pwd]; self.pwd.text=self.strjindu; self.pwd.borderStyle=UITextBorderStyleRoundedRect; self.buton=[[UIButton alloc] initWithFrame:CGRectMake(120, 400, 174, 66)]; self.buton.backgroundColor=[UIColor colorWithRed:0.532 green:1.000 blue:0.161 alpha:1.000]; self.buton.layer.cornerRadius=10; [self.buton setTitle:@"確認" forState:0]; [self.buton setTitleColor:[UIColor whiteColor] forState:0]; [self.buton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.buton]; } -(void)next { RootTableViewController *root=[[RootTableViewController alloc]init]; [self presentViewController:root animated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end