在第二部的基础上,我们给视图添加给MKMapview,具体代码如下:
//
// ViewController.m
// LBS_002_mapview
//
// Created by liqun on 13-7-25.
// Copyright (c) 2013年 Block Cheng. All rights reserved.
//
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "CustomAnnotation.h"
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic,retain)MKMapView* mapView;
@property (nonatomic,retain)CLLocationManager* locationManager;
@property (nonatomic,retain)CLLocation* location;
@property (nonatomic, retain) CLGeocoder *myGeocoder;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MKMapView* map = [[MKMapView alloc] initWithFrame:self.view.frame];
map.mapType = MKMapTypeStandard;
map.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:map];
[map release];
self.mapView = map;
UIButton *addBt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addBt.frame = CGRectMake(0, 00, 320, 50);
[addBt setTitle:@"locationMe" forState:UIControlStateNormal];
[ addBt addTarget:self action:@selector(handleLocationMe:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: addBt];
if ([CLLocationManager locationServicesEnabled]){
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.purpose =
@"To provide functionality based on user's current location.";
[self.locationManager startUpdatingLocation];
} else {
/* Location services are not enabled.
Take appropriate action: for instance, prompt the user to enable location services */
NSLog(@"Location services are not enabled");
}
self.myGeocoder = [[CLGeocoder alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)handleLocationMe:(id)sender
{
[self.myGeocoder
reverseGeocodeLocation:self.location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil &&[placemarks count] > 0){
CLPlacemark *placemark = [placemarks objectAtIndex:0];
/* We received the results */
NSLog(@"Country = %@", placemark.country);
NSLog(@"Postal Code = %@", placemark.postalCode);
NSLog(@"Locality = %@", placemark.locality);
NSLog(@"dic = %@", placemark.addressDictionary );
NSLog(@"dic FormattedAddressLines= %@", [placemark.addressDictionary objectForKey:@"FormattedAddressLines"]);
NSLog(@"dic Name = %@", [placemark.addressDictionary objectForKey:@"Name"]);
NSLog(@"dic State = %@", [placemark.addressDictionary objectForKey:@"State"]);
NSLog(@"dic Street = %@", [placemark.addressDictionary objectForKey:@"Street"]);
NSLog(@"dic SubLocality= %@", [placemark.addressDictionary objectForKey:@"SubLocality"]);
NSLog(@"dic SubThoroughfare= %@", [placemark.addressDictionary objectForKey:@"SubThoroughfare"]);
NSLog(@"dic Thoroughfare = %@", [placemark.addressDictionary objectForKey:@"Thoroughfare"]);
}
else if (error == nil &&
[placemarks count] == 0){
NSLog(@"No results were returned.");
}
else if (error != nil){
NSLog(@"An error occurred = %@", error);
}
}];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
/* We received the new location */
NSLog(@"Latitude = %f", newLocation.coordinate.latitude);
NSLog(@"Longitude = %f", newLocation.coordinate.longitude);
self.location = newLocation;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
/* Failed to receive user's location */
}
- (void)dealloc
{
self.mapView = nil;
self.location = nil;
self.locationManager = nil;
self.myGeocoder = nil;
[super dealloc];
}
@end
运行后,结果如下: