iOS地图开发

          周末自己练习写了一个demo,通过查询地址,然后在地图上用大头针显示出来。主要使用到的类是MKMapView,MKAnnotation,CLGeocoder,MKMapViewDelegate,MKPinAnnotationView。

        先看下效果:

     iOS地图开发_第1张图片

     1.新建一个项目,勾选storyboard和arc选项

     2.导入MapKit.framework和CoreLocation.framework.

     3.界面布局,添加一个UITextField,UIButton和MKMapView,并设置好映射关系:

     iOS地图开发_第2张图片


各个控件的映射关系:iOS地图开发_第3张图片

  4.写代码:

  mViewController.h文件

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

@class mCustomTextField;

@interface mViewController : UIViewController <UITextFieldDelegate,MKMapViewDelegate>
@property (nonatomic,strong) IBOutlet MKMapView * mapView;
@property (nonatomic,strong) IBOutlet UIButton * searchButton;
@property (nonatomic,strong) IBOutlet mCustomTextField * searchTextField;


-(IBAction)search:(id)sender;
@end
  mViewController.m文件:
//
//  mViewController.m
//  MapDemo
//
//  Created by Carl on 13-7-20.
//  Copyright (c) 2013年 contact..com. All rights reserved.
//

#import "mViewController.h"
#import "mCustomTextField.h"
#import "mAnnotationView.h"

@interface mViewController ()

@end

@implementation mViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _mapView.mapType = MKMapTypeStandard;
    _mapView.showsUserLocation = YES;
    [_mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
    
    
    
    //指示器
    UIActivityIndicatorView * indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [indicatorView setFrame:CGRectMake(0, 0, 60.0f, 60.0f)];
    indicatorView.center = self.view.center;
    indicatorView.hidesWhenStopped = YES;
    indicatorView.tag = 5;

    [self.view addSubview:indicatorView];
    
    
    
    
	// Do any additional setup after loading the view, typically from a nib.
}

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

-(IBAction)search:(id)sender
{
    [_searchTextField resignFirstResponder];
    
    
    if([_searchTextField.text length] == 0)
    {
        NSLog(@"the address is nil");
        return ;
    }
    
    UIActivityIndicatorView * indicatorView = (UIActivityIndicatorView *)[self.view viewWithTag:5];
    [indicatorView startAnimating];
    
    
    CLGeocoder * geocoder = [[CLGeocoder alloc] init];

    [geocoder geocodeAddressString:_searchTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {
        
        
        if(error)
        {
            NSLog(@"Geocoder error : %@",error);
            return ;
        }
        
        if([placemarks count] == 0)
        {
            NSLog(@"Could found the address.");
            return ;
        }
        
        CLPlacemark * placeMark = [placemarks objectAtIndex:0];
        
        _mapView.centerCoordinate = placeMark.location.coordinate;
     
        mAnnotationView * annotationView = [[mAnnotationView alloc] initWithCLLocationCoordinate2d:placeMark.location.coordinate title:placeMark.locality subTitle:[NSString stringWithFormat:@"%@-%@",placeMark.country,placeMark.administrativeArea]];
     
        [_mapView addAnnotation:annotationView];
     
        [_mapView selectAnnotation:annotationView animated:YES];
     
     
        NSLog(@"country:%@,locality:%@,%@,%@",placeMark.country,placeMark.locality,placeMark.administrativeArea,placeMark.region);
        
        [indicatorView stopAnimating];
     
    }];


}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_searchTextField resignFirstResponder];
    return YES;
}



-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//    _mapView.centerCoordinate = userLocation.location.coordinate;
}

-(void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
    NSLog(@"error : %@",error);
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    
    if([annotation isKindOfClass:[mAnnotationView class]] == NO)
    {
        NSLog(@"not mAnnotationView");
        return nil;
    }
    
    
    static NSString * AnnotationIdentifier = @"Annotation";
    
    MKPinAnnotationView * result = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    
    if(result == nil)
    {
        
        result = [[MKPinAnnotationView alloc] initWithAnnotation:(mAnnotationView *)annotation reuseIdentifier:AnnotationIdentifier];
        
    }
    
    [result setCanShowCallout:YES];
    result.draggable = true;
    result.animatesDrop = YES;
    result.selected = YES;

    
    
    return result;
}


@end

 

你可能感兴趣的:(ios,MkMapView,地图开发,CLGeocoder)