ios-使用ios 6苹果地图定位和跟踪
来源:互联网 发布:免费收银软件哪个好 编辑:程序博客网 时间:2024/04/24 04:24
在ios 6中,苹果自己的地图代替了谷歌地图,但是API编程接口没有太大的变化,所以开发人员不需要再学习很多新东西就能开发地图应用。开发地图应用需要导入#import<MapKit/MapKit.h>框架。
代码实现如下:
- (void)initUI{ UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 200, 30)]; label.backgroundColor = [UIColor clearColor]; label.textAlignment = NSTextAlignmentLeft; label.text = @"输入查询地点关键字:"; [self.view addSubview:label]; [label release]; textQuery = [[UITextField alloc] initWithFrame:CGRectMake(20, 90, 280, 30)]; textQuery.backgroundColor = [UIColor whiteColor]; textQuery.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; textQuery.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; textQuery.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:textQuery]; [textQuery release]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(60, 150, 200, 30)]; btn.backgroundColor = [UIColor whiteColor]; btn.layer.cornerRadius = 5.0f; btn.layer.masksToBounds = YES; btn.showsTouchWhenHighlighted = YES; [btn setTitle:@"查询" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(geoCodeQuery:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; [btn release]; m_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(20, 200, 280, 280)]; m_mapView.backgroundColor = [UIColor clearColor]; m_mapView.mapType = MKMapTypeHybrid; m_mapView.delegate = self; m_mapView.layer.cornerRadius = 5.0f; m_mapView.layer.masksToBounds = YES; m_mapView.showsUserLocation = YES; [m_mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES]; [self.view addSubview:m_mapView]; [m_mapView release]; /* MKMapTypeHybrid // 混合地图类型 MKMapTypeStandard // 标注地图类型 MKMapTypeSatellite // 卫星地图类型 */ /* MKUserTrackingModeFollowWithHeading // 可以跟踪用户的位置和方向变化 MKUserTrackingModeFollow // 可以跟踪用户的位置变化 MKUserTrackingModeNone // 没有用户跟踪模式 */}- (void)geoCodeQuery:(id)sender{ if (textQuery.text == nil || textQuery.text.length <= 0) { return; } CLGeocoder *geoCoder = [[CLGeocoder alloc] init]; [geoCoder geocodeAddressString:textQuery.text completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"查询记录数:%d",[placemarks count]); if ([placemarks count] > 0) { [m_mapView removeAnnotations:m_mapView.annotations]; } for (int i = 0; i < [placemarks count]; i ++) { CLPlacemark *placeMark = placemarks[i]; [textQuery resignFirstResponder]; // 调整地图位置和缩放比例 MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placeMark.location.coordinate, 10000, 10000); [m_mapView setRegion:viewRegion animated:YES]; // 添加大头针的附件信息 MapLocation *annotation = [[MapLocation alloc] init]; annotation.streetAddress = placeMark.thoroughfare; annotation.city = placeMark.locality; annotation.state = placeMark.postalCode; annotation.coordinate = placeMark.location.coordinate; [m_mapView addAnnotation:annotation]; [annotation release]; } }]; [geoCoder release];}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ static NSString *indentifier = @"PIN_ANNOTATION"; MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:indentifier]; if (annotationView == nil) { annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:indentifier] autorelease]; } annotationView.pinColor = MKPinAnnotationColorPurple; annotationView.animatesDrop = YES; annotationView.canShowCallout = YES; return annotationView; /* MKPinAnnotationColorRed // 大头针的颜色为红色 MKPinAnnotationColorGreen // 大头针的颜色为绿色 MKPinAnnotationColorPurple // 大头针的颜色为紫色 */ // animatesDrop /* 设置为YES,大头针以动画的效果显示在地图上;设置为NO,即没有动画效果 */ // canShowCallout /* 大头针附加信息,即点击大头针时,会出现气泡 */}- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{ m_mapView.centerCoordinate = userLocation.location.coordinate;}- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error{ NSLog(@"%@",[error description]);}
//// MapLocation.h//#import <Foundation/Foundation.h>#import <MapKit/MapKit.h>@interface MapLocation : NSObject <MKAnnotation>@property (copy, nonatomic) NSString *streetAddress;@property (copy, nonatomic) NSString *city;@property (copy, nonatomic) NSString *state;@property (copy, nonatomic) NSString *zip;@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;@end
//// MapLocation.m//#import "MapLocation.h"@implementation MapLocation- (NSString *)title{ return @"你的位置";}- (NSString *)subtitle{ NSMutableString *mutStr = [NSMutableString new]; if (_state) { [mutStr appendString:_state]; } if (_city) { [mutStr appendString:_city]; } if (_city && _state) { [mutStr appendString:@", "]; } if (_streetAddress && (_city || _state || _zip)) { [mutStr appendString:@" -"]; } if (_streetAddress) { [mutStr appendString:_streetAddress]; } if (_zip) { [mutStr appendFormat:@", %@",_zip]; } return mutStr;}@end
至此,ios 6苹果地图定位和跟踪介绍已经完毕,程序运行效果图如下:
0 0
- ios-使用ios 6苹果地图定位和跟踪
- iOS地图和定位
- iOS 定位和地图
- IOS开发 地图定位 使用
- iOS使用系统地图定位
- iOS地图开发和定位
- iOS系统地图和定位
- iOS的地图和定位
- iOS之苹果和百度地图的使用
- iOS - MapKit地图的简单使用和定位
- iOS苹果的定位
- IOS 调用苹果地图
- ios使用地图定位记录运动轨迹
- IOS使用内置地图,定位应用开发
- iOS 地图SDK的使用 -- 定位基础
- iOS地图定位使用几个小功能
- IOS 使用高德地图一次定位
- iOS地图定位使用几个小功能
- Let the Balloon Rise
- STL algorithm算法equal(12)
- 实际用户ID”、“有效用户ID”和“保存的设置用户ID”
- Android学习之路
- Linux下vim的配置
- ios-使用ios 6苹果地图定位和跟踪
- 头文件不匹配导致的崩溃问题
- qt 解决空间中文显示问题
- 迅为三星四核 cortex a9 + Linux开发板特点介绍
- handler thread asynctask runnable 分析
- 使用uploadify3上传图片的示例支持图片预览
- MongoDB基本管理命令
- Linux下的最基本的几个命令
- Longest Palindromic Substring