博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
地图地位
阅读量:6595 次
发布时间:2019-06-24

本文共 3068 字,大约阅读时间需要 10 分钟。

//

//  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

转载于:https://www.cnblogs.com/liujiahua/p/5352305.html

你可能感兴趣的文章
Centos 6.5使用vsftpd配置FTP服务器教程
查看>>
hdu 5009 Paint Pearls
查看>>
个人工作总结03
查看>>
lesson8:AtomicInteger源码解析及性能分析
查看>>
经济学路谱
查看>>
Js构造对象-添加方法的三种方式
查看>>
手动修改magento域名
查看>>
Linux使用Shell脚本实现ftp的自动上传下载(转)
查看>>
[java] DOS编译 .java 文件得到 .class 文件 并执行 以及使用外部 .jar包 时的命令...
查看>>
UESTC 1703 Seven Dices
查看>>
java8:时间与日期
查看>>
==和equals的比较
查看>>
uva10480(最小割)
查看>>
hdu1520(树形dp)
查看>>
Android实现购物车功能
查看>>
url加密和解密
查看>>
bzoj 1150 贪心
查看>>
TensorFlow_CNN_MNIST问题
查看>>
使用process_monitor.sh监控hadoop进程的crontab配置
查看>>
set容器查找操作使用
查看>>