地图地位

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值