iOS 使用百度地圖,仿滴滴打車的定位方法。拖動時時定位


這里的思路: 
(1)把圖片放到屏幕的中間,這樣在拖動的時候就不會跟隨着地圖移動了。 
(2)百度地圖提供了,View坐標和地理坐標轉換的方法。正式這個方法的存在,方便我們及時的獲取拖動后的,屏幕中間的圖片所在位置的經緯度。

這里寫圖片描述

當拖動地圖的時候,定位的圖片一直在屏幕的中央,當拖動停止的時候會顯示出具體的信息

#import "HouseTypeMapVC.h" @interface HouseTypeMapVC ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>{ BMKLocationService * _locService; } @property (nonatomic,strong) UIView * locationView; @property (nonatomic,strong) UIImageView * locImageView; @property (nonatomic,strong) UIView * messageView; @property (nonatomic,strong) UILabel * addressLabel; @property (nonatomic,strong) UIButton * sureButton; @property (nonatomic,strong) NSString * name; @property (nonatomic,assign) CLLocationCoordinate2D location2D; @property (nonatomic, strong)BMKGeoCodeSearch* searchAddress; @property (strong, nonatomic) IBOutlet BMKMapView *mapView; @property (nonatomic,strong)BMKUserLocation *userLocation; //定位功能 @end @implementation HouseTypeMapVC -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear: animated]; [_mapView viewWillAppear]; _mapView.delegate = self; // 此處記得不用的時候需要置nil,否則影響內存的釋放 _locService.delegate = self; } -(void)viewWillDisappear:(BOOL)animated{ [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用時,置nil _locService.delegate = nil; } - (void)viewDidLoad { [super viewDidLoad]; } - (void)initUI{ [self goBackBtn]; _mapView.mapType=BMKMapTypeStandard; [self initlocationService]; } #pragma mark --initlocationService--定位 -(void)initlocationService{ _locService = [[BMKLocationService alloc]init]; [_locService startUserLocationService]; _mapView.showsUserLocation = NO;//先關閉顯示的定位圖層 _mapView.userTrackingMode = BMKUserTrackingModeNone;//設置定位的狀態 _mapView.showsUserLocation = YES;//顯示定位圖層 _mapView.showMapScaleBar = YES;//顯示比例尺 _mapView.zoomLevel = 17;//地圖顯示的級別 _searchAddress = [[BMKGeoCodeSearch alloc]init]; _searchAddress.delegate = self; } //這里是創建中心顯示的圖片和顯示詳細地址的View - (void)createLocationSignImage{ //LocationView定位在當前位置,換算為屏幕的坐標,創建的定位的圖標 self.locationView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 28, 35)]; self.locImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 28, 35)]; self.locImageView.image = [UIImage imageNamed:@"myLocation"]; [self.locationView addSubview:self.locImageView]; //messageView 展示定位信息的View和Label和button self.messageView = [[UIView alloc]init]; self.messageView.backgroundColor = [UIColor whiteColor]; //把當前定位的經緯度換算為了View上的坐標 CGPoint point = [self.mapView convertCoordinate:_mapView.centerCoordinate toPointToView:_mapView]; //當解析出現錯誤的時候,會出現超出屏幕的情況,一種是大於了屏幕,一種是小於了屏幕 if(point.x > ScreenWidth || point.x < ScreenWidth/5){ point.x = _mapView.centerX; point.y = _mapView.centerY-64; } NSLog(@"Point------%f-----%f",point.x,point.y); //重新定位了LocationView self.locationView.center = point; [self.locationView setFrame:CGRectMake(point.x-14, point.y-18, 28, 35)]; //重新定位了messageView [self.messageView setFrame:CGRectMake(30, point.y-40-20, SCREEN_WIDTH-60, 40)]; //展示地址信息的label self.addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.messageView.frame.size.width - 80, 40)]; self.addressLabel.font = [UIFont systemFontOfSize:13.0f]; [self.messageView addSubview:self.addressLabel]; //把地址信息傳遞到上個界面的button self.sureButton = [[UIButton alloc]initWithFrame:CGRectMake(self.addressLabel.frame.origin.x + self.addressLabel.frame.size.width, 0,self.messageView.frame.size.width - self.addressLabel.frame.origin.x - self.addressLabel.frame.size.width, 40)]; [self.messageView addSubview:self.sureButton]; self.sureButton.backgroundColor = [UIColor colorWithHex:0x2ecb7d]; [self.sureButton setTitle:@"確定" forState:UIControlStateNormal]; self.sureButton.titleLabel.font = [UIFont systemFontOfSize:13.0f]; [self.sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self.sureButton addTarget:self action:@selector(sureButtonClick:) forControlEvents:UIControlEventTouchUpInside]; [self.mapView addSubview:self.messageView]; [self.mapView addSubview:self.locationView]; } /** *用戶位置更新后,會調用此函數 *@param userLocation 新的用戶位置 */ - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{ BMKCoordinateRegion region; region.center.latitude = userLocation.location.coordinate.latitude; region.center.longitude = userLocation.location.coordinate.longitude; region.span.latitudeDelta = 0; region.span.longitudeDelta = 0; NSLog(@"當前的坐標是:%f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude); [_mapView updateLocationData:userLocation]; [_locService stopUserLocationService];//取消定位 這個一定要寫,不然無法移動定位了 _mapView.centerCoordinate = userLocation.location.coordinate; NSLog(@" _mapView.centerCoordinate------%f-----%f", _mapView.centerCoordinate.latitude, _mapView.centerCoordinate.longitude); [self createLocationSignImage]; } //確定按鈕的點擊 - (void)sureButtonClick:(UIButton *)button{ if([self.chosseAddressDelegate respondsToSelector:@selector(chosseAddressBack:name:point:)]){ [self.chosseAddressDelegate chosseAddressBack:self.addressLabel.text name:self.name point:self.location2D]; } [self.navigationController popViewControllerAnimated:YES]; } - (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view{ NSLog(@"點擊了"); CLLocationCoordinate2D pt=(CLLocationCoordinate2D){0,0}; pt=(CLLocationCoordinate2D){mapView.region.center.latitude,mapView.region.center.longitude}; BMKReverseGeoCodeOption * option = [[BMKReverseGeoCodeOption alloc]init]; option.reverseGeoPoint = pt; BOOL flag=[_searchAddress reverseGeoCode:option]; if (flag) { // _mapView.showsUserLocation=NO;//不顯示自己的位置 } } //地圖被拖動的時候,需要時時的渲染界面,當渲染結束的時候我們就去定位然后獲取圖片對應的經緯度 - (void)mapView:(BMKMapView *)mapView onDrawMapFrame:(BMKMapStatus*)status{ NSLog(@"onDrawMapFrame"); } - (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{ NSLog(@"regionWillChangeAnimated"); } - (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{ NSLog(@"regionDidChangeAnimated"); CGPoint touchPoint = self.locationView.center; CLLocationCoordinate2D touchMapCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];//這里touchMapCoordinate就是該點的經緯度了 NSLog(@"touching %f,%f",touchMapCoordinate.latitude,touchMapCoordinate.longitude); BMKReverseGeoCodeOption * option = [[BMKReverseGeoCodeOption alloc]init]; option.reverseGeoPoint = touchMapCoordinate; BOOL flag=[_searchAddress reverseGeoCode:option]; if (flag) { // _mapView.showsUserLocation=NO;//不顯示自己的位置 } } //定位自己的位置 - (IBAction)locationButtonClick:(UIButton *)sender { [_locService startUserLocationService]; _mapView.showsUserLocation = NO;//先關閉顯示的定位圖層 _mapView.userTrackingMode = BMKUserTrackingModeNone;//設置定位的狀態 _mapView.showsUserLocation = YES;//顯示定位圖層 } //點擊地圖的空白區域 - (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate{ NSLog(@"onClickedMapBlank-latitude==%f,longitude==%f",coordinate.latitude,coordinate.longitude); } //點擊地圖中的背景有標記的區域 - (void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi *)mapPoi{ NSLog(@"點擊onClickedMapPoi---%@",mapPoi.text); CLLocationCoordinate2D coordinate = mapPoi.pt; //長按之前刪除所有標注 NSArray *arrayAnmation=[[NSArray alloc] initWithArray:_mapView.annotations]; [_mapView removeAnnotations:arrayAnmation]; //設置地圖標注 BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init]; annotation.coordinate = coordinate; annotation.title = mapPoi.text; [_mapView addAnnotation:annotation]; BMKReverseGeoCodeOption *re = [[BMKReverseGeoCodeOption alloc] init]; re.reverseGeoPoint = coordinate; [SVProgressHUD show]; [_searchAddress reverseGeoCode:re]; BOOL flag =[_searchAddress reverseGeoCode:re]; if (!flag){ NSLog(@"search failed!"); } } //根據經緯度返回點擊的位置的名稱 -(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{ [SVProgressHUD dismiss]; NSString * resultAddress = @""; NSString * houseName = @""; CLLocationCoordinate2D coor = result.location; if(result.poiList.count > 0){ BMKPoiInfo * info = result.poiList[0]; if([info.name rangeOfString:@"-"].location != NSNotFound){ houseName = [info.name componentsSeparatedByString:@"-"][0]; }else{ houseName = info.name; } resultAddress = [NSString stringWithFormat:@"%@%@",result.address,info.name]; }else{ resultAddress =result.address; } if(resultAddress.length == 0){ self.addressLabel.text = @"位置解析錯誤,請拖動重試!"; return; } self.addressLabel.text = resultAddress; self.location2D = coor; self.name = houseName; } //點擊一個大頭針 - (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{ NSLog(@"點擊didSelectAnnotationView-"); } /** *在地圖View將要啟動定位時,會調用此函數 *@param mapView 地圖View */ - (void)willStartLocatingUser { NSLog(@"start locate"); } /** *用戶方向更新后,會調用此函數 *@param userLocation 新的用戶位置 */ - (void)didUpdateUserHeading:(BMKUserLocation *)userLocation { [_mapView updateLocationData:userLocation]; NSLog(@"heading is %@",userLocation.heading); } /** *在地圖View停止定位后,會調用此函數 *@param mapView 地圖View */ - (void)didStopLocatingUser { NSLog(@"stop locate"); } /** *定位失敗后,會調用此函數 *@param mapView 地圖View *@param error 錯誤號,參考CLError.h中定義的錯誤號 */ - (void)didFailToLocateUserWithError:(NSError *)error { NSLog(@"location error"); } - (void)dealloc { if (_mapView) { _mapView = nil; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM