废话不多说 直接上项目工程 ,还是 先看下效果图吧!
项目中有两个文件,chonViewController 和mapLocation文件 ,mapLocation 文件用于地标显示 并有附加信息,昨天做得时候附加信息显示正常,今天想写点博客 却不显示了 ,很郁闷!
新建工程后 记得加入类库,MapKit.framework 具体添加方法 这里不在说明了
下面 看下 chonViewController.h 文件 代码如下:
- #import <UIKit/UIKit.h>
- #import <MapKit/MapKit.h>
- #import "mapLocation.h"
- @interface chonViewController : UIViewController<MKMapViewDelegate>
- @property (weak, nonatomic) IBOutlet UITextField *txtQueryKey;
- @property (weak, nonatomic) IBOutlet MKMapView *mapView;
- - (IBAction)geocodeQuery:(id)sender;
- @end
chonViewController.m文件代码如下:
- //
- // chonViewController.m
- // MyLocation2
- //
- // Created by choni on 14-5-13.
- // Copyright (c) 2014年 choni. All rights reserved.
- //
- #import "chonViewController.h"
- @interface chonViewController ()
- @end
- @implementation chonViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // 标注地图类型
- _mapView.mapType = MKMapTypeStandard ;
- //用于将当前视图控制器赋值给地图视图的delegate属性
- _mapView.delegate = self ;
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- }
- #pragma mark - 查询按钮触发动作
- - (IBAction)geocodeQuery:(id)sender {
- if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) {
- return ;
- }
- CLGeocoder *geocode = [[CLGeocoder alloc] init];
- [geocode geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) {
- NSLog(@"查询记录数: %i",[placemarks count]);
- if ([placemarks count ] > 0) {
- //移除目前地图上得所有标注点
- [_mapView removeAnnotations:_mapView.annotations];
- }
- for (int i = 0; i< [placemarks count]; i++) {
- CLPlacemark * placemark = placemarks[i];
- //关闭键盘
- [_txtQueryKey resignFirstResponder];
- //调整地图位置和缩放比例,第一个参数是目标区域的中心点,第二个参数:目标区域南北的跨度,第三个参数:目标区域的东西跨度,单位都是米
- MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, 10000, 10000);
- //重新设置地图视图的显示区域
- [_mapView setRegion:viewRegion animated:YES];
- // 实例化 MapLocation 对象
- 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;
- //把标注点MapLocation 对象添加到地图视图上,一旦该方法被调用,地图视图委托方法mapView:ViewForAnnotation:就会被回调
- [_mapView addAnnotation:annotation];
- }
- }];
- }
- #pragma mark mapView Delegate 地图 添加标注时 回调
- - (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
- // 获得地图标注对象
- MKPinAnnotationView * annotationView = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"];
- if (annotationView == nil) {
- annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN_ANNOTATION"];
- }
- // 设置大头针标注视图为紫色
- annotationView.pinColor = MKPinAnnotationColorPurple ;
- // 标注地图时 是否以动画的效果形式显示在地图上
- annotationView.animatesDrop = YES ;
- // 用于标注点上的一些附加信息
- annotationView.canShowCallout = YES ;
- return annotationView;
- }
- - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
- {
- _mapView.centerCoordinate = userLocation.location.coordinate;
- }
- - (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error {
- NSLog(@"error : %@",[error description]);
- }
- @end
mapLocation.h 代码如下:
- //
- // mapLocation.h
- // MyLocation2
- //
- // Created by choni on 14-5-13.
- // Copyright (c) 2014年 choni. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import <MapKit/MapKit.h>
- @interface mapLocation : NSObject<MKAnnotation>
- // 地图标点类必须实现 MKAnnotation 协议
- // 地理坐标
- @property (nonatomic ,readwrite) CLLocationCoordinate2D coordinate ;
- //街道属性信息
- @property (nonatomic , copy) NSString * streetAddress ;
- // 城市信息属性
- @property (nonatomic ,copy) NSString * city ;
- // 州,省 市 信息
- @property(nonatomic ,copy ) NSString * state ;
- //邮编
- @property (nonatomic ,copy) NSString * zip ;
- @end
- //
- // mapLocation.m
- // MyLocation2
- //
- // Created by choni on 14-5-13.
- // Copyright (c) 2014年 choni. All rights reserved.
- //
- #import "mapLocation.h"
- @implementation mapLocation
- #pragma mark 标点上的主标题
- - (NSString *)title{
- return @"您的位置!";
- }
- #pragma mark 标点上的副标题
- - (NSString *)subtitle{
- NSMutableString *ret = [NSMutableString new];
- if (_state) {
- [ret appendString:_state];
- }
- if (_city) {
- [ret appendString:_city];
- }
- if (_city && _state) {
- [ret appendString:@", "];
- }
- if (_streetAddress && (_city || _state || _zip)) {
- [ret appendString:@" · "];
- }
- if (_streetAddress) {
- [ret appendString:_streetAddress];
- }
- if (_zip) {
- [ret appendFormat:@", %@",_zip];
- }
- return ret;
- }
- @end
Ok 搞定, 代码中注释还是比较详细的!