- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if ([CLLocationManager locationServicesEnabled]) {
_mapView.delegate = self;
_mapView.mapType = MKMapTypeStandard;
_mapView.showsUserLocation = YES;
[_mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
}
- (IBAction)geocodeQuery:(id)sender {
if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) {
return;
}
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"查询记录数:%i",[placemarks count]);
if ([placemarks count] > 0) {
CLPlacemark* placemark = placemarks[0];
CLLocationCoordinate2D coordinate = placemark.location.coordinate;
NSDictionary* address = placemark.addressDictionary;
MKPlacemark *place = [[MKPlacemark alloc]
initWithCoordinate:coordinate addressDictionary:address];
MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];
[mapItem openInMapsWithLaunchOptions:nil];
//关闭键盘
[_txtQueryKey resignFirstResponder];
}
}];
}
//
// ViewController.m
// MapLearn1
//
// Created by Beyondsoft on 13-12-27.
// Copyright (c) 2013年 Lance. All rights reserved.
//
#import "ViewController.h"
#import "MapLocation.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if ([CLLocationManager locationServicesEnabled]) {
_mapView.delegate = self;
_mapView.mapType = MKMapTypeStandard;
_mapView.showsUserLocation = YES;
[_mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_txtQueryKey release];
[_mapView release];
[super dealloc];
}
- (IBAction)geocodeQuery:(id)sender{
if (_txtQueryKey.text == nil || _txtQueryKey.text.length == 0) {
return ;
}
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"query recodes %d", [placemarks count]);
if ([placemarks count] > 0) {
[_mapView removeAnnotations:_mapView.annotations];
}
// for (int i = 0; i < [placemarks count]; i ++) {
// CLPlacemark *placeMark = [placemarks objectAtIndex:i];
// [_txtQueryKey resignFirstResponder];
// MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placeMark.location.coordinate, 10000, 10000);
// [_mapView setRegion:viewRegion animated:YES];
// MapLocation *annotation = [[MapLocation alloc] init];
// annotation.streetAddress = placeMark.thoroughfare;
// annotation.city = placeMark.locality;
// annotation.state = placeMark.administrativeArea;
// annotation.zip = placeMark.postalCode;
// annotation.coordinate = placeMark.location.coordinate;
// [_mapView addAnnotation:annotation];
// [annotation release];
// }
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < [placemarks count]; i ++) {
CLPlacemark *placemark = [placemarks objectAtIndex:i];
CLLocationCoordinate2D coordinate = placemark.location.coordinate;
NSDictionary *address = placemark.addressDictionary;
MKPlacemark *place = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:address];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:place];
[place release];
[array addObject:mapItem];
[mapItem release];
}
if ([array count] > 0) {
[MKMapItem openMapsWithItems:array launchOptions:nil];
}
[array release];
[_txtQueryKey resignFirstResponder];
}];
}
#pragma mark MKMapViewDelegate
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
NSLog(@"%@", [error description]);
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *annotationIdentifier = @"PIN_ annotation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (annotationView == nil) {
annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier] autorelease];
}
annotationView.pinColor = MKPinAnnotationColorPurple;
annotationView.animatesDrop = YES;
annotationView.canShowCallout = YES;
return annotationView;
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
_mapView.centerCoordinate = userLocation.location.coordinate;
}
@end