您的位置:首页 > 移动开发 > IOS开发

ios-使用ios 6苹果地图定位和跟踪

2014-09-11 10:39 513 查看
在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苹果地图定位和跟踪介绍已经完毕,程序运行效果图如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: