//
// ViewController.m
//
//
// Created by on 16/4/2.
// Copyright © 2016年 . All rights reserved.
//
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate,UISearchBarDelegate>
{
UISearchBar * searchBar1 ;
MKMapView * mapview;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//地图初始化
mapview = [[MKMapView alloc]initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height)];
//设置地图类型
mapview.mapType = MKMapTypeStandard ;
//设置代理
mapview.delegate = self;
//设置地图 显示用户位置 可以滚动 可以粘合 可以缩放 不可旋转
mapview.showsUserLocation = YES;
mapview.rotateEnabled = NO;
mapview.scrollEnabled = YES;
mapview.zoomEnabled = YES;
mapview.pitchEnabled = YES;
//添加子视图
[self.view addSubview:mapview];
//初始化搜索条
searchBar1 = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 60)];
searchBar1.delegate = self;
// searchBar1.text = @"北京市动物园";
searchBar1.placeholder = @"请输入地址";
// searchBar1.showsBookmarkButton = YES;
// searchBar1.showsCancelButton = YES;
[self.view addSubview:searchBar1];
}
//用户在搜索框中输入内容的时候 改变按钮的名字 变成“搜索”
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
searchBar1.showsCancelButton = YES;
for(id cc in [searchBar1.subviews[0] subviews])
{
if ([cc isKindOfClass:[UIButton class]]) {
UIButton * btn = (UIButton *)cc;
[btn setTitle:@"搜索" forState:UIControlStateNormal];
}
}
}
//
// 当用户单击“取消”按钮时激发该方法
// 由于我们重定义了该控件的外观——将取消按钮的文本改成了“搜索”,因此单击取消按钮也执行搜索
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar1 resignFirstResponder];
NSString * str = searchBar1.text;
[self GotoLocation:str];
}
// 当用户单击虚拟键盘上的“搜索”按钮时激发该方法
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar1 resignFirstResponder];
NSString * str = searchBar1.text;
if (str.length > 0) {
[self GotoLocation:str];
}else
{
NSLog(@"地址错误");
}
}
//自己定义的方法 用来跳转至相应地方
-(void)GotoLocation:(NSString *)address
{
CLGeocoder * geocoder = [[CLGeocoder alloc]init];
[geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count >0 && error == nil) {
CLPlacemark * placemark = [placemarks lastObject];
CLLocation * location = placemark.location;
dispatch_async(dispatch_get_main_queue(), ^{
//跳至指定位置
[mapview setRegion:MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.01, 0.01))];
//锚点
MKPointAnnotation * point = [[MKPointAnnotation alloc]init];
point.coordinate = location.coordinate;
point.title = placemark.name;
point.subtitle = [NSString stringWithFormat:@"%@-%@-%@-%@",placemark.country,placemark.administrativeArea,placemark.locality,placemark.subLocality];
[mapview addAnnotation:point];
[mapview selectAnnotation:point animated:YES];
});
}else{
NSLog(@"没有拼配的数据");
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end