高德地图纠正CLLocationManager坐标不准的问题

具体怎么做?

1、注册一个高德帐号,申请一个authKey,这个很有用。

2、下载高德API,下2.0版的。然后解压。因为我们是使用查找转换的API,所以我们只用到部份API。安装它,按照高德的文档做,不过有几点是要注意的:

1
2
(1)main.m及*AppDelegate.m必须改成.mm扩展名,因为高德使用的是objc++,不加会暴异常。
(2)只使用MASearchKit,别的都不用,不过配置过程还是要像官方文档里提出的一致。

3、下面就是使用代码,原理很简单,还是使用CLLocationManager取得坐标,不过在取得坐标之后就立即把这个会标让高德进行转换,高德会转换回一个正确的国标坐标,然后再把这个坐标添加到地图上即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
//  WhereamiViewController.h
//  Whereami
//
//  Created by kut on 3/11/13.
//  Copyright (c) 2013 kut. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "MASearchKit.h"

@interface WhereamiViewController : UIViewController
    <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate, MASearchDelegate>
{
    CLLocationManager *_locationManager;
    MASearch *_chinaSearchEngine;
}

@property (weak, nonatomic) IBOutlet UITextField *ibFieldLocationTitle;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *ibActivityIndicator;
@property (weak, nonatomic) IBOutlet MKMapView *ibMapWorldView;


- (void)findLocation;
- (void)foundLocation:(CLLocation *)location;

@end



//
//  WhereamiViewController.mm
//  Whereami
//
//  Created by kut on 3/11/13.
//  Copyright (c) 2013 kut. All rights reserved.
//

#import "WhereamiViewController.h"
#import "BNRMapPoint.h"

@implementation WhereamiViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        _locationManager = [[CLLocationManager alloc] init];
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        _locationManager.delegate = self;
        _chinaSearchEngine = [[MASearch alloc] initWithSearchKey:@"your authkey"
                                                        Delegate:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.ibMapWorldView.showsUserLocation = YES;
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];

    /// 就是这段代码,强大的高德!!
    MARGCSearchOption *option = [[MARGCSearchOption alloc] init];
    option.config = @"RGC";
    option.coors = [NSString stringWithFormat:@"%f,%f;",
                    location.coordinate.longitude,
                    location.coordinate.latitude];
    [_chinaSearchEngine gpsOffsetSearchWithOption:option];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"location manager could't found location with error: %@", error);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self findLocation];
    [textField resignFirstResponder];
    return YES;
}

- (void)findLocation
{
    [self.ibActivityIndicator startAnimating];
    self.ibFieldLocationTitle.hidden = YES;
    [_locationManager startUpdatingLocation];
}

- (void)foundLocation:(CLLocation *)location
{
    CLLocationCoordinate2D coordinate = location.coordinate;
    BNRMapPoint *mp = [[BNRMapPoint alloc]
                       initWithCoordinate:coordinate
                       title:self.ibFieldLocationTitle.text];
    [self.ibMapWorldView addAnnotation:mp];

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coordinate, 250.0f, 250.0f);
    self.ibMapWorldView.region = region;

    [self.ibActivityIndicator stopAnimating];
    self.ibFieldLocationTitle.hidden = NO;
    self.ibFieldLocationTitle.text = @"";
    [_locationManager stopUpdatingLocation];
}

/// 这段代码就是高德转换后的回调
- (void)gpsOffsetSearch:(MARGCSearchOption *)gpsOffSearchOption
                 Result:(MARGCSearchResult *)result
{
    MARGCItem *info = [result.rgcItemArray objectAtIndex:0];
    CLLocation *location = [[CLLocation alloc] initWithLatitude:info.y.floatValue
                                                      longitude:info.x.floatValue];
    [self foundLocation:location];
}

-(NSString *)keyForSearch
{
    return nil;
}

@end
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值