城市地图
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
// 只有当应用在前台时,该方法才会被调用
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification*)notification{
// 如果应用程序在前台,将应用程序图标上红色徽标中数字设为0
application.applicationIconBadgeNumber = 0;
// 使用UIAlertView显示本地通知的信息
[[[UIAlertView alloc] initWithTitle:@"收到通知"message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// 应用程序再次进入前台时,将应用程序徽标设置0
application.applicationIconBadgeNumber = 0;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *vc = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
return YES;
}
#import "ViewController.h"
#import "TwoViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView *table;
NSDictionary *dic;
NSString *province;
NSString *city;
//通知
UIApplication *app;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化app
app = [UIApplication sharedApplication];
//标题
self.navigationItem.title = @"城市列表";
//初始化表格
table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
//初始化字典
dic = @{@"河北省":@[@"石家庄",@"邯郸",@"燕郊"],@"陕西省":@[@"西安",@"渭南",@"咸阳"],@"山西省":@[@"运城",@"太原",@"临汾"]};
UIBarButtonItem *tongzhi = [[UIBarButtonItem alloc]initWithTitle:@"通知" style:UIBarButtonItemStylePlain target:self action:@selector(tongzhis)];
self.navigationItem.leftBarButtonItem = tongzhi;
UIBarButtonItem *quxiao = [[UIBarButtonItem alloc]initWithTitle:@"取消通知" style:UIBarButtonItemStylePlain target:self action:@selector(quxiaos)];
self.navigationItem.rightBarButtonItem = quxiao;
}
//通知
-(void)tongzhis
{
if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
// 创建一个本地通知
UILocalNotification *notification = [[UILocalNotification alloc]init];
// 设置通知的触发时间
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];;
// 设置通知的时区
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置通知的重复发送的事件间隔
notification.repeatInterval = kCFCalendarUnitHour;
// 设置通知的声音
notification.soundName = @"gu.mp3";
// 设置当设备处于锁屏状态时,显示通知的警告框下方的title
notification.alertAction = @"打开";
// 设置通知是否可显示Action
notification.hasAction = YES;
// 设置通过通知加载应用时显示的图片
notification.alertLaunchImage = @"logo.png";
// 设置通知内容
notification.alertBody = @"亲,好几天不登陆,Tom猫想念你了!";
// 设置显示在应用程序上红色徽标中的数字
notification.applicationIconBadgeNumber = 1;
// 设置userinfo,用于携带额外的附加信息。
NSDictionary *info = @{@"fkjava.org": @"key"};
notification.userInfo = info;
// 调度通知
[app scheduleLocalNotification:notification]; // ①
}
//取消通知
-(void)quxiaos
{
// 获取所有处于调度中本地通知数组
NSArray *localArray = [app scheduledLocalNotifications];
if (localArray)
{
for (UILocalNotification *noti in localArray)
{
NSDictionary *dict = noti.userInfo;
if (dict)
{
// 如果找到要取消的通知
NSString *inKey = [dict objectForKey:@"key"];
if ([inKey isEqualToString:@"fkjava.org"])
{
// 取消调度该通知
[app cancelLocalNotification:noti]; // ②
}
}
}
}
}
//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = [dic.allKeys objectAtIndex:section];
return [[dic objectForKey:key]count];
}
//设置内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString*str = @"sdf";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: str];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
NSString *key = [dic.allKeys objectAtIndex:indexPath.section];
cell.textLabel.text = [[dic objectForKey:key]objectAtIndex:indexPath.row];
return cell;
}
//设置分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return dic.count;
}
//设置分区标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [dic.allKeys objectAtIndex:section];
}
//跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TwoViewController *second = [[TwoViewController alloc]init];
NSString *key = [dic.allKeys objectAtIndex:indexPath.section];
second.city = [[dic objectForKey:key]objectAtIndex:indexPath.row];
[self.navigationController pushViewController:second animated:YES];
}
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface TwoViewController : UIViewController
@property (nonatomic,strong)NSString *city;
@end
#import "TwoViewController.h"
#import <MapKit/MapKit.h>
@interface TwoViewController()<MKMapViewDelegate>
@property (nonatomic,strong)MKMapView *mapView;//地图视图
@end
@implementation TwoViewController
-(MKMapView *)mapView
{
if (!_mapView) {
_mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
_mapView.delegate = self;
_mapView.zoomEnabled = YES;
_mapView.scrollEnabled = YES;
_mapView.pitchEnabled = YES;
_mapView.rotateEnabled = YES;
_mapView.mapType = MKMapTypeStandard;//综合地图
_mapView.showsUserLocation = YES;
}
return _mapView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.mapView];
UIBarButtonItem *bar1 = [[UIBarButtonItem alloc] initWithTitle:@"省会位置" style:UIBarButtonItemStylePlain target:self action:@selector(locateProvince)];
UIBarButtonItem *bar2 = [[UIBarButtonItem alloc] initWithTitle:@"当前位置" style:UIBarButtonItemStylePlain target:self action:@selector(locateCurrentLocation:)];
self.navigationItem.rightBarButtonItems = @[bar1,bar2];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationItem.title = self.city;
}
-(void)getmap:(NSString *)name{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:name completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *place = [placemarks lastObject];
CLLocation *loc = place.location;
dispatch_async(dispatch_get_main_queue(), ^{
[self.mapView setRegion:MKCoordinateRegionMake(loc.coordinate, MKCoordinateSpanMake(0.1, 0.1)) animated:YES];
MKCircle *circle = [MKCircle circleWithCenterCoordinate:loc.coordinate radius:100];
//添加图像覆盖层
[self.mapView addOverlay:circle];
});
}];
}
-(MKOverlayPathRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(nonnull id<MKOverlay>)overlay
{
MKCircle *circle = (MKCircle *)overlay;
MKCircleRenderer *circleView = [[MKCircleRenderer alloc] initWithCircle:circle];
circleView.fillColor = [UIColor cyanColor];
circleView.strokeColor = [UIColor redColor];
circleView.alpha = 0.3;
return circleView;
}
-(void)locateProvince
{
[self getmap:self.city];
}
-(void)locateCurrentLocation:(id)sender
{
[self getmap:@"北京市海淀区上地七街软件园南站"];
}
@end