MapSearch 阅读随笔

MapSearch

https://developer.apple.com/library/ios/samplecode/MapSearch/Introduction/Intro.html#//apple_ref/doc/uid/DTS40013332

//1.#import <MapKit/MapKit.h> //引入头文件
#import <CoreLocation/CoreLocation.h>
//遵循协议
<CLLocationManagerDelegate, UISearchBarDelegate>

//2. 声明 属性
@interface MyTableViewController ()
@property (nonatomic, assign) MKCoordinateRegion boundingRegion;
@property (nonatomic, strong) MKLocalSearch *localSearch;
@property (nonatomic, weak) IBOutlet UIBarButtonItem *viewAllButton;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic) CLLocationCoordinate2D userCoordinate;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@end

//3. 初始化 locationManager
- (void)viewDidLoad {
[super viewDidLoad];

self.locationManager = [[CLLocationManager alloc] init];
}

//4. UITableView delegate methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.places count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];

MKMapItem *mapItem = [self.places objectAtIndex:indexPath.row];
cell.textLabel.text = mapItem.name;

return cell;
}

//5.点击搜索按钮后执行
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
// Check if location services are available
if ([CLLocationManager locationServicesEnabled] == NO) {
NSLog(@"%s: location services are not available.", __PRETTY_FUNCTION__);

// Display alert to the user.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Location services"
message:@"Location services are not enabled on this device. Please enable location services in settings."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}]; // Do nothing action to dismiss the alert.

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

return;
}

// Request "when in use" location service authorization.
// If authorization has been denied previously, we can display an alert if the user has denied location services previously.
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
[self.locationManager requestWhenInUseAuthorization];
} else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
NSLog(@"%s: location services authorization was previously denied by the user.", __PRETTY_FUNCTION__);

// Display alert to the user.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Location services"
message:@"Location services were previously denied by the user. Please enable location services for this app in settings."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}]; // Do nothing action to dismiss the alert.

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

return;
}

// Start updating locations.
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}

//6. 获取定位信息后根据位置信息搜索周围地点
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

// Remember for later the user‘s current location.
CLLocation *userLocation = locations.lastObject;
self.userCoordinate = userLocation.coordinate;

[manager stopUpdatingLocation]; // We only want one update.

manager.delegate = nil; // We might be called again here, even though we
// called "stopUpdatingLocation", so remove us as the delegate to be sure.

// We have a location now, so start the search.
[self startSearch:self.searchBar.text];
}

- (void)startSearch:(NSString *)searchString {
if (self.localSearch.searching)
{
[self.localSearch cancel];
}

// Confine the map search area to the user‘s current location.
MKCoordinateRegion newRegion;
newRegion.center.latitude = self.userCoordinate.latitude;
newRegion.center.longitude = self.userCoordinate.longitude;

// Setup the area spanned by the map region:
// We use the delta values to indicate the desired zoom level of the map,
// (smaller delta values corresponding to a higher zoom level).
// The numbers used here correspond to a roughly 8 mi
// diameter area.
//
newRegion.span.latitudeDelta = 0.112872;
newRegion.span.longitudeDelta = 0.109863;

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];

request.naturalLanguageQuery = searchString;
request.region = newRegion;

MKLocalSearchCompletionHandler completionHandler = ^(MKLocalSearchResponse *response, NSError *error) {
if (error != nil) {
NSString *errorStr = [[error userInfo] valueForKey:NSLocalizedDescriptionKey];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Could not find places"
message:errorStr
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
} else {
self.places = [response mapItems];

// Used for later when setting the map‘s region in "prepareForSegue".
self.boundingRegion = response.boundingRegion;

self.viewAllButton.enabled = self.places != nil ? YES : NO;

[self.tableView reloadData];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
};

if (self.localSearch != nil) {
self.localSearch = nil;
}
self.localSearch = [[MKLocalSearch alloc] initWithRequest:request];

[self.localSearch startWithCompletionHandler:completionHandler];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

//7.点击对应的cell 或者点击showAll 按钮 跳转到下一界面
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
MapViewController *mapViewController = segue.destinationViewController;

if ([segue.identifier isEqualToString:@"showDetail"]) {
// Get the single item.
NSIndexPath *selectedItemPath = [self.tableView indexPathForSelectedRow];
MKMapItem *mapItem = self.places[selectedItemPath.row];

// Pass the new bounding region to the map destination view controller.
MKCoordinateRegion region = self.boundingRegion;
// And center it on the single placemark.
region.center = mapItem.placemark.coordinate;
mapViewController.boundingRegion = region;

// Pass the individual place to our map destination view controller.
mapViewController.mapItemList = [NSArray arrayWithObject:mapItem];

} else if ([segue.identifier isEqualToString:@"showAll"]) {

// Pass the new bounding region to the map destination view controller.
mapViewController.boundingRegion = self.boundingRegion;

// Pass the list of places found to our map destination view controller.
mapViewController.mapItemList = self.places;
}
}

//8. Adjust the map to zoom/center to the annotations we want to show.
[self.mapView setRegion:self.boundingRegion animated:YES];

//9. We add the placemarks here to get the "drop" animation.
if (self.mapItemList.count == 1) {
MKMapItem *mapItem = [self.mapItemList objectAtIndex:0];

self.title = mapItem.name;

// Add the single annotation to our map.
PlaceAnnotation *annotation = [[PlaceAnnotation alloc] init];
annotation.coordinate = mapItem.placemark.location.coordinate;
annotation.title = mapItem.name;
annotation.url = mapItem.url;
[self.mapView addAnnotation:annotation];

// We have only one annotation, select it‘s callout.
[self.mapView selectAnnotation:[self.mapView.annotations objectAtIndex:0] animated:YES];
} else {
self.title = @"All Places";

// Add all the found annotations to the map.

for (MKMapItem *item in self.mapItemList) {
PlaceAnnotation *annotation = [[PlaceAnnotation alloc] init];
annotation.coordinate = item.placemark.location.coordinate;
annotation.title = item.name;
annotation.url = item.url;
[self.mapView addAnnotation:annotation];
}
}

//10.
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error {
NSLog(@"Failed to load the map: %@", error);
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *annotationView = nil;

if ([annotation isKindOfClass:[PlaceAnnotation class]]) {
annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"Pin"];

if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
}
}
return annotationView;
}

//11. 界面将要消失时 removeAnnotations
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.mapView removeAnnotations:self.mapView.annotations];
}

时间: 2024-08-01 10:43:46

MapSearch 阅读随笔的相关文章

Spring 中bean的scop 阅读随笔(记)

bean的scope scope用来声明容器中的对象所应该处的限定场景或者说该对象的存活时间,即容器在对象进入其相应的scope之前,生成并装配这些对象,在该对象不再处于这些scope的限定之后,容器通常会销毁这些对象. Spring容器最初提供了两种bean的scope类型:singleton和prototype,但发布2.0之后,又引入了另外三种scope类型,即request.session和global session类型.不过这三种类型有所限制,只能在Web应用中使用.也就是说,只有在

价值投资实战手册(唐朝) - 阅读随笔(一)

价值投资实战手册(唐朝) - 第一章 - 阅读随笔(一) 1.什么投资? 2.股票的本质 3.投资是持续中分的事 4.股票收益的来源 5.最简单的投资方法 6.普通投资者的道路 7.投资无需接盘侠 8.股权和其他投资品的对比 9.优秀企业的特征 原文地址:https://www.cnblogs.com/edwardsun/p/12232000.html

Google Java 风格 阅读随笔

官方文档:Google Java Style 中文翻译版:Google Java编程风格指南, Hawstein's Blog 可以先看官方文档,遇到不确定有疑问的,可以再对照翻译版本阅读,加深理解. 5.2.1 包名称 包名称全部是小写字母,简单地将连续单词连接在以前(没有下划线). 例如:用 com.example.deepspace, 而不是 com.example.deepSpace 或 com.example.deep_space. 5.3 驼峰式命名法 以短语形式开头的名称: 把短语

YII2框架阅读随笔

今天阅读的是vendor/yiisoft/yii2/web/User.php <?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace yii\web; //命名空间 use Yii; use yii\base\Component; use y

构建之法阅读随笔一

<构建之法>一书已完成了第一遍的阅读,接下来,我将随机抽取其中的一段进行精读. 移山公司的项目进行了一段时间,TFS上也积累了不少数据.大栓做了"数据挖掘",整理出来一些统计信息,向各位领导汇报. 大牛:哇!前端组的这位开发人员是冠军呀,他导致的Bug总量约等于所有其他成员的总和. 二柱:那是有客观原因的,你可能不知道他的工作量是最大的. 大牛:那也不能因此产生这么多Bug,而让整个团队的进度停下来吧. 二柱:别人工作得这么辛苦,我倒是不太忍心再批评他.阿超,其实我觉得我们

《UML大战需求分析》阅读随笔(二)

在需求方面,我自己大体认为,分为两个部分:客户和软件公司. 客户:提出需求. 软件公司:解决需求. 这就是我所认为的 需求的关系. 就像书中所说的: 软件公司(项目组)始终都是跟着客户的后面追,客户需求什么,软件公司就去解决什么. 随着时间的发展,客户对需求的理解越来越强,尽管项目组对需求的理解同样也变强,但项目组对需求的认识总是落后于客户,这样的需求分析工作肯定陷于被动,总会被客户“牵着鼻子走”,很容易出现互相责怪的局面:客户责怪项目组水平太差,而项目组责怪客户需求变来变去 于是我有了新的理解

Thrift0.9.3 C++版代码阅读随笔——TNonblockingServer

0.一些参考资料 (参考资料1)对thrift的一个基本介绍可以参考: http://wenku.baidu.com/link?url=LLL5H3qL4hJ3o6dfq0SBgztqtxYFR5vDyftwowKNRMWiIQ3t87mCu-GMZljxcZVryxxhqna1hM4eu3F7AyCMlC7fFy7yWl18IIl6nY7JKca (参考资料2)thrift IDL定义可参考(就是定义结构化数据和服务的方法): http://diwakergupta.github.io/thr

《UML大战需求分析》阅读随笔(三)

一: 需求中提到的各种业务概念.人物等,经过抽象后都可以视之为类. 平时遇到的人.物,我们将遇到的都是具体的人.物,也就如程序中的对象,是一个实实在在的东西. 当我们分析需求的时候,设计模型的时候,我们就需要将之抽象,也就是最关键的一步--提炼. 提炼出这个东西,我们所需要的部分. 比如,在教室,主要存在两类人,学生和教师. 当我们做的项目为:人员管理系统 在如上的环境中,我们需要的是学生和教师的姓名和年龄.学号(工号)等这些利于了解基本信息的数据.那么,我们进行模型设计的时候,类图就应该主要包

CSS那些事儿-阅读随笔2(选择符的组合与权重)

在知道了CSS选择符最基础的知识后,就要综合利用它们了.这里就记录几种常见的用法. 1.针对性的使用类选择符或者ID选择符 类选择符在一个页面中可能会在不同的地方应用,那么就需要有针对性地使用类选择符.如下例: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>css-test</title&