最近項目中有需要應用到高德地圖的模塊,在參考別的app地圖相關模塊實現過程中,自己產生了一些想法。首先說明要實現的功能需求。類似支付寶app內的跑腿功能,在全市的所有商鋪,電梯廣告等任意地點發布任務,參與者要根據地圖上的標記接取任務后到達指定地點,完成任務,獲取報酬。
首先我想到的是共享單車app的找附近的車功能。
分析其實現原理應該是獲取用戶當前坐標,然后將坐標發送至服務器,服務器計算這個用戶周圍1公里范圍內空閑的單車,然后將坐標繪制在地圖上,最后導航引導用戶找到該車。但是共享單車數量龐大,一個城市或許會有幾百萬輛或者幾千萬輛,但是我們所做的體量比較小,或許有另外的方式實現此功能。
使用高德地圖SDK,具體集成方法和相關配置請參考官方文檔,這里不做介紹。首先我們需要1000個模擬數據 沈陽市的經緯度范圍是 東經122° 25′ --- 123° 48′, 北緯41°12′ --- 42° 17′
我們根據這個范圍隨機生成1萬個坐標,然后先在地圖上自定義視圖標記出來,但是不要顯示,這里先全部顯示看一下 ps:此圖手抖多打個0,是1萬個坐標點...
然后在當前地圖的中心點繪制一個半徑為3km的圓形, 在地圖移動的時候,這個圓心也隨之變化,這個時候遍歷這些數據,如果這個數據的坐標在圓的范圍內那么就顯示,反之就隱藏。
這樣做的好處是只需要從服務器拿取一次數據,數據量小的時候也很流暢,減輕了服務器的運算,但是弊端就是數據量龐大的時候會非常占用手機的內存,這也是我最開始沒有考慮到的一點,
為了穩定不建議這樣處理,除非數據量小的時候,我用iphone6測試 數據量達到500就開始卡頓,用iphoneX 數據量1000還可以接受,為了向下兼容不建議這種做法,還是老實向服務器請求數據比較好些。寫這個文章主要還是想多了解下高德的API,加深下理解,以后再使用高德SDK的時候更加熟練。如果哪位大神看到這篇文章有好的優化方案請賜教,十分感謝,如果感覺垃圾,求輕噴。下面就開始代碼部分
創建地圖視圖
//初始化地圖
_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
_mapView.userTrackingMode = MAUserTrackingModeFollow;
//如果您需要進入地圖就顯示定位小藍點,則需要下面兩行代碼
_mapView.showsUserLocation = YES;
_mapView.showsScale = NO;
_mapView.zoomLevel = 13;
_mapView.showTraffic = YES;
_mapView.showsCompass = NO;
_mapView.delegate = self;
//_mapView.desiredAccuracy = 100;
///把地圖添加至view
[self.view addSubview:_mapView];
//自定義定位小藍點
//初始化 MAUserLocationRepresentation 對象
MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
r.showsAccuracyRing = NO;///精度圈是否顯示,默認YES
r.showsHeadingIndicator = YES;///是否顯示方向指示 (MAUserTrackingModeFollowWithHeading模式開啟)。默認為YES
//r.fillColor = [UIColor redColor];///精度圈 填充顏色, 默認 kAccuracyCircleDefaultColor
r.image = [UIImage imageNamed:@"endPoint"];
//定位圖標, 與藍色原點互斥
// [_mapView updateUserLocationRepresentation:r];`
CLLocationCoordinate2D coor = _mapView.centerCoordinate;
//以初始地圖中心點為圓心 繪制半徑為3km米的圓
_circleView = [MACircle circleWithCenterCoordinate:coor radius:3000];
[self.mapView addOverlay:_circleView];
獲取模擬數據
//沈陽位于東經122゜25'---123゜48’,北緯41゜12’,---42゜17’,之間
self.annotations = [NSMutableArray array];
for (int i = 0; i < 1000; i ++) {
CGFloat ls = [self randomBetween:41 AndBigNum:42 AndPrecision:1000000];
CGFloat lw = [self randomBetween:123 AndBigNum:124 AndPrecision:1000000];
MAPointAnnotation *a1 = [[MAPointAnnotation alloc] init];
a1.coordinate = (CLLocationCoordinate2D){ls,lw};
a1.title = [NSString stringWithFormat:@"anno: %d", i];
a1.subtitle = [NSString stringWithFormat:@"自定義點標記內容: %d",I];
[self.annotations addObject:a1];
}
獲取指定范圍內坐標點的函數
- (float)randomBetween:(float)smallNum AndBigNum:(float)bigNum AndPrecision:(NSInteger)precision{
//求兩數之間的差值
float subtraction = bigNum - smallNum;
//取絕對值
subtraction = ABS(subtraction);
//乘以精度的位數
subtraction *= precision;
//在差值間隨機
float randomNumber = arc4random() % ((int) subtraction + 1);
//隨機的結果除以精度的位數
randomNumber /= precision;
//將隨機的值加到較小的值上
float result = MIN(smallNum, bigNum) + randomNumber;
//返回結果
return result;
}
#pragma mark - MAMapViewDelegate
//繪制區域圖形的相關屬性配置 可以是矩形 多邊形 圓形
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
if ([overlay isKindOfClass:[MACircle class]]) {
MACircleRenderer * polygonRenderer = [[MACircleRenderer alloc]initWithCircle:overlay];
polygonRenderer.lineWidth = 1.f;
// polygonRenderer.strokeColor = [UIColor yellowColor];
polygonRenderer.fillColor = [UIColor colorWithRed:0.73 green:0.73 blue:0.73 alpha:0.2];
return polygonRenderer;
}
return nil;
}
/*!
@brief 根據anntation生成對應的View
@param mapView 地圖View
@param annotation 指定的標注
@return 生成的標注View
*/
- (MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
//定位藍點 如果不在此判斷 自身的定位點樣式會被其他自定義的樣式修改
if ([annotation isKindOfClass:[MAUserLocation class]]) {
return nil;
}
if ([annotation isKindOfClass:[MAPointAnnotation class]]){
static NSString *reuseIndetifier = @"annotationReuseIndetifier";
MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
if (annotationView == nil){
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
}
annotationView.image = [UIImage imageNamed:@"qwuh"];
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//設置中心點偏移,使得標注底部中間點成為經緯度對應點
annotationView.centerOffset = CGPointMake(0, -18);
return annotationView;
}
return nil;
}
//點擊屏幕獲取經緯度 (手動獲取模擬數據使用)
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate{
NSLog(@"%f ---- %f",coordinate.latitude,coordinate.longitude);
//41.737987 ---- 123.422523
//41.765668 ---- 123.434932
//41.794761 ---- 123.409902
}
/**
* @brief 地圖區域改變過程中會調用此接口 since 4.6.0
* @param mapView 地圖View
*/
- (void)mapViewRegionChanged:(MAMapView *)mapView{
//移動地圖 根據新的中心點坐標 改變所繪制圖形的位置
[self.circleView setCircleWithCenterCoordinate:mapView.centerCoordinate radius:3000];
//遍歷所有的自定義坐標點
for (int i = 0; i < self.annotations.count; i ++) {
MAPointAnnotation *a1 = self.annotations[I];
CLLocationCoordinate2D loc1 = a1.coordinate;
// [self.mapView addAnnotation:a1];
if(MACircleContainsCoordinate(loc1, self.circleView.coordinate, 3000)) {
NSLog(@"在區域內 新增自定義坐標點");
[self.mapView addAnnotation:a1];
} else {
NSLog(@"不在區域內 移除自定義坐標點");
[self.mapView removeAnnotation:a1];
}
}
}
或許會用到的相關代理方法
/**
* @brief 地圖移動結束后調用此接口
* @param mapView 地圖view
* @param wasUserAction 標識是否是用戶動作
*/
- (void)mapView:(MAMapView *)mapView mapDidMoveByUser:(BOOL)wasUserAction{
if (wasUserAction) {
//當前地圖的中心點,改變該值時,地圖的比例尺級別不會發生變化
}
}
/**
* @brief 定位失敗后,會調用此函數
* @param mapView 地圖View
* @param error 錯誤號,參考CLError.h中定義的錯誤號
*/
- (void)mapView:(MAMapView *)mapView didFailToLocateUserWithError:(NSError *)error{
NSLog(@"定位失敗");
}
/**
* @brief 地圖初始化完成(在此之后,可以進行坐標計算)
* @param mapView 地圖View
*/
- (void)mapInitComplete:(MAMapView *)mapView{
// NSLog(@"當前經緯度%lf--%lf",mapView.userLocation.coordinate.latitude,mapView.userLocation.coordinate.longitude);
}
/*!
@brief 當mapView新添加annotation views時調用此接口
@param mapView 地圖View
@param views 新添加的annotation views
*/
- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views {
}
/*!
@brief 當選中一個annotation views時調用此接口
@param mapView 地圖View
@param views 選中的annotation views
*/
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
}
/*!
@brief 當取消選中一個annotation views時調用此接口
@param mapView 地圖View
@param views 取消選中的annotation views
*/
- (void)mapView:(MAMapView *)mapView didDeselectAnnotationView:(MAAnnotationView *)view {
}
/*!
@brief 標注view的accessory view(必須繼承自UIControl)被點擊時調用此接口
@param mapView 地圖View
@param annotationView callout所屬的標注view
@param control 對應的control
*/
- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
}
https://github.com/ReReReReReRe/GDMapDemo/tree/master/PaiPaiPai