IOS地图及定位使用
1.定位
定位使用CoreLocation库,引入CoreLocation/CoreLocation。创建CLLocationManager对象,使用startUpdatingLocation方法开始更新位置信息。
_mgr = [[CLLocationManager alloc] init]; [_mgr requestWhenInUseAuthorization]; _mgr.delegate = self; [_mgr startUpdatingLocation];
更新成功后,会调用CLLocationManagerDelegate的代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *location = [locations objectAtIndex:locations.count - 1]; NSLog(@"%f,%f\n",location.coordinate.latitude,location.coordinate.longitude); }
如果要停止更新,调用stopUpdatingLocation即可。
PS1:如果启动时提示是否打开定位,需要调用CLLocationManager对象的requestWhenInUseAuthorization。
PS2:定位用途的提示,在info.plist中添加NSLocationWhenInUseUsageDescription、NSLocationAlwaysUsageDescription添加提示
2.坐标与城市的转换
使用CLGeocoder的
geocodeAddressString将地址转换为CLPlacemark(包含坐标、城市信息等内容),如
CLGeocoder *coder = [[CLGeocoder alloc] init]; [coder geocodeAddressString:@"青岛" completionHandler:^(NSArray *placemarks, NSError *error) { for (int i = 0; i < placemarks.count; i++) { CLPlacemark *placemark = [placemarks objectAtIndex:i]; NSLog(@"%@,%@",placemark.locality,placemark.country); } }];
或reverseGeocodeLocation将坐标转换为CLPlacemark,如
CLGeocoder *geo = [[CLGeocoder alloc] init]; [geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error){ CLPlacemark *placemark = [placemarks objectAtIndex:0]; NSLog(placemark.locality); }];
3.标记(大头针)
大头针要实现MKAnnotation协议,设置其中的coordinate(坐标)、title(标题)、subtitle(副标题)后,用mapview的addAnnotation方法即可添加到地图上,如
@interface MKAnno : NSObject<MKAnnotation> @property (nonatomic,assign) CLLocationCoordinate2D coordinate; // Title and subtitle for use by selection UI. @property (nonatomic, assign) NSString *title; @property (nonatomic, assign) NSString *subtitle; @end
添加时使用
MKAnno *anno = [[MKAnno alloc] init]; anno.coordinate = _mapview.centerCoordinate; anno.title = @"测试位置"; anno.subtitle = @"这就是个测试位置而已"; [_mapview addAnnotation: anno];
调用效果
4.自定义大头针
实现MKMapViewDelegate代理的-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation方法,如
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { NSString *identifier = @"test"; MKPinAnnotationView *view = (MKPinAnnotationView*)[_mapview dequeueReusableAnnotationViewWithIdentifier:identifier]; if (!view) { view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; } //设置右边View为一个自定义Button UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; [button setTitle:@"我就试试" forState:UIControlStateNormal]; [button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside]; [button setBackgroundColor:[UIColor greenColor]]; view.rightCalloutAccessoryView = button; //设置针头颜色 view.pinColor = MKPinAnnotationColorPurple; //设置允许弹出框 view.canShowCallout = YES; //设置掉下动画 view.animatesDrop = YES; return view; }
实现方法后,添加大头针效果为
PS:如果返回nil,则按系统默认方法显示,而不是不现实
5.自定义大头针2 - 修改大头针样式
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { NSString *identifier = @"test"; MKPinAnnotationView *view = (MKPinAnnotationView*)[_mapview dequeueReusableAnnotationViewWithIdentifier:identifier]; if (!view) { view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; view.canShowCallout = YES; } //覆盖掉默认的 view.annotation = annotation; //设置右边View为一个自定义Button UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; [button setTitle:@"我就试试" forState:UIControlStateNormal]; [button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside]; [button setBackgroundColor:[UIColor greenColor]]; view.rightCalloutAccessoryView = button; if (arc4random()%100 > 50) { view.image = [UIImage imageNamed:@"A"]; } else { view.image = [UIImage imageNamed:@"B"]; } return view; }
注意:不能使用animatesDrop,否则还是大头针
使用效果