zoukankan      html  css  js  c++  java
  • iOS.定位服务与地图应用.06.调用iOS苹果地图

    #import <UIKit/UIKit.h>
    #import <CoreLocation/CoreLocation.h>
    #import <MapKit/MapKit.h>
    
    @interface T20140621001526ViewController : UIViewController
    
    @property (weak, nonatomic) IBOutlet UITextField *txtQueryKey;
    
    @property (weak, nonatomic) IBOutlet UITextView *txtView;
    
    - (IBAction)geocodeQuery:(id)sender;
    
    @end
    #import "T20140621001526ViewController.h"
    
    @interface T20140621001526ViewController ()
    
    @end
    
    @implementation T20140621001526ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // 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)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];
                
                /*
                 //地图上设置行车路线
                 NSDictionary* options =[[NSDictionary alloc]initWithObjectsAndKeys:
                 MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsDirectionsModeKey, nil];
                 
                 MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];
                 [mapItem openInMapsWithLaunchOptions:options];
                 */
                
                //关闭键盘
                [_txtQueryKey resignFirstResponder];
            }
        }];
        
    }
    
    /*
     //多个点需要标注
     - (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]);
     
     NSMutableArray* array = [NSMutableArray new];
     
     for (int i = 0; i < [placemarks count]; i++) {
     
     CLPlacemark* placemark = placemarks[i];
     
     CLLocationCoordinate2D coordinate = placemark.location.coordinate;
     NSDictionary* address = placemark.addressDictionary;
     
     MKPlacemark *place = [[MKPlacemark alloc]
     initWithCoordinate:coordinate addressDictionary:address];
     
     MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];
     
     [array addObject:mapItem];
     }
     
     //关闭键盘
     [_txtQueryKey resignFirstResponder];
     
     if ([array count] > 0) {
     [MKMapItem openMapsWithItems:array launchOptions:nil];
     }
     }];
     }
     */
    @end
  • 相关阅读:
    第一次个人编程作业
    第一次结对编程作业
    第一次编程作业——地址簿
    我的第一次博客作业
    团队作业1
    作业一
    寒假作业四
    寒假作业三
    第二次寒假作业
    电梯
  • 原文地址:https://www.cnblogs.com/cqchen/p/3802288.html
Copyright © 2011-2022 走看看