CoreLocation.framework框架基本用法

来源:互联网 发布:杭州淘宝代运营收费 编辑:程序博客网 时间:2024/04/29 15:43

////  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
原创粉丝点击