寫在前面: 盡管精確度已然調至最佳, 但還是會有一定程度的定位誤差, 作為開發(fā)者我已然盡力, 只能希望apple官方做些優(yōu)化吧
輸入目的地地址時最好填入區(qū), 不然有時會定位不到
http://developer.baidu.com/map/wiki/index.php?title=uri/api/ios
這個網址是百度地圖官方的, 提供web和ios跳轉到百度地圖app的各種url, 諸如可以輸入起點終點進行導航, 或者把輸入的地址顯示在百度地圖上等等
下面這個對地址反編譯成經緯度就已經要用到了, 更別說后面的定位了
#import <CoreLocation/CoreLocation.h>
要調用自帶的高德地圖, 就要
#import <MapKit/MapKit.h>
我的demo控件只是創(chuàng)建了一個開始跳轉到地圖的按鈕和輸入目的地的textField而已
以下是屬性
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *buttonOfBeginLocate;
//目的地經緯度
@property (nonatomic, assign) CGFloat longitude;
@property (nonatomic, assign) CGFloat latitude;
//目前所在地經緯度
@property (nonatomic, assign) CGFloat currentLatitude;
@property (nonatomic, assign) CGFloat currentLongitude;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLGeocoder *geocoder;
寫倆懶加載
#pragma mark - 懶加載
- (CLLocationManager *)locationManager{
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
// 設置定位精確度到米
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// 設置過濾器為無
_locationManager.distanceFilter = kCLDistanceFilterNone;
// 一個是requestAlwaysAuthorization厨相,一個是requestWhenInUseAuthorization
[_locationManager requestWhenInUseAuthorization];//這句話ios8以上版本使用。
}
return _locationManager;
}
- (CLGeocoder *)geocoder{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
在button點擊事件里
- (void)beginLocate:(UIButton *)button{
[self.locationManager startUpdatingLocation];
[self translateAddress];
//設備安裝了百度地圖
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
[self dumpToBaidu];
}
//沒安裝百度地圖, 跳轉到自帶的高德
else{
[self testAppleMapWithLatitude:_latitude longitude:_longitude];
}
}
#pragma mark - 定位協(xié)議方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
_currentLatitude = newLocation.coordinate.latitude;
_currentLongitude = newLocation.coordinate.longitude;
if (_currentLatitude && _currentLongitude) {
[manager stopUpdatingLocation];
}
}
#pragma mark - 目的地地址編譯為經緯度.
//地址盡量有區(qū), 比如龍湖區(qū)
- (void)translateAddress{
[self.geocoder geocodeAddressString:_textField.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0 && error == nil) {
CLPlacemark *placemark = placemarks.firstObject;
_longitude = placemark.location.coordinate.longitude;
_latitude = placemark.location.coordinate.latitude;
}
else if (placemarks.count == 0 && error == nil){
NSLog(@"placemarks元素為0");
}else if(error != nil){
NSLog(@"an arror occurred = %@", error);
}
}];
}
#pragma mark - 跳轉到百度地圖
- (void)dumpToBaidu{
//轉成UTF8 [NSCharacterSet URLQueryAllowedCharacterSet]
//四個參數分別是, 當前位置緯度, 經度, 目的地緯度, 經度
NSString *url4 = [[NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=%f,%f&mode=driving&src=webapp.navi.yourCompanyName.yourAppName", _currentLatitude, _currentLongitude, _latitude, _longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//根據起點終點跳轉到百度地圖并進行駕車導航
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url4]];
}
#pragma mark - 跳轉到蘋果高德地圖
-(void)testAppleMapWithLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{
// CLLocationCoordinate2D coords1 = CLLocationCoordinate2DMake(30.691793,104.088264);
// CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(30.691293,104.088264);
CLLocationCoordinate2D coords1 = CLLocationCoordinate2DMake(_currentLatitude, _currentLongitude);
// CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(40.001,116.404);
CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(latitude, longitude);
//這個判斷我沒試過, 現(xiàn)在也沒幾個用ios6了吧
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0){
// ios6以下嗽测,調用google map {
NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d", coords1.latitude,coords1.longitude,coords2.latitude,coords2.longitude];
NSURL *aURL = [NSURL URLWithString:urlString]; //打開網頁google地圖
[[UIApplication sharedApplication] openURL:aURL];
}else// 直接調用ios自己帶的apple map
{
//當前的位置
// MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
//起點
MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords1 addressDictionary:nil]];
currentLocation.name = @"目前位置";
//目的地的位置
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords2 addressDictionary:nil]];
//顯示在地圖上的目的地名稱
toLocation.name = @"目的地";
NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil];
NSDictionary *options = @{
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES
}; //打開蘋果自身地圖應用撑帖,并呈現(xiàn)特定的item
[MKMapItem openMapsWithItems:items launchOptions:options];
}
}
最后感謝 http://blog.csdn.net/hengshujiyi/article/details/45560609 給我靈感