CoreLocation.framework框架基本用法

//
//  ViewController.m
//  Search
//
//  Created by lcy on 16/1/14.
//  Copyright (c) 2016年 lcy. All rights reserved.
//

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "CYAnnotationView.h"

@interface ViewController () <UISearchBarDelegate,MKMapViewDelegate>

@property (nonatomic,strong) MKMapView *mapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];
    
    searchBar.delegate = self;
    
    searchBar.showsCancelButton = YES;
    self.navigationItem.titleView = searchBar;
    self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    
    self.mapView.delegate = self;
    [self.mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake(22.533367, 113.935404), MKCoordinateSpanMake(0.1, 0.1)) animated:YES];
    [self.view addSubview:self.mapView];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    
    [self.mapView addGestureRecognizer:longPress];
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    //判断  如果是自己的大头针   才会自定义  否则 直接返回系统默认的大头针
    if([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        //重用队列中 取大头针
        MKAnnotationView *view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"cell"];
        //如果不存在 创建大头针
        if(view == nil)
        {
            view = [[CYAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"cell"];
        }
        
        
#if 0
        //得到对应的类型的view 改变大头针的颜色
        MKPinAnnotationView *pinView = (MKPinAnnotationView *)view;
        pinView.pinColor = MKPinAnnotationColorGreen;
        pinView.animatesDrop = YES;

#endif

        return view;
    }
    return nil;
}


-(void)longPress:(UILongPressGestureRecognizer *)press
{
    
    if(press.state == UIGestureRecognizerStateBegan)
    {
        CGPoint point = [press locationInView:self.mapView];
        CLLocationCoordinate2D coor = [self.mapView convertPoint:point toCoordinateFromView:self.view];
        MKPointAnnotation *ann = [[MKPointAnnotation alloc] init];
        
        ann.title = @"新的大头针";
        
        ann.coordinate = coor;
        
        [self.mapView addAnnotation:ann];
        
    }
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    
    MKLocalSearchRequest *req = [[MKLocalSearchRequest alloc] init];
    //seaxx.text
    //搜索内容
    req.naturalLanguageQuery = searchBar.text;
    //搜索范围
    req.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(22.533367, 113.935404), MKCoordinateSpanMake(0.1, 0.1));
    MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:req];
    
    [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
        //删除之前的大头针
        [self.mapView removeAnnotations:self.mapView.annotations];
        
        for (MKMapItem *item in response.mapItems) {
            NSLog(@"%@",item.name);
            NSLog(@"%@",item.phoneNumber);
            
            //大头针
            MKPointAnnotation *pointAnn = [[MKPointAnnotation alloc] init];
            pointAnn.title = item.name;
            pointAnn.subtitle = item.phoneNumber;
            //位置
            pointAnn.coordinate = item.placemark.location.coordinate;
            [self.mapView addAnnotation:pointAnn];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值