map

- (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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值