--转自:keakon的涂鸦馆 http://www.keakon.net/2011/07/02/WGS84%E5%9D%90%E6%A0%87%E8%BD%AC%E7%81%AB%E6%98%9F%E5
在这个神奇的国度里,我们总得学习一些有中国特色的东东,例如“火星坐标”。也许有人还不知道这是什么玩意,我就简要介绍一下吧。
如果你有带GPS模块的智能手机,打开定位功能,然后访问 Google地图。只要你身处中国大陆,你就会发现定位不准,大概有几百米的偏差。然而运行一些导航软件,你又会发现定位很准确,说明手机的GPS模块确实是正常的。// MapDemoViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapDemoViewController : UIViewController <CLLocationManagerDelegate> {
UITextField *wgsText;
UITextField *marsText;
CLLocationManager *locationManager;
}
@property (nonatomic, retain) IBOutlet UITextField *wgsText;
@property (nonatomic, retain) IBOutlet UITextField *marsText;
@property (nonatomic, retain) CLLocationManager *locationManager;
@end
// MapDemoViewController.m
#import "MapDemoViewController.h"
@implementation MapDemoViewController
@synthesize wgsText;
@synthesize marsText;
@synthesize locationManager;
- (void)viewDidLoad {
[super viewDidLoad];
if ([CLLocationManager locationServicesEnabled]) { // 检查定位服务是否可用
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation]; // 开始定位
}
}
// 定位成功时调用
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D coordinate = newLocation.coordinate;
CLLocationDegrees latitude = coordinate.latitude;
CLLocationDegrees longitude = coordinate.longitude;
wgsText.text = [NSString stringWithFormat:@"%f,%f", latitude, longitude];
}
// 定位失败时调用
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
wgsText.text = [error localizedDescription];
marsText.text = @"";
}
- (void)viewDidUnload {
self.wgsText = nil;
self.marsText = nil;
self.locationManager = nil;
}
- (void)dealloc {
[wgsText release];
[marsText release];
[locationManager release];
[super dealloc];
}
@end
构建前别忘了在Framework里加上CoreLocation.framework。这个例子只能在真机上运行,因为模拟器没有定位功能,肯定会失败。
self.search = [MSearch MSearchWithKey:@"你申请的API KEY" delegate:self];
poiXY.X = longitude; // 经度
poiXY.Y = latitude; // 维度
[search GPSToOffSetByPoint:self];
这里不需要设置options属性,使用初始值即可。GPSToOffSetByPoint:方法会返回SEARCH_THREAD_ID,由于不需要停止线程,因此无视。
-(void) GPSToOffsetResponse:(MLONLAT)lonlat {
marsText.text = [NSString stringWithFormat:@"%f,%f", lonlat.Y, lonlat.X];
}
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@class MKLocationManager;
@interface ViewController : UIViewController <CLLocationManagerDelegate> {
UITextField *wgsText;
UITextField *marsText;
CLLocationManager *locationManager;
MKLocationManager *shiftLocationManager;
}
@property (nonatomic, retain) IBOutlet UITextField *wgsText;
@property (nonatomic, retain) IBOutlet UITextField *marsText;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) MKLocationManager *shiftLocationManager;
@end
#import <MapKit/MapKit.h>
#import "ViewController.h"
@interface MKLocationManager : NSObject
- (CLLocation*)_applyChinaLocationShift:(CLLocation*)arg;
- (BOOL)chinaShiftEnabled;
+ (id)sharedLocationManager;
@end
@implementation ViewController
@synthesize wgsText;
@synthesize marsText;
@synthesize locationManager;
@synthesize shiftLocationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
if ([CLLocationManager locationServicesEnabled]) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
self.shiftLocationManager = MKLocationManager.sharedLocationManager;
if (shiftLocationManager.chinaShiftEnabled) {
[[[MKMapView alloc] init] release]; // 必须初始化一个MKMapView对象,否则调用_applyChinaLocationShift:时会出错
} else {
self.shiftLocationManager = nil;
}
}
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D coordinate = newLocation.coordinate;
wgsText.text = [NSString stringWithFormat:@"%f,%f", coordinate.latitude, coordinate.longitude];
if (shiftLocationManager) {
newLocation = [shiftLocationManager _applyChinaLocationShift:newLocation]; // 获得偏移后的坐标
coordinate = newLocation.coordinate;
marsText.text = [NSString stringWithFormat:@"%f,%f", coordinate.latitude, coordinate.longitude];
}
}
// ...
@end
接着添加CoreLocation和MapKit这2个框架,就可以构建了。