IOS基础学习之添加地图定位方法
0
IOS开发中,使用地图只需要新建一个MKMapView,addSubView即可。如下:
1、添加地图
1.1 新一个Single View app ,选择默认项,创建后,在ViewController.h
复制代码
1.2 在ViewController.m中添加
复制代码
运行即可
2、定位到指定经纬度
复制代码
点此查看更多IOS学习视频教程。
1、添加地图
1.1 新一个Single View app ,选择默认项,创建后,在ViewController.h
-
#import <UIKit/UIKit.h>
-
#import <MapKit/MapKit.h>
-
#import <CoreLocation/CoreLocation.h>
-
-
@interface ViewController : UIViewController
-
<MKMapViewDelegate, CLLocationManagerDelegate> {
-
MKMapView *map;
-
CLLocationManager *locationManager;
-
}
- @end
1.2 在ViewController.m中添加
-
- (void)viewDidLoad
-
{
-
map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
-
map.showsUserLocation = YES;
-
map.mapType = MKMapTypeSatellite;
-
[self.view addSubview:map];
-
-
-
[super viewDidLoad];
-
// Do any additional setup after loading the view, typically from a nib.
- }
运行即可
2、定位到指定经纬度
-
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);
-
-
float zoomLevel = 0.02;
-
MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
-
[map setRegion:[map regionThatFits:region] animated:YES];
点此查看更多IOS学习视频教程。