UITableViewController和XML解析還有地圖的簡單結合,ofbizcontroller.xml
然後My Code就按照上面的這個順序輸出。
#import <Foundation/Foundation.h>#import <MapKit/MapKit.h>@interface MapAnnotation : NSObject<MKAnnotation>@property(nonatomic,readwrite) CLLocationCoordinate2D coordinate;@property(nonatomic,strong) NSString* titler;-(id)initWithTirle:(NSString *)titler andCoordinate:(CLLocationCoordinate2D)coordinate2d;@end
#import "MapAnnotation.h"@implementation MapAnnotation-(id)initWithTirle:(NSString *)titler andCoordinate:(CLLocationCoordinate2D)coordinate2d{ self.titler=titler; self.coordinate=coordinate2d; return self;}@end
#import <UIKit/UIKit.h>#import "RootTableViewController.h"@interface AppDelegate : UIResponder <UIApplicationDelegate>@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:UITableViewStylePlain]]; return YES;}
#import <UIKit/UIKit.h>#import <MapKit/MapKit.h>#import <CoreLocation/CoreLocation.h>@interface ViewController : UIViewController<MKMapViewDelegate>@property(strong,nonatomic)NSString *latitude;@property(strong,nonatomic)NSString *longitude;@end
#import "ViewController.h"#import "MapAnnotation.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //初始化你的地圖在手機上的大小 MKMapView *mapView=[[MKMapView alloc] initWithFrame:self.view.frame]; //遵循協議 mapView.delegate = self; //當前地圖以座標為中心點擴散 mapView.centerCoordinate=CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]); //地圖類型 mapView.mapType=MKMapTypeHybrid; //建立位置 CLLocationCoordinate2D location; //位置的經度緯度 location.latitude=[self.latitude doubleValue]; location.longitude=[self.longitude doubleValue]; //用圖釘來接收你所在的位置 MapAnnotation *newAnnotation=[[MapAnnotation alloc] initWithTirle:@"Apple Head quaters" andCoordinate:location]; //添加到你的地圖上 [mapView addAnnotation:newAnnotation]; //把地圖添加你的頁面上 [self.view addSubview:mapView]; self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"back" style:2 target:self action:@selector(backPage)]; }- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views{ MKAnnotationView *annotationView=[views objectAtIndex:0]; //代理屬性 調用方法 id<MKAnnotation>mp=[annotationView annotation]; //縮放你所看到的的X軸和Y軸 MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500); //mv 是否實現縮放 [mv setRegion:region animated:YES]; //mv 是否實現mp [mv selectAnnotation:mp animated:YES]; }-(void)backPage{ [self.navigationController popToRootViewControllerAnimated:YES];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
#import <UIKit/UIKit.h>#import "ViewController.h"@interface RootTableViewController : UITableViewController<NSXMLParserDelegate>@property(strong,nonatomic)NSMutableArray *arr;@property(strong,nonatomic)NSMutableDictionary *dic;@property(strong,nonatomic)NSString *str;@end
#import "RootTableViewController.h"@interface RootTableViewController ()@end@implementation RootTableViewController- (void)viewDidLoad { [super viewDidLoad]; self.title=@"城市列表"; 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"];}- (void)parserDidStartDocument:(NSXMLParser *)parser{ self.arr=[NSMutableArray array];}- (void)parserDidEndDocument:(NSXMLParser *)parser{ NSLog(@"%@",self.arr);}-(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.dic=[NSMutableDictionary dictionary]; [self.dic setDictionary:attributeDict]; }}- (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.dic setObject:self.str forKey:elementName]; } else if ([elementName isEqualToString:@"division"]) { [self.arr addObject:self.dic]; }}- (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]; cell.textLabel.text=self.arr[indexPath.row][@"name"]; return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"%@",self.arr[indexPath.row]); ViewController *viewvc=[[ViewController alloc] init]; viewvc.longitude=self.arr[indexPath.row][@"longitude"]; viewvc.latitude=self.arr[indexPath.row][@"latitude"]; [self.navigationController pushViewController:viewvc animated:YES]; }