您的位置:首页 > 其它

CoreLocation.framework框架基本用法

2016-03-08 12:42 281 查看
//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: