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];
}
MapSearch 阅读随笔的更多相关文章
- 《UML大战需求分析》阅读随笔(一)
UML:Unified Modeling Language(统一建模语言) 作为我专业学科里的一门语言,其目的就是交流,同客户交流,同自己交流. 用图像和文字,详细地讲解将要做的工程的 需求和功能细节 ...
- 《重构网络-SDN架构与实现》阅读随笔
<重构网络-SDN架构与实现>: SDNLAB <重构网络-SDN架构与实现>新书有奖试读活动 资源下载 随笔 有幸拜读了李呈前辈和杨泽卫杨老师的作品<重构网络-SDN架 ...
- Google Java 风格 阅读随笔
官方文档:Google Java Style 中文翻译版:Google Java编程风格指南, Hawstein's Blog 可以先看官方文档,遇到不确定有疑问的,可以再对照翻译版本阅读,加深理解. ...
- sqlalchemy源代码阅读随笔(2)
这次阅读的,是Strategies.py文件. 文件自身,是这么描述的: """Strategies for creating new instances of Engi ...
- Java核心技术卷阅读随笔--第3章【Java 的基本程序设计结构】
Java 的基本程序设计结构 现在, 假定已经成功地安装了 JDK,并且能够运行第 2 章中给出的示例程序.我们从现在开始将介绍 Java 应用程序设计.本章主要介绍程序设计的基本概念(如数据类型.分 ...
- Java核心技术卷阅读随笔--第4章【对象与类】
对 象 与 类 4.1 面向对象程序设计概述 面向对象程序设计(简称 OOP) 是当今主流的程序设计范型, 它已经取代了 20 世纪 70 年代的" 结构化" 过程化程序设计开发技 ...
- Java核心技术卷阅读随笔--第2章【Java 程序设计环境】
Java 程序设计环境 本章主要介绍如何安装 Java 开发工具包( JDK ) 以及如何编译和运行不同类型的程序: 控制台程序. 图形化应用程序以及 applet.运行 JDK 工具的方法是在终端窗 ...
- 《UML大战需求分析》阅读随笔(六)
在我们做的代码设计中分为系统设计和程序设计.程序设计是系统设计中模拟程序的执行逻辑,定义客户机服务器对象合作的框架的那个部分.程序和事务设计中,作者讲述到程序和事务设计将系统设计制品放在一起,并作为系 ...
- 《UML大战需求分析》阅读随笔(五)
在处理复杂事物的时候,用到一种基本手段就是抽象.抽象的目的是区别事物之间的本质和不同,面向对象编程(OOP)的实质就是利用 类和对象来建立抽象模型. 类表示对象的类别,是创建对象的蓝本.建立一个事物的 ...
随机推荐
- Top 10 steps to optimize data access in SQL Server
2009年04月28日 Top 10 steps to optimize data access in SQL Server: Part I (use indexing) 2009年06月01日 To ...
- Codeforces Round #174 (Div. 1)A
题不怎么难,按线段树的解法 就是延迟标记,更新 因为找错找了N久 记一篇吧 向下更新时把+=写成了= 还做在了2W组的数据上 那个错找得真费劲.. #include <iostream> ...
- C++ new、delete
C++中向系统申请堆内存的方法为使用new.new[]操作符,new申请单个对象的内存,new[]申请对象数组的内存.对应的delete.delete[]操作符将new.new[]操作符申请到的内存还 ...
- Gradle学习系列(一)
今天就开始学习Gradle构建了,听说很牛X.本篇内容就带领我初步窥探Gradle的世界. 1.什么是Gradle 相信之前都接触过用Ant或者Meavn进行项目的构建,两者各有千 ...
- Bootstrap_组件
一.Glyphicons 字体图标 1.所有可用的图标查看:http://v3.bootcss.com/components/ 2.获取字体图标:我们已经在 环境安装 章节下载了 Bootstrap ...
- Qt之国际化
简介 Qt国际化属于Qt高级中的一部分,本想着放到后面来说,上节刚好介绍了Qt Linguist,趁热打铁就一起了解下. 对于绝大多数的应用程序,在刚启动时,需要加载默认的语言(或最后一次设置的语言) ...
- wxWidgets简单的多线程
#include <wx/wx.h> #include <wx/thread.h> #include <wx/event.h> #include <wx/pr ...
- Cython:基础教程(1) 语法
1 变量定义 http://docs.cython.org/src/reference/language_basics.html http://blog.csdn.net/i2cbus/article ...
- 查看mysql库大小,表大小,索引大小
查看所有库的大小 mysql> use information_schema; Database changed mysql> selectconcat(round(sum(DATA_LE ...
- 【转】Android 服务器之SFTP服务器上传下载功能
原文网址:http://blog.csdn.net/tanghua0809/article/details/47056327 本文主要是讲解Android服务器之SFTP服务器的上传下载功能,也是对之 ...