CoreLocation.framework框架基本用法
2016-03-08 12:42
281 查看
// // ViewController.m // Search // // Created by lcy on 16/1/14. // Copyright (c) 2016年 lcy. All rights reserved. // #import "ViewController.h" #import <MapKit/MapKit.h> #import "CYAnnotationView.h" @interface ViewController () <UISearchBarDelegate,MKMapViewDelegate> @property (nonatomic,strong) MKMapView *mapView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 375, 44)]; searchBar.delegate = self; searchBar.showsCancelButton = YES; self.navigationItem.titleView = searchBar; self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; self.mapView.delegate = self; [self.mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake(22.533367, 113.935404), MKCoordinateSpanMake(0.1, 0.1)) animated:YES]; [self.view addSubview:self.mapView]; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [self.mapView addGestureRecognizer:longPress]; } -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { //判断 如果是自己的大头针 才会自定义 否则 直接返回系统默认的大头针 if([annotation isKindOfClass:[MKPointAnnotation class]]) { //重用队列中 取大头针 MKAnnotationView *view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"cell"]; //如果不存在 创建大头针 if(view == nil) { view = [[CYAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"cell"]; } #if 0 //得到对应的类型的view 改变大头针的颜色 MKPinAnnotationView *pinView = (MKPinAnnotationView *)view; pinView.pinColor = MKPinAnnotationColorGreen; pinView.animatesDrop = YES; #endif return view; } return nil; } -(void)longPress:(UILongPressGestureRecognizer *)press { if(press.state == UIGestureRecognizerStateBegan) { CGPoint point = [press locationInView:self.mapView]; CLLocationCoordinate2D coor = [self.mapView convertPoint:point toCoordinateFromView:self.view]; MKPointAnnotation *ann = [[MKPointAnnotation alloc] init]; ann.title = @"新的大头针"; ann.coordinate = coor; [self.mapView addAnnotation:ann]; } } -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; } -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; MKLocalSearchRequest *req = [[MKLocalSearchRequest alloc] init]; //seaxx.text //搜索内容 req.naturalLanguageQuery = searchBar.text; //搜索范围 req.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(22.533367, 113.935404), MKCoordinateSpanMake(0.1, 0.1)); MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:req]; [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { //删除之前的大头针 [self.mapView removeAnnotations:self.mapView.annotations]; for (MKMapItem *item in response.mapItems) { NSLog(@"%@",item.name); NSLog(@"%@",item.phoneNumber); //大头针 MKPointAnnotation *pointAnn = [[MKPointAnnotation alloc] init]; pointAnn.title = item.name; pointAnn.subtitle = item.phoneNumber; //位置 pointAnn.coordinate = item.placemark.location.coordinate; [self.mapView addAnnotation:pointAnn]; } }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- hdoj 5638 Toposort 【线段树 维护 拓扑序】
- ZOJ-3923-In the Rain
- Linux查看JDK的安装路径和JAVA_HOME的环境变量设置
- “TableDetails”中列“IsPrimaryKey”的值为DBNull.
- 【android】ArrayAdapter 使用浅析
- static int 和 int 区别
- 实现Writable类型的ListWritable,SetWritable
- 怎么发布局局域网内使用,配置iis,进行测试
- Selenium中expected_conditions下text_to_be_present_in_element_value方法的使用
- 深入浅出剖析内容分发网络CDN(图自画)
- 在Ubuntu上使用apt-get安装MySQL+安全优化
- leetcode:Recover Binary Search Tree 【Java】
- (五) SpringMVC 导出Excel
- 关于Oracle字段类型Date使用mybatis generator自动生成工具出现的查询日期只精确到年月日问题
- 对象池的优劣
- hdoj 5636 Shortest Path 【最短路】
- 跨平台云笔记的安装
- 9. SpringMVC 数据验证例子2 - 自定义数据验证规则
- CSS常用的属性命名
- 好用的Windows命令积累