地圖(系統&帶大頭針)

//

//  ViewController.m

//  XMMap

//

//  Created by Floating_SH on 15/12/29.

//  Copyright © 2015 SH. All rights reserved.

//


#import "ViewController.h"

#import <MapKit/MapKit.h>

#import <CoreLocation/CoreLocation.h>

#import "MyAnnotation.h"


@interface ViewController ()<MKMapViewDelegate>


@property (nonatomic, strong) MKMapView *mapView;


// 定位管理類.

@property (nonatomic, strong) CLLocationManager *manager;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    // 實例化定位管理對象

    self.manager = [[CLLocationManager alloc] init];

    

    // 判斷當前設備是否支持定位

    if ([CLLocationManager locationServicesEnabled] == YES) {

        NSLog(@"支持定位");

    }else{

        NSLog(@"不支持定位功能");

    }

    

    // 向系統和用戶申請使用權限.

    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {

        // 開始申請

        [self.manager requestWhenInUseAuthorization];

    }


    self.mapView = [[MKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    

    [self.view addSubview:self.mapView];

    

    // 地圖的樣式

    self.mapView.mapType = MKMapTypeStandard;

    

    // 設置地圖顯示的中心

    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(40, 116);

    [self.mapView setCenterCoordinate:coor];

    

    // 設置地圖顯示的範圍

    [self.mapView setRegion:MKCoordinateRegionMake(coor,MKCoordinateSpanMake(3, 3))];

    

    // 跟蹤用戶

    self.mapView.showsUserLocation = YES;

    self.mapView.userTrackingMode = MKUserTrackingModeFollow;

    

    // 是否支持地圖旋轉(默認是開的)

    self.mapView.rotateEnabled = NO;

    

    // 設置mapView代理

    self.mapView.delegate = self;

    

    // 點擊屏幕插一個大頭針

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

    

    [self.mapView addGestureRecognizer:tap];

    

}


- (void)tapAction:(UITapGestureRecognizer *)sender{

    

    // 1.獲取用戶點擊在view的點

    CGPoint point = [sender locationInView:self.view];

    // 2.將該點轉換爲mapView上的經緯度座標

    CLLocationCoordinate2D coor = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];

    

    // 創建一個大頭針對象

    MyAnnotation *ann = [[MyAnnotation alloc]init];

    ann.coordinate = coor;

    ann.title = @"拉登在此";

    ann.subtitle = @"導彈已定位!";

    

    // 3. 添加大頭針的方法

    [self.mapView addAnnotation:ann];

    

    // 添加多個大頭針

//    [self.mapView addAnnotations:array];

    

}


// 更新用戶位置

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

    

    NSLog(@"更新用戶位置成功!");

}


- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error{

    

    NSLog(@"更新用戶位置失敗");

}


// 屏幕顯示區域發生變化

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{

    

    NSLog(@"屏幕發生了移動");

}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

    

    NSLog(@"屏幕移動完畢");

}


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    

    MKPinAnnotationView *pinAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ann"];

    if (pinAnnotationView == nil) {

        pinAnnotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ann"];

    }

    

    // 設置左右視圖

    pinAnnotationView.leftCalloutAccessoryView = [UIButton buttonWithType:(UIButtonTypeContactAdd)];

    pinAnnotationView.rightCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Icon-Small"]];

    

    // 設置大頭針的出現效果, 從天而降,只能使用於系統樣式的大頭針

    pinAnnotationView.animatesDrop = YES;

    

    // 設置氣泡偏移量

    pinAnnotationView.calloutOffset = CGPointMake(0, 0);

    // 顯示氣泡

    pinAnnotationView.canShowCallout = YES;

    

    return pinAnnotationView;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



----------------------------------下方爲大頭針類----------------------------------

//

//  MyAnnotation.h

//  XMMap

//

//  Created by Floating_SH on 15/12/29.

//  Copyright © 2015 SH. All rights reserved.

//


#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject<MKAnnotation>


@property (nonatomic) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *subtitle;


@end



//

//  MyAnnotation.m

//  XMMap

//

//  Created by Floating_SH on 15/12/29.

//  Copyright © 2015 SH. All rights reserved.

//


#import "MyAnnotation.h"


@implementation MyAnnotation


@end






----------------------------------下方爲plist文件----------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSLocationWhenInUseUsageDescription</key>
<string>跪求開啓定位!</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>


附截圖:






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章