1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > iOS中 百度地图详解

iOS中 百度地图详解

时间:2022-02-02 04:41:30

相关推荐

iOS中 百度地图详解

版权声明:本文为博主原创文章,未经博主允许。

需要准备工作按照下图引进类库

需要添加

添加的两个字符串为:NSLocationWhenInUseUsageDescription / NSLocationAlwaysUsageDescription

默认定位设置:

设置工作准备完毕上代码:

指示根视图:

[objc]view plaincopy [[UINavigationBarappearance]setBarTintColor:[UIColorcolorWithRed:23/255.0green:180/255.0blue:237/255.0alpha:1]];self.window.rootViewController=[MapViewControllernew];

MapViewController.m//设置需要的属性

[objc]view plaincopy #import"MapViewController.h"#import<MapKit/MapKit.h>#import"Mypoint.h"#import<CoreLocation/CoreLocation.h>@interfaceMapViewController()<MKMapViewDelegate,CLLocationManagerDelegate>@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@implementationMapViewController

调用:

[objc]view plaincopy -(void)viewDidLoad{[superviewDidLoad];self.view.backgroundColor=[UIColorwhiteColor];self.locManager=[[CLLocationManageralloc]init];//代理_locManager.delegate=self;//定位精度_locManager.desiredAccuracy=kCLLocationAccuracyBest;//定位频率,10米定位一次CLLocationDistancedistance=10.0;_locManager.distanceFilter=distance;//更新位置[_locManagerrequestAlwaysAuthorization];[self.locManagerstartUpdatingLocation];//查询两个地点之间的距离[selfcountDistance];//调用布置视图[selfconfigureView];[selfsetMapView];}

//布置视图

[objc]view plaincopy -(void)configureView{//经度self.longitudeLabel=[[UILabelalloc]initWithFrame:CGRectMake(0,20,40,30)];self.longitudeLabel.text=@"经度";self.longitudetext=[[UITextFieldalloc]initWithFrame:CGRectMake(40,20,120,30)];self.longitudetext.borderStyle=UITextBorderStyleRoundedRect;//纬度self.latitudelabel=[[UILabelalloc]initWithFrame:CGRectMake(0,50,40,30)];self.latitudelabel.text=@"纬度";self.latitudeText=[[UITextFieldalloc]initWithFrame:CGRectMake(40,50,120,30)];self.latitudeText.borderStyle=UITextBorderStyleRoundedRect;//放置标注按钮self.button=[UIButtonbuttonWithType:(UIButtonTypeSystem)];self.button.frame=CGRectMake(30,73,100,30);[self.buttonsetTitle:@"放置标注"forState:(UIControlStateNormal)];[self.buttonaddTarget:selfaction:@selector(annotationAction:)forControlEvents:(UIControlEventTouchUpInside)];//地址输入self.destination=[[UITextFieldalloc]initWithFrame:CGRectMake(200,26,100,30)];self.destination.borderStyle=UITextBorderStyleRoundedRect;//查询按钮self.searchButton=[UIButtonbuttonWithType:(UIButtonTypeSystem)];self.searchButton.frame=CGRectMake(200,46,100,30);[self.searchButtonsetTitle:@"查询"forState:(UIControlStateNormal)];[self.searchButtonaddTarget:selfaction:@selector(detailSearchAction:)forControlEvents:(UIControlEventTouchUpInside)];[self.viewaddSubview:self.button];[self.viewaddSubview:self.latitudelabel];[self.viewaddSubview:self.longitudeLabel];[self.viewaddSubview:self.longitudetext];[self.viewaddSubview:self.latitudeText];[self.viewaddSubview:self.searchButton];[self.viewaddSubview:self.destination];}

[objc]view plaincopy -(void)countDistance{CLLocation*loc1=[[CLLocationalloc]initWithLatitude:34longitude:113];CLLocation*loc2=[[CLLocationalloc]initWithLatitude:35longitude:113];CLLocationDistancedistance=[loc1distanceFromLocation:loc2];NSLog(@"(%@)和(%@)的距离为:%f",loc1,loc2,distance);}

#pragma mark - CLLocationManagerDelegate Methods

// 此方法会被频繁调用

[objc]view plaincopy -(void)locationManager:(CLLocationManager*)managerdidUpdateLocations:(NSArray*)locations{//NSLog(@"didUpdateLocations---%lu",(unsignedlong)locations.count);//用来表示某个位置的地理信息,比如经纬度,海拔等等CLLocation*location=locations.lastObject;//location.coordinate.latitude维度//location.coordinate.longitude经度NSLog(@"经度==%f,维度==%f",location.coordinate.longitude,location.coordinate.latitude);self.longitudetext.text=[NSStringstringWithFormat:@"%f",location.coordinate.longitude];self.latitudeText.text=[NSStringstringWithFormat:@"%f",location.coordinate.latitude];//停止更新位置(不用定位服务时马上停止,因为非常耗电)//[managerstopUpdatingLocation];}

//调出地图

[objc]view plaincopy -(void)setMapView{//创建地图视图,初始化参数//MKMapTypeStandard显示街道和道路//MKMapTypeSatellite显示卫星//MKMapTypeHybrid显示混合地图self.mapView=[[MKMapViewalloc]initWithFrame:CGRectMake(0,100,320,460)];[self.mapViewsetMapType:MKMapTypeStandard];//显示用户当前的坐标,打开地图有相应的提示self.mapView.showsUserLocation=YES;//设置地图代理self.mapView.delegate=self;[self.viewaddSubview:self.mapView];}

#pragma mark 根据输入的经纬度确定位置

//放置标注

[objc]view plaincopy //放置标注-(void)annotationAction:(UIButton*)sender{//创建CLLocation设置经纬度CLLocationCoordinate2Dlocation=CLLocationCoordinate2DMake([[self.latitudeTexttext]floatValue],[[self.longitudetexttext]floatValue]);//创建标题NSString*title=[NSStringstringWithFormat:@"%f,%f",location.latitude,location.longitude];Mypoint*myPoint=[[Mypointalloc]initWithCoordinate:locationandTitle:title];//添加标注[self.mapViewaddAnnotation:myPoint];//放大到标注的位置MKCoordinateRegionregion=MKCoordinateRegionMakeWithDistance(location,0.01,0.01);[self.mapViewsetRegion:region];[selfshowLocation];}

//根据输入的经纬度显示位置

[objc]view plaincopy //根据输入的经纬度显示位置-(void)showLocation{//创建CLLocation设置经纬度CLLocationCoordinate2Dlocation=CLLocationCoordinate2DMake([[self.latitudeTexttext]floatValue],[[self.longitudetexttext]floatValue]);//放大到标注的位置MKCoordinateRegionregion=MKCoordinateRegionMakeWithDistance(location,0.01,0.01);[self.mapViewsetRegion:regionanimated:YES];}

#pragma mark 根据输入的地址搜寻位置

//根据地址输入搜索地图

[objc]view plaincopy //根据地址输入搜索地图-(void)detailSearchAction:(UIButton*)search{if(_destination.text==nil||[_destination.textlength]==0){return;}CLGeocoder*geocode=[[CLGeocoderalloc]init];[geocodegeocodeAddressString:_destination.textcompletionHandler:^(NSArray*placemarks,NSError*error){if(error||placemarks.count==0){NSLog(@"地址不存在");}else{for(CLPlacemark*placemarkinplacemarks){NSLog(@"name=%@locality=%@country=%@postalCode=%@",placemark.name,placemark.locality,placemark.country,placemark.postalCode);}CLPlacemark*firstPlacemark=[placemarksfirstObject];CLLocationDegreeslatitude=firstPlacemark.location.coordinate.latitude;CLLocationDegreeslongitude=firstPlacemark.location.coordinate.longitude;//显示经纬度self.latitudeText.text=[NSStringstringWithFormat:@"%.2f",latitude];self.longitudetext.text=[NSStringstringWithFormat:@"%.2f",longitude];[selfshowLocation];[selfsearchDetailLocationAction];}}];}

//根据地址搜寻位置

[objc]view plaincopy //根据地址搜寻位置-(void)searchDetailLocationAction{//创建CLLocation设置经纬度CLLocationCoordinate2Dlocation=CLLocationCoordinate2DMake([self.latitudeText.textfloatValue],[self.longitudetext.textfloatValue]);//创建标题NSString*title=[NSStringstringWithFormat:@"%f,%f",[self.latitudeText.textfloatValue],[self.longitudetext.textfloatValue]];Mypoint*myPoint=[[Mypointalloc]initWithCoordinate:locationandTitle:title];//添加标注[self.mapViewaddAnnotation:myPoint];//放大到标注的位置MKCoordinateRegionregion=MKCoordinateRegionMakeWithDistance(location,0.01,0.01);[self.mapViewsetRegion:region];}

建一个类: [objc]view plaincopy //.h#import<Foundation/Foundation.h>#import<MapKit/MapKit.h>@interfaceMypoint:NSObject<MKAnnotation>//实现MKAnnotion协议必须要定义这个属性@property(nonatomic,readonly)CLLocationCoordinate2Dcoordinate;//标题@property(nonatomic,copy)NSString*title;//初始化方法-(id)initWithCoordinate:(CLLocationCoordinate2D)candTitle:(NSString*)t;@end [objc]view plaincopy //.m#import"Mypoint.h"@implementationMypoint//初始化方法-(id)initWithCoordinate:(CLLocationCoordinate2D)candTitle:(NSString*)t{if(self=[superinit]){_coordinate=c;_title=t;}returnself;}@end

最终效果:

关注博主微博每日更新技术:/hanjunqiang

原文地址:/qq_31810357/article/details/49847813

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。