地图定位

#import "WQApplyViewController.h"

#import "JZCommonListViewController.h"

#import "MapKit/MapKit.h"

#import "JZWQDataView.h"

#import "CLLocation+YCLocation.h"

#import "BasicMapAnnotation.h"

#import "OutWork.h"

#import "JZUserLocation.h"

 

#define LATITUDE -0.003015

#define LONGITUDE 0.004875

#define WHScale 260

@interface WQApplyViewController ()<WQDataViewDelegate,MKMapViewDelegate,TextLableInputViewDelegate,CLLocationManagerDelegate>

{

    TextLableInputView *sjTLIV;

    TextLableInputView *syTLIV;

    TextLableInputView *dwTLIV;

    TextLableInputView *ddTLIV;

    TextLableInputView *yyTLIV;

    UIButton *ksBT;

    TextLableInputView *rqTLIV;

    UILabel *kssjLB;

    UILabel *yssjLB;

    //计时器

    NSTimer *timer;

    

    //是否开始外勤

    BOOL isKS;

    //路径

    MKMapRect _routeRect;

    CLLocation* _currentLocation;

    CLLocationManager *locationManager;

    

    BOOL isFirst;

    UINavigationBar *NBar;

    //用户位置、起点、终点

    BasicMapAnnotation *  annotationKS;

    BasicMapAnnotation *  annotationJS;

    BasicMapAnnotation *  annotationUser;

    JZUserLocation *userLocation;

    //外勤日志

    NSDictionary *_endWorkDic;

    //时间

    NSString *sjstr;

    //是否定位成功

    BOOL locationscceed;

    //手动定位

    BOOL isHandleLocation;

}

@property(nonatomic,strong)NSDictionary *dataDic;

@property(nonatomic,strong)NSMutableArray *mAr;

 

@property (nonatomic, retain) MKMapView* mapView;

@property (nonatomic, retain) MKPolyline* routeLine;

@property (nonatomic, retain) MKPolylineView* routeLineView;

@property (nonatomic, retain) UIView *lineView;

@property (strong, nonatomic) UIButton *backbtn;

@property (strong, nonatomic) UIButton *listbtn;

@property (strong, nonatomic) UIButton *locationBT;

@property (strong, nonatomic) UILabel *lineLB;

@property (nonatomic, retain) UIView *fview;

@end

 

@implementation WQApplyViewController

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    //初始化视图

    [self initsubViews];

     self.mapView.delegate = self;

    

    //    /*

    //     * 测试

    //     */

    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(self.mapView.centerCoordinate.latitude, self.mapView.centerCoordinate.longitude);

    MKCoordinateSpan theSpan;

    //地图的范围 越小越精确

    theSpan.latitudeDelta = 0.002;

    theSpan.longitudeDelta = 0.002;

    MKCoordinateRegion theRegion;

    theRegion.center = coordinate;

    theRegion.span = theSpan;

    [self.mapView setRegion:theRegion animated:YES];

 

    if([CLLocationManager locationServicesEnabled]) {

        //定位初始化

        locationManager=[[CLLocationManager alloc] init];

        locationManager.delegate=self;

        locationManager.desiredAccuracy=kCLLocationAccuracyBest;

        locationManager.distanceFilter=10;

//        [locationManager startUpdatingLocation];//开启定位

    }else {

        //提示用户无法进行定位操作

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"定位不成功 ,请确认开启定位" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

        [alertView show];

    }

    //ios8以后

    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

        [locationManager requestWhenInUseAuthorization];

    }

    // 开始定位

    [locationManager startUpdatingLocation];

    [locationManager startUpdatingHeading];

    //未定位成功

    locationscceed = NO;

    //初始化自动定位

    isHandleLocation = NO;

    

    //设置地图类型

    _mapView.mapType=MKMapTypeStandard;

      self.mapView.showsUserLocation = NO;

}

 

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    if ([[UIDevice currentDevice].systemVersion intValue]>= 7.0) {

        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    }

    NBar.hidden = NO;

    self.navigationController.navigationBarHidden = YES;

    if (self.isEnd) {

        [self initDataView];

    }

}

- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    self.navigationController.navigationBarHidden = NO;

    NBar.hidden = YES;

}

- (void)initsubViews

{

    UIScrollView *scrollview = self.scrollView;

    //去掉view的手势

    [scrollview removeGestureRecognizer:viewtap];

    [self.scrollView removeFromSuperview];

    

    self.fview = [[[NSBundle mainBundle]loadNibNamed:@"WQApplicationViewController" owner:nil options:nil] firstObject];

    [self.keyLayoutView addSubview:self.fview];

    

    

    NBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];

    [NBar setBarTintColor:[UIColor colorWithOrange]];

    [self.navigationController.view addSubview:NBar];

    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

    

    self.scrollView = scrollview;

    //重新初始化rect

    self.fview.frame = CGRectMake(self.fview.frame.origin.x, self.fview.frame.origin.y, self.keyLayoutView.frame.size.width, self.keyLayoutView.frame.size.height);

    self.fview.backgroundColor = [UIColor whiteColor];

    [self.fview addSubview:self.scrollView];

    

    //地图

    self.mapView = [(MKMapView *)self.fview viewWithTag:10];

    MKMapView *mapview = self.mapView;

    mapview.frame = CGRectMake(mapview.frame.origin.x, mapview.frame.origin.y, mapview.frame.size.width, ([UIScreen mainScreen].bounds.size.height)-WHScale);

    [self.mapView removeFromSuperview];

    

    self.mapView = mapview;

    [self.scrollView addSubview:self.mapView];

    self.mapView.delegate = self;

//    self.mapView.showsCompass = NO;

    self.mapView.rotateEnabled = NO;

    //返回按钮

    self.backbtn = (UIButton *)[self.fview viewWithTag:11];

    UIButton *btn = self.backbtn;

    [self.backbtn removeFromSuperview];

    self.backbtn = btn;

    [self.keyLayoutView addSubview:self.backbtn];

    [self.backbtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];

    

    //列表按钮

    self.listbtn = (UIButton *)[self.fview viewWithTag:12];

    UIButton *listbtn = self.listbtn;

    [self.listbtn removeFromSuperview];

    listbtn.frame = CGRectMake(ScreenWidth-listbtn.frame.size.width, listbtn.frame.origin.y, listbtn.frame.size.width, listbtn.frame.size.height);

    self.listbtn = listbtn;

    [self.keyLayoutView addSubview:self.listbtn];

    [self.listbtn addTarget:self action:@selector(listBt:) forControlEvents:UIControlEventTouchUpInside];

    

    isKS = NO;

    isFirst = YES;

    NBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];

    [NBar setBarTintColor:[UIColor colorWithOrange]];

    [self.navigationController.view addSubview:NBar];

    

    [self.backbtn setImage:[UIImage imageNamed:@"ic_back"] forState:UIControlStateNormal];

    [self.backbtn setImage:[UIImage imageNamed:@"ic_back_press"] forState:UIControlStateHighlighted];

    

    CGFloat y = 220;

    CGFloat h = 40;

    sjTLIV = [[TextLableInputView alloc]initWithFrame:CGRectMake(0, y, WidthText, h) Delegate:self title:@""];

    sjTLIV.textView.editable = NO;

    UILabel* kssjtsLB = [UILabel labelNameWithText:@"开始时间" Frame:CGRectMake(10,5,70,30)];

    [sjTLIV addSubview:kssjtsLB];

    kssjLB = [UILabel labelNameWithText:@"" Frame:CGRectMake(102,5,70,30)];

    kssjLB.textColor = [UIColor colorWithtext];

    [sjTLIV addSubview:kssjLB];

    UILabel* yssjtsLB = [UILabel labelNameWithText:@"用时" Frame:CGRectMake(ScreenWidth-145,5,50,30)];

    [sjTLIV addSubview:yssjtsLB];

    yssjLB = [UILabel labelNameWithText:@"" Frame:CGRectMake(ScreenWidth-95,5,100,30)];

    yssjLB.textColor = [UIColor colorWithtext];

    [sjTLIV addSubview:yssjLB];

    [self.scrollView addSubview:sjTLIV];

    

    y += h;

    

    rqTLIV = [[TextLableInputView alloc]initWithFrame:CGRectMake(0, y, WidthText, h) Delegate:self title:@"外勤日期"];

    [rqTLIV addTextInputString:nil TextColor:nil Editable:NO];

    [self.scrollView addSubview:rqTLIV];

    

    y += h;

    dwTLIV = [[TextLableInputView alloc]initWithFrame:CGRectMake(0, y, WidthText, h) Delegate:self title:@"单位"];

    [dwTLIV addTextInputString:nil TextColor:nil Editable:NO];

    [self.scrollView addSubview:dwTLIV];

    y += h;

    ddTLIV = [[TextLableInputView alloc]initWithFrame:CGRectMake(0, y, WidthText, h) Delegate:self title:@"地点"];

    [ddTLIV addTextInputString:nil TextColor:nil Editable:NO];

    [self.scrollView addSubview:ddTLIV];

    y += h;

    syTLIV = [[TextLableInputView alloc]initWithFrame:CGRectMake(0, y, WidthText, h) Delegate:self title:@"事由"];

    [syTLIV addTextInputString:nil TextColor:nil Editable:NO];

    syTLIV.lineView.hidden = YES;

    [self.scrollView addSubview:syTLIV];

    

    y += h;

    //底部视图

    UIView *buttomView = (UIView *)[self.fview viewWithTag:13];

    buttomView.frame = CGRectMake(buttomView.frame.origin.x, y, buttomView.frame.size.width, 40);

    UIView *view = buttomView;

    [buttomView removeFromSuperview];

    buttomView = view;

    [self.scrollView addSubview:buttomView];

    

    //定位

    self.locationBT = [[UIButton alloc]initWithFrame:CGRectMake(10, self.mapView.frame.size.height-h, h, h)];

    [self.locationBT setImage:[UIImage imageNamed:@"location_press"] forState:UIControlStateHighlighted];

    [self.locationBT setImage:[UIImage imageNamed:@"location_normal"] forState:UIControlStateNormal];

    [self.mapView addSubview:self.locationBT];

    [self.locationBT addTarget:self action:@selector(clicklocationUser:) forControlEvents:UIControlEventTouchUpInside];

    

    //开始或结束

    ksBT = (UIButton *)[buttomView viewWithTag:14];

    [ksBT addTarget:self action:@selector(ksjsBt:) forControlEvents:UIControlEventTouchUpInside];

    self.tliVAy = @[self.mapView,rqTLIV,sjTLIV,dwTLIV,ddTLIV,syTLIV,buttomView];

    self.scrollView.scrollEnabled = NO;

}

- (void)layoutScrollSubview

{

    CGFloat y = 0;

    int i = 0;

    for (UIView *tliv in self.tliVAy)

    {

        i++;

        if(!tliv.hidden)

        {

            tliv.frame = CGRectMake(tliv.frame.origin.x, y, self.scrollView.frame.size.width, tliv.frame.size.height);

            if (dealerView && i == 1) {

                y += dealerView.frame.size.height + GapText;

            }

            else

            {

                y += tliv.frame.size.height ;

                

            }

        }

    }

    y += GapText;

    if (self.fujianView)

    {

        self.fujianView.frame = CGRectMake(Gapframe, y, self.scrollView.frame.size.width-2*Gapframe, self.fujianView.frame.size.height);

        y += self.fujianView.frame.size.height + GapText;

    }

    

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, y);

    

}

- (void)viewDidLayoutSubviews

{

    [super viewDidLayoutSubviews];

    

}

 

#pragma mark - CustomerMethod

- (IBAction)locationUser:(id)sender {

   [self locateCurrentUser];

}

#pragma mark - 定位当前位置

- (IBAction)clicklocationUser:(id)sender {

    //开启手动定位

    isHandleLocation = YES;

    [self locateCurrentUser];

}

- (void)locateCurrentUser {

    //未定位成功

    locationscceed = NO;

    //先停止之前的定位

    [locationManager stopUpdatingLocation];

    

    [self.locationBT setTitle:@"" forState:UIControlStateNormal];

    // 开始定位

    [locationManager startUpdatingLocation];

    [locationManager startUpdatingHeading];

}

- (void)initDataView

{

    [timer invalidate];

    dwTLIV.textView.text = @"";

    ddTLIV.textView.text = @"";

    syTLIV.textView.text = @"";

    rqTLIV.textView.text = @"";

    [ksBT setTitle:@"开始" forState:UIControlStateNormal];

    isKS = NO;

    kssjLB.text = @"00:00:00";

    yssjLB.text = @"00:00:00";

 

    self.dataDic = nil;

}

 

- (IBAction)back:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];

}

 

- (IBAction)ksjsBt:(id)sender {

    if (isKS)

    {

        JZWQDataView *w = [[JZWQDataView alloc]initWithFrame:CGRectMake(10, 0, ScreenWidth-20, 100) Delegate:self IsStart:isKS];

        w.billID = self.billID;

        w.delegate = self;

        w.userLocation = [userLocation locationBaiduFromMars];

        [w showPopoverOnCenterWithDisRemove:true];

    }

    else

    {

        JZWQDataView *w = [[JZWQDataView alloc]initWithFrame:CGRectZero Delegate:self IsStart:isKS];

        w.billID = self.billID;

        w.delegate = self;

        w.userLocation = [userLocation locationBaiduFromMars];

        [w showPopoverOnCenterWithDisRemove:true];

    }

}

 

#warning test...

- (void)updateDataSubmit

{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        @autoreleasepool

        {

            dispatch_async(dispatch_get_main_queue(), ^{

                [MBProgressHUD showHUDAddedTo:self.view.window animated:YES];

            });

            

            [self submitEndOutWork];

            

            dispatch_async(dispatch_get_main_queue(), ^{

                [MBProgressHUD hideAllHUDsForView:self.view.window animated:YES];

            });

        }

    });

    

}

 - (void)clickUserLocation:(id)sender

{

    if(!isKS)

    {

        //self.mapView.userLocation.title = @"当前位置";

        return;

    }

    else

    {

        //self.mapView.userLocation.title = @"点击签到";

    }

    //    MKAnnotationView * annotationView = [[MKAnnotationView alloc] initWithAnnotation:self.mapView.userLocation reuseIdentifier:nil];

    //    [self.mapView bringSubviewToFront:annotationView];

    

    CLLocation *lo = userLocation;

    if (lo)

    {

        

        

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            @autoreleasepool

            {

                dispatch_async(dispatch_get_main_queue(), ^{

                    [MBProgressHUD showHUDAddedTo:self.view.window animated:YES];

                });

                

                if ([[self submitLocation] isEqualToString:@"Success"])

                {

                    dispatch_async(dispatch_get_main_queue(), ^{

                        

                        if (self.mAr == nil)

                        {

                            self.mAr = [[NSMutableArray alloc]init];

                        }

                        [self.mAr addObject:lo];

                        [self configureRoutes:self.mAr];

                        

                        [UIAlertView showAlertViewToDiss:@"签到成功"];

                    });

                }

                else

                {

                    dispatch_async(dispatch_get_main_queue(), ^{

                        [UIAlertView showAlertView:@"提交失败" Message:[self submitLocation]];

                    });

                }

                dispatch_async(dispatch_get_main_queue(), ^{

                    [MBProgressHUD hideAllHUDsForView:self.view.window animated:YES];

                });

            }

        });

    }

    else

    {

        [UIAlertView showAlertViewToDiss:@"获取位置失败"];

    }

    

}

#pragma mark CLLocationManagerDelegate<br>/**<br>* 获取经纬度<br>*/

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    

    CLLocation *lo = [locations lastObject];

    if  (lo.coordinate.latitude == 0.0f || lo.coordinate.longitude == 0.0f)

    {

        //定位失败

        userLocation = nil;

        return;

    }

    isFirst = NO;

    

    //CLLocation转换坐标成mapview

    userLocation = [[JZUserLocation alloc]initWithLatitude:lo.coordinate.latitude+LATITUDE longitude:lo.coordinate.longitude+LONGITUDE];

 

    self.mapView.centerCoordinate = userLocation.coordinate;

    

 

    NSLog(@"%f---%f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);

    

    //重新跟新用户位置

    if (annotationUser) {

        [self.mapView removeAnnotation:annotationUser];

    }

    annotationUser = [[BasicMapAnnotation alloc] initWithLatitude:userLocation.coordinate.latitude andLongitude:userLocation.coordinate.longitude];

    annotationUser.image = @"Map_userLocation";

    [self.mapView addAnnotation:annotationUser];

    

    //定位成功

    locationscceed = YES;

    //重新初始化自动定位

    isHandleLocation = NO;

    //判断开始还是结束外勤

    if (self.isStart) {

        if ([self.WQdelegate respondsToSelector:@selector(giveStartUserLocation:)]) {

            [self.WQdelegate giveStartUserLocation:[userLocation locationBaiduFromMars]];

        }

    }else{

        if ([self.WQdelegate respondsToSelector:@selector(giveEndUserLocation:)]) {

            [self.WQdelegate giveEndUserLocation:[userLocation locationBaiduFromMars]];

        }

    }

}

/**

 *定位失败,回调此方法

 */

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

    //如果定位失败

    userLocation = nil;

    if (isHandleLocation) {

         [UIAlertView showAlertView:@"定位失败,无法获取位置信息!"];

    }

    if ([error code]==kCLErrorDenied) {

        //        NSLog(@"访问被拒绝");

        //[UIAlertView showAlertView:@"定位失败,请打开定位权限!"];

    }

    if ([error code]==kCLErrorLocationUnknown) {

        //        NSLog(@"无法获取位置信息");

        //[UIAlertView showAlertView:@"定位失败,无法获取位置信息,请尝试放大地图!"];

    }

    if ([error code]== kCLErrorNetwork) {

        //        NSLog(@"无法获取Error Domain=kCLErrorDomain Code=0 "(null)"网络信息");

        //[UIAlertView showAlertView:@"定位失败,请尝试重新打开WiFi开关!"];

    }

    //重新初始化自动定位

    isHandleLocation = NO;

    //判断开始还是结束外勤

    if (!locationscceed) {

        if (self.isStart) {

            if ([self.WQdelegate respondsToSelector:@selector(giveStartUserLocation:)]) {

                [self.WQdelegate giveStartUserLocation:[userLocation locationBaiduFromMars]];

                return;

            }

        }else{

            if ([self.WQdelegate respondsToSelector:@selector(giveEndUserLocation:)]) {

                [self.WQdelegate giveEndUserLocation:[userLocation locationBaiduFromMars]];

                return;

            }

        }

    }

}

#pragma mark - WQDataViewDelegate

//开始外勤

#pragma mark

#pragma mark Map View

 

- (void)configureRoutes:(NSArray*)location

{

    if (location.count == 0)

    {

        return;

    }

 

    NSMutableArray *ar = [[NSMutableArray alloc]initWithArray:location];

 

    [_mapView removeAnnotation:annotationKS];

    [_mapView removeAnnotation:annotationJS];

    

    if (ar.count > 0)

    {

        CLLocation *location1 = [ar objectAtIndex:0];

        annotationKS = [[BasicMapAnnotation alloc] initWithLatitude:location1.coordinate.latitude andLongitude:location1.coordinate.longitude];

        annotationKS.image = @"Map_st";

        [_mapView   addAnnotation:annotationKS];

        

    }

    if (ar.count >1)

    {

        CLLocation *location2 = [ar lastObject];

        annotationJS = [[BasicMapAnnotation alloc] initWithLatitude:location2.coordinate.latitude andLongitude:location2.coordinate.longitude];

        annotationJS.image = @"Map_en";

        [_mapView   addAnnotation:annotationJS];

    }

    

    //[self.mapView bringSubviewToFront:annotationView];

    

    

    MKMapPoint northEastPoint = MKMapPointMake(0.f, 0.f);

    MKMapPoint southWestPoint = MKMapPointMake(0.f, 0.f);

    

    

    MKMapPoint pointArray[ar.count];

    

    for(int idx = 0; idx < ar.count; idx++)

    {

        CLLocation *location = [ar objectAtIndex:idx];

        CLLocationDegrees latitude  = location.coordinate.latitude;

        CLLocationDegrees longitude = location.coordinate.longitude;

        

        

        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);

        MKMapPoint point = MKMapPointForCoordinate(coordinate);

        //        float zoomLevel = 0.02;

        //        MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(zoomLevel, zoomLevel));

        //        [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

        

        if (idx == 0)

        {

            northEastPoint = point;

            southWestPoint = point;

        }

        else

        {

            if (point.x > northEastPoint.x)

                northEastPoint.x = point.x;

            if(point.y > northEastPoint.y)

                northEastPoint.y = point.y;

            if (point.x < southWestPoint.x)

                southWestPoint.x = point.x;

            if (point.y < southWestPoint.y)

                southWestPoint.y = point.y;

        }

        

        pointArray[idx] = point;

    }

    

    if (self.routeLine)

    {

        [self.mapView removeOverlay:self.routeLine];

    }

    

    self.routeLine = [MKPolyline polylineWithPoints:pointArray count:ar.count];

    

    // add the overlay to the map

    if (nil != self.routeLine)

    {

        [self.mapView addOverlay:self.routeLine];

    }

    

}

 

 

#pragma mark

#pragma mark MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView didAddOverlayViews:(NSArray *)overlayViews

{

    //    NSLog(@"%@ ----- %@", self, NSStringFromSelector(_cmd));

    //    NSLog(@"overlayViews: %@", overlayViews);

}

 

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay

{

    //    NSLog(@"%@ ----- %@", self, NSStringFromSelector(_cmd));

    

    MKOverlayView* overlayView = nil;

    

    if(overlay == self.routeLine)

    {

        //if we have not yet created an overlay view for this overlay, create it now.

        if (self.routeLineView)

        {

            [self.routeLineView removeFromSuperview];

        }

        

        self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];

        self.routeLineView.fillColor = [UIColor redColor];

        self.routeLineView.strokeColor = [UIColor redColor];

        self.routeLineView.lineWidth = 10;

        

        overlayView = self.routeLineView;

    }

    

    return overlayView;

}

 

 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

{

    static NSString *placemarkIdentifier = @"my annotation identifier";

    if ([annotation isKindOfClass:[MKUserLocation class]])

    {

        MKAnnotationView *annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];

        if (annotationView == nil)

        {

            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];

            annotationView.image = [UIImage imageNamed:@"touming"];

            annotationView.canShowCallout = YES;

        }

        else

            annotationView.annotation = annotation;

        return annotationView;

        

    }

    else

    {

        MKAnnotationView *annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];

        BasicMapAnnotation *an = nil;

        if (annotationView == nil) {

            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];

            an = (BasicMapAnnotation*)annotation;

            if ([an.image isEqualToString:@"Map_userLocation"]) {

                annotationView.tag = 100;

                annotationView.canShowCallout = YES;

                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickUserLocation:)];

                [annotationView addGestureRecognizer:tap];

                annotationView.frame = CGRectMake(annotationView.frame.origin.x, annotationView.frame.origin.y, annotationView.frame.origin.x+20, annotationView.frame.size.height+20);

            }

        }else

            an = (BasicMapAnnotation*)annotation;

        annotationView.image = [UIImage imageNamed:an.image];

        

        return annotationView;

    }

}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views

{

    //    MKAnnotationView * annotationView = (MKAnnotationView *)[self.mapView viewWithTag:101];

    //    if (annotationView) {

    //        NSArray *arr = @[annotationView];

    //        [annotationView removeFromSuperview];

    //        [_mapView addSubview:arr[0]];

    //    }

    MKAnnotationView * annotationView = (MKAnnotationView *)[self.mapView viewWithTag:100];

    if (annotationView) {

        NSArray *arr = @[annotationView];

        [annotationView removeFromSuperview];

        [_mapView addSubview:arr[0]];

    }

    

}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view

{

    

}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view

{

    

}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

{

    

}

//用户坐标更新之后调用

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

    //    if (isFirst)

    //    {

    //        isFirst = NO;

    //        [UIActivityIndicatorView hideIndicatorWithView:self.view];

    //

    //    if  (userLocation.coordinate.latitude == 0.0f || userLocation.coordinate.longitude == 0.0f)

    //        return;

    //        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);

    //

    //

    //        MKCoordinateSpan theSpan;

    //        //地图的范围 越小越精确

    //        theSpan.latitudeDelta = 0.02;

    //        theSpan.longitudeDelta = 0.02;

    //        MKCoordinateRegion theRegion;

    //        theRegion.center = coordinate;

    //        theRegion.span = theSpan;

    //        [self.mapView setRegion:theRegion animated:YES];

    //

    ////    }

    

}

- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error

{

    

    //    NSLog(@"%@",error);

    //

    ////    [UIAlertView showAlertView:[NSString stringWithFormat:@"%@",error]];

    //    [UIActivityIndicatorView hideIndicatorWithView:self.view];

    //    if (isFirst) {

    //        [UIAlertView showAlertView:@"定位失败,请尝试重新打开WiFi开关!"];

    //    }

}

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView

{

    

}

@end

 

posted @ 2015-12-22 14:54  天生的普通人  阅读(449)  评论(0编辑  收藏  举报