[IOS开发进阶与实战]第四天:MapKit的绘制

1.首先,由于《IOS开发进阶与实战》这本书真的是太晦涩难懂勒。三个不同的作者,有些变量定义的竟然都不一样。我决定匆匆结束这本书完事。这是我学习的有关手机地图的一章,后续的内容我会在以后补上。

好了,不再抱怨,开始我们新的知识的学习吧。先看看视图



1.先看看我们的.h文件,对我们视图的部分了解一下

//
//  MAPKitViewController.h
//  第十章:MApKit的使用
//
//  Created by lichan on 13-12-12.
//  Copyright (c) 2013年 lichan. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface MAPKitViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate,UIAlertViewDelegate>


- (IBAction)findMe:(id)sender;


@property (weak, nonatomic) IBOutlet UIProgressView *proessView;

@property (weak, nonatomic) IBOutlet UILabel *progressLabel;
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property (weak, nonatomic) IBOutlet UIButton *button;

@end

这三个属性的意思分别是:设置一个状态label,以便于显示信息。mapView ,显示全球地图,和 button,用于显示寻找我们的当前的位置

2.。m文件,注释中解释

//
//  MAPKitViewController.m
//  第十章:MApKit的使用
//
//  Created by lichan on 13-12-12.
//  Copyright (c) 2013年 lichan. All rights reserved.
//

#import "MAPKitViewController.h"

#import "MapLocation.h"

@interface MAPKitViewController ()
{
    CLLocationManager *manager;
    //The CLLocationManager object is your entry point to the location service.
    CLGeocoder *geocoder;
     geocoding handler, CLPlacemarks are provided in order of most confident to least confident
    //编码处理
    CLPlacemark *placemark;  //大头针
    //位置的储存model
    // Represents placemark data for a geographic location. Placemark data can be
    //    information such as the country, state, city, and street address.
    
}

- (void)openCallout:(id<MKAnnotation>)annotation;

- (void)reverseGeocode:(CLLocation *)location;


@end

@implementation MAPKitViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.mapView.mapType = MKMapTypeHybrid;
    
    //self.mapView.mapType =MKMapTypeSatellite;
    
    //self.mapView.mapType = MKMapTypeStandard;
    
    
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)findMe:(id)sender {//确定用户的当前位置
    
    if (manager == nil) {
        manager = [[CLLocationManager alloc]init];
    }
    
    manager.delegate = self;
    manager.desiredAccuracy = kCLLocationAccuracyBest;
    [manager startUpdatingLocation];//开始更新位置
    self.proessView.hidden = NO;
    
    self.proessView.progress = 0.0;
    
    self.progressLabel.text = NSLocalizedString(@"Determanint Current Location", @"Determanint Current Location");
    
    self.button.hidden = YES;
    
}

- (void)openCallout:(id<MKAnnotation>)annotation
{
    self.proessView.progress = 1.0;
    self.progressLabel.text = NSLocalizedString(@"Show Annotation", @"Show Annotation");
    
    [self.mapView selectAnnotation:annotation animated:YES];

}

- (void)reverseGeocode:(CLLocation *)location//反向地理位置编码,使之让suer能看懂
{
    if (!geocoder) {
        geocoder = [[CLGeocoder alloc]init];
        //对当前的位置进行解析,得到一个placemarks数组。最后得到第一个index
        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks,NSError *error){
            if (nil != error) {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Error translating coordanates into location", @"Error translating coordanates into location") message:NSLocalizedString(@"Feocoder did not recognize coordinates", @"Feocoder did not recognize coordinates") delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", @"OK") otherButtonTitles:nil];
                [alert show];
            }
            else if([placemarks count] > 0)
            {
                placemark = [placemarks objectAtIndex:0];
                
                self.proessView.progress = 0.5;
                
                self.progressLabel.text = NSLocalizedString(@"Location Determined", @"Location Determined");
                
                MapLocation *annotation = [[MapLocation alloc]init];
                //进行反向解析
                annotation.street = placemark.thoroughfare;
                annotation.city = placemark.locality;
                annotation.state = placemark.administrativeArea;
                annotation.zip = placemark.postalCode;
                
                annotation.coordinate = location.coordinate;
                
                [self.mapView addAnnotation:annotation];//在当前的mapview中绘制
            
            }

        }];
        
        
    }
    


}

#pragma mark CLLocationManagerDelegate METHODS
//设置绘制的位置
- (void)locationManager:(CLLocationManager *)amanager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if ([newLocation.timestamp timeIntervalSince1970] < [NSDate timeIntervalSinceReferenceDate] - 60) {
        return;
    }
    
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
    
    MKCoordinateRegion adjuestedRegion = [self.mapView regionThatFits:viewRegion];
    
    [self.mapView setRegion:adjuestedRegion animated:YES];
    
    amanager.delegate = nil;
    
    [amanager stopUpdatingLocation];
    
    self.proessView.progress = 0.25;
    
    self.progressLabel.text = NSLocalizedString(@"Reverse Grocoding Location", @"Reverse Grocoding Location");
    
    [self reverseGeocode:newLocation];
    

}

//错误处理
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSString *errorType = (error.code == kCLErrorDenied)
    ? NSLocalizedString(@"Access Denied", @"Access Denied")
    : NSLocalizedString(@"Unknown Error", @"Unknown Error");
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"ERROR getting Location", @"ERROR getting Location") message:errorType delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"OK") otherButtonTitles:nil];
    
    [alert show];

}


#pragma mark MKMapViewDelegate methods
//接收到 annotaiton之后 的处理~~~~
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *placemarkIdentifiter = @"MAP Location Identifier";
    
    if ([annotation isKindOfClass:[MapLocation class]]) {
        MKPinAnnotationView *annotitionView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifiter];
        
        
        if (nil == annotitionView) {
            annotitionView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:placemarkIdentifiter];
        }
        else
            annotitionView.annotation =annotation;
        
        annotitionView.enabled = YES;
        
        annotitionView.animatesDrop = YES;
        
        annotitionView.pinColor = MKPinAnnotationColorPurple;
        
        annotitionView.canShowCallout = YES;
        
        [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:0.5];
        
        self.proessView.progress = 0.75;
        
        self.progressLabel.text = NSLocalizedString(@"Create Annotation", @"Create Annotation");
        
        return annotitionView;
   
    }
    
    return nil;

}

- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Error loading map", @"Error loading map") message:[error localizedDescription] delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"OK") otherButtonTitles:nil];
    
    [alert show];

}


#pragma marl UialertView protocol methods

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    self.proessView.hidden = YES;
    self.progressLabel.text = @" ";
    self.button.hidden = NO;

}










@end



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值