iOS 版百度地圖 SDK 滿視口顯示一組圖釘,iossdk
iOS 版百度地圖 SDK 滿視口顯示一組圖釘
太陽火神的美麗人生 (http://blog.csdn.net/opengl_es)
本文遵循“署名-非商業用途-保持一致”創作公用協議
轉載請保留此句:太陽火神的美麗人生 - 本部落格專註於 敏捷開發及移動和物聯裝置研究:iOS、Android、Html5、Arduino、pcDuino,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。
參考以下兩個連結文章內容:
http://www.bkjia.com/IOSjc/817431.html
http://yang120jun.blog.51cto.com/3199830/1087548
但仍無法正確縮放,使用的百度地圖方法是:
/** *設定當前地圖的顯示範圍,採用直角座標系表示 *@param mapRect 要設定的地圖範圍,用直角座標系表示 *@param animate 是否採用動畫效果 */- (void)setVisibleMapRect:(BMKMapRect)mapRect animated:(BOOL)animate;
經反覆償試,發現有以下幾個問題:
1、按第一個地址處理完後,仍需按第二個地址中進行適應性轉換;
2、座標跨距很小時,載入地圖即轉換到很高的縮放層級,地圖不顯示內容,所以需要延時縮放,0.1 秒基本可以,0.08秒延遲基本看不出來有縮放過程;
基本情況如上,下面貼代碼:
[self performSelector:@selector(delayZoom) withObject:nil afterDelay:0.08];
- (void)delayZoom { BMKCoordinateRegion region = [self generateRegion:_annotationsArray]; // 適配當前地圖視窗顯示的 region region = [_mapView regionThatFits:region]; //百度地圖的座標範圍轉換成相對視圖的位置 CGRect fitRect = [_mapView convertRegion:region toRectToView:_mapView]; //將地圖視圖的位置轉換成地圖的位置 BMKMapRect fitMapRect = [_mapView convertRect:fitRect toMapRectFromView:_mapView]; //設定地圖可視範圍為資料所在的地圖位置 [_mapView setVisibleMapRect:fitMapRect animated:YES];}
為防止以上兩個連結內容在不久將來會人間蒸發,在下面作個記錄
----------------------------------------------------------
iOS根據座標資料點所在的座標地區來動態顯示到可視範圍
熱度5 評論 74
作者:gzl1163 TA關注 MP3/MP4,QQ登陸, 2014-06-24 12:06:55 瀏覽數15618次
在地圖上標註很多點之後,地圖的中心點可以設定,但是縮放層級用起來就有點囧了,
所以,就需要根據座標資料點所在的座標地區來動態計算,把所有點都剛好顯示到地圖的可視範圍內。
直接上代碼:
//清理座標資料的視圖和資料 [_bMapView removeAnnotations:_mapAnnotations]; [_mapAnnotations removeAllObjects]; [_carPointArray removeAllObjects]; //聲明解析時對座標資料的位置地區的篩選,包括經度和緯度的最小值和最大值 CLLocationDegrees minLat; CLLocationDegrees maxLat; CLLocationDegrees minLon; CLLocationDegrees maxLon; //解析資料 for (int i=0; i<rows.count; i++) { NSDictionary *row = [rows objectAtIndex:i]; 座標模型類 *item = [[座標模型類 alloc] initWithJson:row]; if (item.vehicleNo && [item.vehicleNo length]>0) { 標註模型類 *annotation = [[標註模型類 alloc] init]; annotation.coordinate = item.baiduCoordinate; annotation.item = item; [_mapAnnotations addObject:annotation]; [_bMapView addAnnotation:annotation]; [annotation release]; if (i==0) { //以第一個座標點做初始值 minLat = item.baiduCoordinate.latitude; maxLat = item.baiduCoordinate.latitude; minLon = item.baiduCoordinate.longitude; maxLon = item.baiduCoordinate.longitude; }else{ //對比篩選出最小緯度,最大緯度;最小經度,最大經度 minLat = MIN(minLat, item.baiduCoordinate.latitude); maxLat = MAX(maxLat, item.baiduCoordinate.latitude); minLon = MIN(minLon, item.baiduCoordinate.longitude); maxLon = MAX(maxLon, item.baiduCoordinate.longitude); } [_carPointArray addObject:item]; } [item release]; } //動態根據座標資料的地區,來確定地圖的顯示中心點和縮放層級 if (_carPointArray.count > 0) { //計算中心點 CLLocationCoordinate2D centCoor; centCoor.latitude = (CLLocationDegrees)((maxLat+minLat) * 0.5f); centCoor.longitude = (CLLocationDegrees)((maxLon+minLon) * 0.5f); BMKCoordinateSpan span; //計算地理位置的跨度 span.latitudeDelta = maxLat - minLat; span.longitudeDelta = maxLon - minLon; //得出資料的座標地區 BMKCoordinateRegion region = BMKCoordinateRegionMake(centCoor, span); //百度地圖的座標範圍轉換成相對視圖的位置 CGRect fitRect = [_bMapView convertRegion:region toRectToView:_bMapView]; //將地圖視圖的位置轉換成地圖的位置 BMKMapRect fitMapRect = [_bMapView convertRect:fitRect toMapRectFromView:_bMapView]; //設定地圖可視範圍為資料所在的地圖位置 [_bMapView setVisibleMapRect:fitMapRect animated:YES]; }
補充:
MKMapRect zoomRect = MKMapRectNull;for (id <MKAnnotation> annotation in mapView.annotations) { MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0); if (MKMapRectIsNull(zoomRect)) { zoomRect = pointRect; } else { zoomRect = MKMapRectUnion(zoomRect, pointRect); }}[mapView setVisibleMapRect:zoomRect animated:YES];
----------------------------------------------------------
根據指定經緯度建立地圖並且定位
//地圖邊框的實現
UIView *view =[[UIView alloc]initWithFrame:CGRectMake(10, 120, 300, 240)];
view.layer.cornerRadius =12;
view.backgroundColor=[UIColor whiteColor];
view.layer.masksToBounds=YES;
[self.view addSubview:view];
[view release];
//添加地圖
map = [[[MKMapView alloc] initWithFrame:view.bounds] autorelease];
map.showsUserLocation =YES;
map.mapType=MKMapTypeStandard;//地圖樣式
[view addSubview:map];
//將傳過來的經緯度轉換成doubel
double fourth = [_fourth doubleValue];
double fourth2 = [_fourth2 doubleValue];
//設定定為器 和指定經緯度
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(fourth,fourth2);
//縮放層級
float zoomLevel =0.02;
MKCoordinateRegion region=MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel,zoomLevel));
[map setRegion:[map regionThatFits:region]animated:YES];