地图location定位
2019獨角獸企業重金招聘Python工程師標準>>>
#import <UIKit/UIKit.h>
@interface MapViewController : UIViewController
@property(nonatomic,strong)NSString *addressName; // 用于接收地址字符串的屬性
@end
#import <MapKit/MapKit.h>
#import "LocationHandle.h"
@interface MapViewController ()<MKMapViewDelegate>
@property(nonatomic,strong)MKMapView *mapView;? // 地圖視圖
@end
@implementation MapViewController
-(MKMapView *)mapView
{
? ? if (!_mapView) {
? ? ? ? _mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
? ? ? ? _mapView.delegate = self;
? ? ? ? _mapView.zoomEnabled = YES;
? ? ? ? _mapView.scrollEnabled = YES;
? ? ? ? _mapView.pitchEnabled = YES;
? ? ? ? _mapView.rotateEnabled = YES;
?? ? ? ?
? ? ? ? /*
?? ? ? ? typedef NS_ENUM(NSUInteger, MKMapType) {
?? ? ? ? MKMapTypeStandard = 0,
?? ? ? ? MKMapTypeSatellite,
?? ? ? ? MKMapTypeHybrid,
?? ? ? ? MKMapTypeSatelliteFlyover NS_ENUM_AVAILABLE(10_11, 9_0),
?? ? ? ? MKMapTypeHybridFlyover NS_ENUM_AVAILABLE(10_11, 9_0),
?? ? ? ? }
?? ? ? ? */
?? ? ? ?
?? ? ? ?
? ? ? ? _mapView.mapType = MKMapTypeStandard;? // 綜合地圖
? ? ? ? //? [_mapView setUserTrackingMode:MKUserTrackingModeFollow]; // 設置用戶跟蹤模式
? ? ? ? // _mapView.showsUserLocation = YES; // 顯示用戶當前位置
?? ? ? ?
? ? }
? ? return _mapView;
}
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view.
? ? [self.view addSubview:self.mapView];
?? ?
? ? UIBarButtonItem *bar1 = [[UIBarButtonItem alloc] initWithTitle:@"省會位置" style:UIBarButtonItemStylePlain target:self action:@selector(locateProvince:)];
? ? UIBarButtonItem *bar2 = [[UIBarButtonItem alloc] initWithTitle:@"當前位置" style:UIBarButtonItemStylePlain target:self action:@selector(locateCurrentLocation:)];
?? ?
? ? self.navigationItem.rightBarButtonItems = @[bar1,bar2];
?? ?
?? ?
? ? // 注冊通知,監聽當前位置獲取
? ? [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCurrentLocationByNotification:) name:@"passLoc" object:nil];
}
-(void)viewWillAppear:(BOOL)animated
{
? ? [super viewWillAppear:animated];
? ? self.navigationItem.title = self.addressName;
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
#pragma mark - 自定義觸發方法
// 定位省會位置
-(void)locateProvince:(id)sender
{
? ? // 獲取字符串代表的經緯度
? ? CLGeocoder *geocoder = [[CLGeocoder alloc] init];
? ? [geocoder geocodeAddressString:self.addressName completionHandler:^(NSArray *placemarks, NSError *error) {
? ? ? ? CLPlacemark *place = [placemarks lastObject];
? ? ? ? CLLocation *loc = ? place.location;
?? ? ? ?
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? [self.mapView setRegion:MKCoordinateRegionMake(loc.coordinate, MKCoordinateSpanMake(0.1, 0.1)) animated:YES];
?? ? ? ? ? ?
?? ? ? ? ? ?
? ? ? ? ? ? /*
?? ? ? ? ? ? 添加覆蓋層方法與添加錨點方法相似,但是覆蓋層沒有定義好的視圖,必須實現mapView中設置覆蓋層視圖的回調方法;
?? ? ? ? ? ? */
? ? ? ? ? ? // 定義圓形覆蓋層
? ? ? ? ? ? MKCircle *circle = [MKCircle circleWithCenterCoordinate:loc.coordinate radius:100];
? ? ? ? ? ? // 添加圓形覆蓋層
? ? ? ? ? ? [self.mapView addOverlay:circle];
?? ? ? ? ? ?
? ? ? ? });
?? ? ? ?
? ? }];
}
// 定位當前位置
-(void)locateCurrentLocation:(id)sender
{
? ? [[LocationHandle sharedLocationControl] getCurrentLocation];
}
#pragma mark - 當前位置獲取
-(void)getCurrentLocationByNotification:(NSNotification *)notifi
{
? ? // 獲取當前位置
? ? CLLocation *curLoc = [notifi object];
?? ?
?? ?
? ? // 設置顯示區域
? ? MKCoordinateRegion region = MKCoordinateRegionMake(curLoc.coordinate, MKCoordinateSpanMake(0.1, 0.1));
? ? // 改變地圖的顯示范圍
? ? [self.mapView setRegion:region animated:YES];
?? ?
#if 1
? ? // 在當前位置添加一個錨點
? ? MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
? ? // 設置顯示位置
? ? point.coordinate = curLoc.coordinate;
? ? // 設置主標題
? ? point.title = [NSString stringWithFormat:@"緯度:%g,經度:%g",curLoc.coordinate.latitude,curLoc.coordinate.longitude];
?? ?
? ? // 反向地址解析,得到經緯度代表的地址字符串
? ? CLGeocoder *geocoder = [[CLGeocoder alloc] init];
? ? [geocoder reverseGeocodeLocation:curLoc completionHandler:^(NSArray *placemarks, NSError *error) {
? ? ? ? CLPlacemark *curPlace = [placemarks lastObject];
? ? ? ? NSString *address = curPlace.name;
?? ? ? ?
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? // 設置錨點副標題
? ? ? ? ? ? point.subtitle = address;
? ? ? ? ? ? // 將錨點添加到地圖上
? ? ? ? ? ? [self.mapView addAnnotation:point];
? ? ? ? });
? ? }];
?? ?
#else
?? ?
? ? /*
?? ? 添加覆蓋層方法與添加錨點方法相似,但是覆蓋層沒有定義好的視圖,必須實現mapView中設置覆蓋層視圖的回調方法;
?? ? */
? ? // 定義圓形覆蓋層
? ? MKCircle *circle = [MKCircle circleWithCenterCoordinate:curLoc.coordinate radius:600];
? ? // 添加圓形覆蓋層
? ? [self.mapView addOverlay:circle];
?? ?
#endif
?? ?
}
#pragma mark - MKMapViewDelegate
// 設置錨點視圖樣式,(在MKMapView對象調用addAnnotation方法時回調此方法)
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
#if 0
? ? static NSString *identifier = @"pin";
? ? // 使用大頭針錨點
? ? MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
? ? //
? ? if (pin == nil) {
? ? ? ? pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
? ? }
? ? // 設置大頭針顏色
? ? [pin setPinColor:MKPinAnnotationColorPurple];
? ? // 設置大頭針掉落的動畫效果
? ? pin.animatesDrop = YES;
? ? // 設置可以彈出氣泡
? ? pin.canShowCallout = YES;
? ? // 設置氣泡左選項
? ? UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
? ? [btn setFrame:CGRectMake(0, 0, 20, 20)];
? ? [btn addTarget:self action:@selector(btnDidhandle:) forControlEvents:UIControlEventTouchUpInside];
? ? pin.leftCalloutAccessoryView = btn;
? ? return pin;
#else
? ? static NSString *identifier = @"custom";
? ? MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
? ? if (annoView == nil) {
? ? ? ? annoView = [[MKAnnotationView alloc] init];
? ? }
? ? // 設置可以彈出氣泡
? ? annoView.canShowCallout = YES;
? ? // 設置氣泡右側附件視圖
? ? UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
? ? [btn setFrame:CGRectMake(0, 0, 20, 20)];
? ? [btn addTarget:self action:@selector(btnDidhandle:) forControlEvents:UIControlEventTouchUpInside];
? ? annoView.rightCalloutAccessoryView = btn;
? ? // 設置自定義的圖片
? ? annoView.image = [UIImage imageNamed:@"location"];
?? ?
?? ?
?? ?
? ? return annoView;
?? ?
#endif
}
// 設置覆蓋層樣式的回調方法
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
?? ?
? ? MKCircle *circle = (MKCircle *)overlay;
? ? // 設置圓形覆蓋層視圖
? ? MKCircleRenderer *circleView = [[MKCircleRenderer alloc] initWithCircle:circle];
? ? // 設置填充顏色
? ? circleView.fillColor = [UIColor redColor];
? ? // 設置描邊顏色
? ? circleView.strokeColor = [UIColor yellowColor];
?? ?
? ? return circleView;
}
-(void)btnDidhandle:(id)sender
{
? ? NSLog(@"盛大的撒旦撒旦撒旦是");
}
轉載于:https://my.oschina.net/zhaofengye/blog/632050
總結
以上是生活随笔為你收集整理的地图location定位的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 四种数据绩效指标管理(上)
- 下一篇: 今天踏上博客之旅,好饭不怕晚