你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS中 百度地圖詳解 韓俊強的博文

iOS中 百度地圖詳解 韓俊強的博文

編輯:IOS開發綜合

需要准備工作按照下圖引進類庫

\

需要添加

\

添加的兩個字符串為:NSLocationWhenInUseUsageDescription / NSLocationAlwaysUsageDescription

 

默認定位設置:

\

 

設置工作准備完畢上代碼:

指示根視圖:

 

    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]];
    self.window.rootViewController = [MapViewController new];

 

MapViewController.m//設置需要的屬性

 

#import MapViewController.h
#import 
#import Mypoint.h
#import 
@interface MapViewController ()
@property (nonatomic, strong) MKMapView *mapView;
//經度
@property (nonatomic, strong) UITextField *longitudetext;
//緯度
@property (nonatomic, strong) UITextField *latitudeText;
//經度
@property (nonatomic, strong) UILabel *longitudeLabel;
//緯度
@property (nonatomic, strong) UILabel *latitudelabel;
//防止標注的button[
@property (nonatomic, strong) UIButton *button;
//地址輸入
@property (nonatomic, strong) UITextField *destination;
//輸入地址查詢地圖
@property (nonatomic, retain) UIButton *searchButton;
//可以獲取設備當前的經緯度信息
@property (nonatomic, strong) CLLocationManager *locManager;
@end

@implementation MapViewController

 

 

調用:
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.locManager = [[CLLocationManager alloc]init];
    //代理
    _locManager.delegate = self;
    //定位精度
    _locManager.desiredAccuracy = kCLLocationAccuracyBest;
    //定位頻率,10米定位一次
    CLLocationDistance distance = 10.0;
    _locManager.distanceFilter = distance;
    //更新位置
    [_locManager requestAlwaysAuthorization];
    [self.locManager startUpdatingLocation];
    //查詢兩個地點之間的距離
    [self countDistance];
    //調用布置視圖
    [self configureView];
    [self setMapView];
}

//布置視圖

 

- (void)configureView{
    //經度
    self.longitudeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 40, 30)];
    self.longitudeLabel.text = @經度;
    
    self.longitudetext = [[UITextField alloc]initWithFrame:CGRectMake(40, 20, 120, 30)];
    self.longitudetext.borderStyle = UITextBorderStyleRoundedRect;
    //緯度
    self.latitudelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 40, 30)];
    self.latitudelabel.text = @緯度;
    
    self.latitudeText = [[UITextField alloc]initWithFrame:CGRectMake(40, 50, 120, 30)];
    self.latitudeText.borderStyle = UITextBorderStyleRoundedRect;
    //放置標注按鈕
    self.button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.button.frame = CGRectMake(30, 73, 100, 30);
    [self.button setTitle:@放置標注 forState:(UIControlStateNormal)];
    [self.button addTarget:self action:@selector(annotationAction:) forControlEvents:(UIControlEventTouchUpInside)];
    //地址輸入
    self.destination = [[UITextField alloc]initWithFrame:CGRectMake(200, 26, 100, 30)];
    self.destination.borderStyle = UITextBorderStyleRoundedRect;
    //查詢按鈕
    self.searchButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.searchButton.frame = CGRectMake(200, 46, 100, 30);
    [self.searchButton setTitle:@查詢 forState:(UIControlStateNormal)];
    [self.searchButton addTarget:self action:@selector(detailSearchAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:self.button];
    [self.view addSubview:self.latitudelabel];
    [self.view addSubview:self.longitudeLabel];
    [self.view addSubview:self.longitudetext];
    [self.view addSubview:self.latitudeText];
    [self.view addSubview:self.searchButton];
    [self.view addSubview:self.destination];
}

- (void)countDistance{
    CLLocation *loc1 = [[CLLocation alloc]initWithLatitude:34 longitude:113];
    CLLocation *loc2 = [[CLLocation alloc]initWithLatitude:35 longitude:113];
    CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
    NSLog(@(%@) 和 (%@)的距離為: %f, loc1, loc2, distance);
}

 

#pragma mark - CLLocationManagerDelegate Methods

// 此方法會被頻繁調用

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//    NSLog(@didUpdateLocations---%lu, (unsigned long)locations.count);
    // 用來表示某個位置的地理信息, 比如經緯度, 海拔等等
    CLLocation *location = locations.lastObject;
    // location.coordinate.latitude  維度
    // location.coordinate.longitude 經度
    NSLog(@經度 == %f, 維度 == %f, location.coordinate.longitude, location.coordinate.latitude);
    self.longitudetext.text = [NSString stringWithFormat:@%f,location.coordinate.longitude];
    self.latitudeText.text = [NSString stringWithFormat:@%f,location.coordinate.latitude];
    // 停止更新位置(不用定位服務時馬上停止, 因為非常耗電)
//    [manager stopUpdatingLocation];
}

//調出地圖

- (void)setMapView{
    //創建地圖視圖,初始化參數
    //MKMapTypeStandard 顯示街道和道路
    //MKMapTypeSatellite 顯示衛星
    //MKMapTypeHybrid 顯示混合地圖
    self.mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 100, 320, 460)];
    [self.mapView setMapType:MKMapTypeStandard];
    //顯示用戶當前的坐標,打開地圖有相應的提示
    self.mapView.showsUserLocation = YES;
    //設置地圖代理
    self.mapView.delegate = self;
    [self.view addSubview:self.mapView];
}

#pragma mark 根據輸入的經緯度確定位置

//放置標注

//放置標注
- (void)annotationAction:(UIButton *)sender{
    //創建CLLocation 設置經緯度
    CLLocationCoordinate2D location = CLLocationCoordinate2DMake([[self.latitudeText text]floatValue], [[self.longitudetext text]floatValue] );
    //創建標題
    NSString *title = [NSString stringWithFormat:@%f,%f,location.latitude,location.longitude];
    Mypoint *myPoint = [[Mypoint alloc]initWithCoordinate:location andTitle:title];
    //添加標注
    [self.mapView addAnnotation:myPoint];
    //放大到標注的位置
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 0.01, 0.01);
    [self.mapView setRegion:region];
    [self showLocation];
}

//根據輸入的經緯度顯示位置

//根據輸入的經緯度顯示位置
- (void)showLocation{
    //創建CLLocation 設置經緯度
    CLLocationCoordinate2D location = CLLocationCoordinate2DMake([[self.latitudeText text]floatValue], [[self.longitudetext text]floatValue] );
    //放大到標注的位置
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 0.01, 0.01);
    [self.mapView setRegion:region animated:YES];
}

#pragma mark 根據輸入的地址搜尋位置

//根據地址輸入搜索地圖

//根據地址輸入搜索地圖
- (void)detailSearchAction:(UIButton *)search{
    if (_destination.text == nil || [_destination.text length] == 0) {
        return;
    }
    CLGeocoder *geocode = [[CLGeocoder alloc]init];
    [geocode geocodeAddressString:_destination.text completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error || placemarks.count == 0) {
            NSLog(@地址不存在);
        }else{
            for (CLPlacemark *placemark in placemarks) {
                NSLog(@name=%@ locality=%@ country=%@ postalCode=%@,placemark.name,placemark.locality,placemark.country,placemark.postalCode);
            }
            CLPlacemark *firstPlacemark = [placemarks firstObject];
            CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;
            CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;
            //顯示經緯度
            self.latitudeText.text = [NSString stringWithFormat:@%.2f,latitude];
            self.longitudetext.text = [NSString stringWithFormat:@%.2f,longitude];
            [self showLocation];
            [self searchDetailLocationAction];
        }
    }];
}

 

//根據地址搜尋位置

//根據地址搜尋位置
- (void)searchDetailLocationAction{
    //創建CLLocation 設置經緯度
    CLLocationCoordinate2D location = CLLocationCoordinate2DMake([self.latitudeText.text floatValue], [self.longitudetext.text floatValue]);
    //創建標題
    NSString *title = [NSString stringWithFormat:@%f,%f,[self.latitudeText.text floatValue], [self.longitudetext.text floatValue]];
    Mypoint *myPoint = [[Mypoint alloc]initWithCoordinate:location andTitle:title];
    //添加標注
    [self.mapView addAnnotation:myPoint];
    //放大到標注的位置
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 0.01, 0.01);
    [self.mapView setRegion:region];
}

建一個類:

 

//.h
#import 
#import 
@interface Mypoint : NSObject
//實現MKAnnotion協議必須要定義這個屬性
@property (nonatomic,readonly)CLLocationCoordinate2D coordinate;
//標題
@property (nonatomic,copy)NSString *title;

//初始化方法
- (id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString *)t;

@end
//.m
#import Mypoint.h

@implementation Mypoint
//初始化方法
- (id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString *)t{
    if (self = [super init]) {
        _coordinate = c;
        _title = t;
    }
    return self;
}
@end

 

 

最終效果:
\

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved