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