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學習視頻教程。