#import "ViewController.h"
#import@interface
ViewController ()
{
MKMapView *mkmapview;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化地图
mkmapview = [[MKMapView alloc] init];
// 设置位置
[mkmapview setFrame:self.view.bounds];
// 设置地图类型为标准类型
mkmapview.mapType = MKMapTypeStandard;
// 设置缩放
mkmapview.zoomEnabled = YES;
// 设置旋转
mkmapview.rotateEnabled = YES;
// 设置用户显示位置
mkmapview.showsUserLocation = YES;
// 设置代理
mkmapview.delegate = self;
// 设置地图的经纬度
CLLocationCoordinate2D center = {40.0412330000,116.3004370000};
// 设置地图显示范围为
MKCoordinateSpan span;
// 地图显示范围越小,细节越清楚
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
// 创建mkcoor对象,该对象代表了地图的显示中心和显示范围
MKCoordinateRegion refion = {center,span};
// 设置当前地图的显示中心和显示范围
[mkmapview setRegion:refion animated:YES];
[self.view addSubview:mkmapview];
UISearchBar *searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 40)];
searchbar.prompt = @"搜索";
searchbar.searchBarStyle = UISearchBarStyleMinimal;
searchbar.translucent = YES;
searchbar.delegate = self;
[mkmapview addSubview:searchbar];
}
//点击搜索按钮调用
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
// 获取搜索内容
NSString *str = searchBar.text;
// 将文字由逗号隔开
NSArray *array = [str componentsSeparatedByString:@","];
if ([array count] == 2) {
// 设置地图的经纬度
CLLocationDegrees latitude = [array[0]floatValue];
CLLocationDegrees longitude = [array[1]floatValue];
if (latitude >= 90 || latitude <= -90) {
NSLog(@"纬度超标");
return;
}
if (longitude >= 180 || longitude <= -180) {
NSLog(@"经度超标");
return;
}
CLLocationCoordinate2D center = {latitude,longitude};
// 设置地图显示范围为
MKCoordinateSpan span;
// 地图显示范围越小,细节越清楚
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
// 创建mkcoor对象,该对象代表了地图的显示中心和显示范围
MKCoordinateRegion refion = {center,span};
// 设置当前地图的显示中心和显示范围
[mkmapview setRegion:refion animated:YES];
}
}
//显示区域改变完成时
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"显示区域改变完成时");
}
//加载数据完成时激发该方法
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView{
NSLog(@"加载数据完成时激发该方法");
}
//渲染地图完成时技法该方法
- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered NS_AVAILABLE(10_9, 7_0){
NSLog(@"渲染地图完成时技法该方法");
}
@end