ios地图定位 分类: ios开发 2014-12-25 13:32 131人阅读 评论(0) 收藏

 

  废话不多说 直接上项目工程  ,还是 先看下效果图吧!

 

  项目中有两个文件,chonViewController 和mapLocation文件 ,mapLocation 文件用于地标显示 并有附加信息,昨天做得时候附加信息显示正常,今天想写点博客 却不显示了 ,很郁闷!

  

新建工程后 记得加入类库,MapKit.framework 具体添加方法 这里不在说明了

下面 看下 chonViewController.h 文件 代码如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2. #import <MapKit/MapKit.h>  
  3. #import "mapLocation.h"  
  4.   
  5.   
  6. @interface chonViewController : UIViewController<MKMapViewDelegate>  
  7. @property (weak, nonatomic) IBOutlet UITextField *txtQueryKey;  
  8.   
  9. @property (weak, nonatomic) IBOutlet MKMapView *mapView;  
  10. - (IBAction)geocodeQuery:(id)sender;  
  11.   
  12. @end  

chonViewController.m文件代码如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  chonViewController.m  
  3. //  MyLocation2  
  4. //  
  5. //  Created by choni on 14-5-13.  
  6. //  Copyright (c) 2014年 choni. All rights reserved.  
  7. //  
  8.   
  9. #import "chonViewController.h"  
  10.   
  11. @interface chonViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation chonViewController  
  16.   
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.     // 标注地图类型  
  21.     _mapView.mapType = MKMapTypeStandard ;  
  22.     //用于将当前视图控制器赋值给地图视图的delegate属性  
  23.     _mapView.delegate = self ;  
  24. }  
  25.   
  26. - (void)didReceiveMemoryWarning  
  27. {  
  28.     [super didReceiveMemoryWarning];  
  29.      
  30. }  
  31. #pragma mark - 查询按钮触发动作  
  32. - (IBAction)geocodeQuery:(id)sender {  
  33.       
  34.     if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) {  
  35.         return ;  
  36.     }  
  37.       
  38.     CLGeocoder *geocode = [[CLGeocoder alloc] init];  
  39.       
  40.     [geocode geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) {  
  41.         NSLog(@"查询记录数: %i",[placemarks count]);  
  42.           
  43.         if ([placemarks count ] > 0) {  
  44.             //移除目前地图上得所有标注点  
  45.             [_mapView removeAnnotations:_mapView.annotations];  
  46.               
  47.         }  
  48.           
  49.         for (int i = 0; i< [placemarks count]; i++) {  
  50.             CLPlacemark * placemark = placemarks[i];  
  51.               
  52.             //关闭键盘  
  53.             [_txtQueryKey resignFirstResponder];  
  54.             //调整地图位置和缩放比例,第一个参数是目标区域的中心点,第二个参数:目标区域南北的跨度,第三个参数:目标区域的东西跨度,单位都是米  
  55.             MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placemark.location.coordinate1000010000);  
  56.               
  57.             //重新设置地图视图的显示区域  
  58.             [_mapView setRegion:viewRegion animated:YES];  
  59.             // 实例化 MapLocation 对象  
  60.             mapLocation * annotation = [[mapLocation alloc] init];  
  61.             annotation.streetAddress = placemark.thoroughfare ;  
  62.             annotation.city = placemark.locality;  
  63.             annotation.state = placemark.administrativeArea ;  
  64.             annotation.zip = placemark.postalCode;  
  65.             annotation.coordinate = placemark.location.coordinate;  
  66.               
  67.             //把标注点MapLocation 对象添加到地图视图上,一旦该方法被调用,地图视图委托方法mapView:ViewForAnnotation:就会被回调  
  68.             [_mapView addAnnotation:annotation];  
  69.         }  
  70.           
  71.           
  72.     }];  
  73.       
  74. }  
  75.   
  76. #pragma mark mapView Delegate 地图 添加标注时 回调  
  77. - (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {  
  78.     // 获得地图标注对象  
  79.     MKPinAnnotationView * annotationView = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"];  
  80.     if (annotationView == nil) {  
  81.         annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN_ANNOTATION"];  
  82.     }  
  83.     // 设置大头针标注视图为紫色  
  84.     annotationView.pinColor = MKPinAnnotationColorPurple ;  
  85.     // 标注地图时 是否以动画的效果形式显示在地图上  
  86.     annotationView.animatesDrop = YES ;  
  87.     // 用于标注点上的一些附加信息  
  88.     annotationView.canShowCallout = YES ;  
  89.       
  90.     return annotationView;  
  91.       
  92. }  
  93.   
  94. - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation  
  95. {  
  96.     _mapView.centerCoordinate = userLocation.location.coordinate;  
  97. }  
  98.   
  99. - (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error {  
  100.     NSLog(@"error : %@",[error description]);  
  101. }  
  102.   
  103. @end  

mapLocation.h 代码如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  mapLocation.h  
  3. //  MyLocation2  
  4. //  
  5. //  Created by choni on 14-5-13.  
  6. //  Copyright (c) 2014年 choni. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import <MapKit/MapKit.h>  
  11.   
  12. @interface mapLocation : NSObject<MKAnnotation>  
  13. // 地图标点类必须实现 MKAnnotation 协议  
  14. // 地理坐标  
  15. @property (nonatomic ,readwrite) CLLocationCoordinate2D coordinate ;  
  16.   
  17. //街道属性信息  
  18. @property (nonatomic , copyNSString * streetAddress ;  
  19.   
  20. // 城市信息属性  
  21. @property (nonatomic ,copyNSString * city ;  
  22.   
  23. // 州,省 市 信息  
  24.   
  25. @property(nonatomic ,copy ) NSString * state ;  
  26. //邮编  
  27. @property (nonatomic ,copyNSString * zip  ;  
  28.   
  29.   
  30.   
  31.   
  32. @end  
mapLocation.m 文件如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  mapLocation.m  
  3. //  MyLocation2  
  4. //  
  5. //  Created by choni on 14-5-13.  
  6. //  Copyright (c) 2014年 choni. All rights reserved.  
  7. //  
  8.   
  9. #import "mapLocation.h"  
  10.   
  11. @implementation mapLocation  
  12. #pragma mark 标点上的主标题  
  13. - (NSString *)title{  
  14.     return @"您的位置!";  
  15. }  
  16.   
  17. #pragma  mark 标点上的副标题  
  18. - (NSString *)subtitle{  
  19.     NSMutableString *ret = [NSMutableString new];  
  20.     if (_state) {  
  21.         [ret appendString:_state];  
  22.     }  
  23.     if (_city) {  
  24.         [ret appendString:_city];  
  25.     }  
  26.     if (_city && _state) {  
  27.         [ret appendString:@", "];  
  28.     }  
  29.     if (_streetAddress && (_city || _state || _zip)) {  
  30.         [ret appendString:@" · "];  
  31.     }  
  32.     if (_streetAddress) {  
  33.         [ret appendString:_streetAddress];  
  34.     }  
  35.     if (_zip) {  
  36.         [ret appendFormat:@",  %@",_zip];  
  37.     }  
  38.     return ret;  
  39. }  
  40.   
  41. @end  

Ok  搞定, 代码中注释还是比较详细的!
posted @ 2014-12-25 13:32  欣哥传奇  阅读(174)  评论(0编辑  收藏  举报