UITableViewController和XML解析还有地图的简单结合

时间:2024-01-07 20:51:56

UITableViewController和XML解析还有地图的简单结合

UITableViewController和XML解析还有地图的简单结合

然后我的代码就按照上面的这个顺序输出。

#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]; }