// Created by 妖精的尾巴 on 15-8-13.
// Copyright (c) 2015年 妖精的尾巴. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<MKMapViewDelegate>
/**
*地理常识:跨一个经度或者纬度,就代表横跨了大约110~111公里的距离
*
*地图控件(北京市石景山区古城西街-道路 领秀大厦B座 经度116.1860630000,纬度39.9241150000)
*/
@property(nonatomic,strong)MKMapView* mapView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setMap];
//[self setCenter];
//[self setArea];
[self GetUserLocation];
}
-(void)setMap
{
self.mapView =[[MKMapView alloc]init];
self.mapView.frame=self.view.bounds;
self.mapView.mapType= MKMapTypeStandard;
[self.view addSubview:self.mapView];
}
/**
*这个方法显示地图中心位置不太精确
*/
-(void)setCenter
{
CLLocationCoordinate2D coordinate=CLLocationCoordinate2DMake(39, 116);
[self.mapView setCenterCoordinate:coordinate animated:YES];
}
/**
*这个方法可以显示地图区域,并且可以通过MKCoordinateSpan指定经纬度的跨度是多大
*
*这个方法的弊端在于位置是自己定的,并不代表用户的实际位置。那么如何确定用户的位置了
*
*通过地图的self.mapView.userTrackingMode=MKUserTrackingModeFollow去获取,当然这得遵守代理MKMapViewDelegate
*/
-(void)setArea
{
CLLocationCoordinate2D coordinate=CLLocationCoordinate2DMake(39.9241150000, 116.1860630000);
MKCoordinateSpan span=MKCoordinateSpanMake(5, 5);
MKCoordinateRegion region=MKCoordinateRegionMake(coordinate, span);
[self.mapView setRegion:region animated:YES];
}
/**
*该方法可以获取用户当前所在位置,只不过得给模拟器一个位置
*/
-(void)GetUserLocation
{
self.mapView.userTrackingMode=MKUserTrackingModeFollow;
self.mapView.delegate =self;
}
#pragma mark-MKMapViewDelegate代理方法
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}
对-(void)GetUserLocation方法的截图说明
当我们运行程序回来到下图的位置: