序言
在我們生活周圍的應(yīng)用中經(jīng)诚盥辏看到這樣的現(xiàn)象就是有一個固定的大頭針在地圖中心貌笨,然后移動地圖的時候弱判,地址會發(fā)生變化,例如我們平常用的應(yīng)用小黃車ofo锥惋,膜拜昌腰,小藍(lán)單車等,都是在地圖的中心固定一個大頭針膀跌,當(dāng)移動地圖的時候就刷新周邊的數(shù)據(jù)遭商。
思路
這其實(shí)不是一個真正的大頭針,其實(shí)是一個自定義的視圖捅伤。一個圖片添加到地圖中心劫流,然后在mapView拖動地圖的代理方法中,去翻解析根據(jù)定位坐標(biāo),進(jìn)行周邊搜索祠汇。
示例代碼
1、添加地圖
self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(20, 64 + 100, self.view.frame.size.width - 40, self.view.frame.size.height - 64 - 150)];
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mapView.delegate = self;
self.mapView.mapType = MAMapTypeStandard;
// 禁止旋轉(zhuǎn)
self.mapView.rotateEnabled = NO;
// 禁止立體旋轉(zhuǎn)
self.mapView.rotateCameraEnabled = NO;
self.mapView.showsBuildings = NO;
self.mapView.showsCompass = YES;
self.mapView.showsUserLocation = YES;
self.mapView.userTrackingMode = MAUserTrackingModeFollow;
[self.view addSubview:self.mapView];
// 初始化搜索
self.searchAPI = [[AMapSearchAPI alloc] init];
self.searchAPI.delegate = self;
#pragma mark--創(chuàng)建假的大頭針
- (void)creatPinView {
self.pinView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];
self.pinView.image = [UIImage imageNamed:@"mapimage"];
self.pinView.center = CGPointMake((self.view.frame.size.width - 40) * 0.5, (self.view.frame.size.height - 64 - 150) * 0.5);
self.pinView.hidden = NO;
[self.mapView addSubview:self.pinView];
}
2、創(chuàng)建地址顯示標(biāo)簽
#pragma mark--creatPostionLabel
- (void)creatPostionLabel {
self.positonLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 64 + 20, self.view.frame.size.width - 40, 60)];
self.positonLabel.font = [UIFont systemFontOfSize:15];
self.positonLabel.textColor = [UIColor cyanColor];
self.positonLabel.backgroundColor = [UIColor grayColor];
self.positonLabel.numberOfLines = 2;
[self.view addSubview:self.positonLabel];
}
3浴捆、進(jìn)行單次逆地理定位查出當(dāng)前位置
-(void)configLocationManager {
self.locationManager = [[AMapLocationManager alloc] init];
[self.locationManager setDelegate:self];
//設(shè)置期望定位精度
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//設(shè)置不允許系統(tǒng)暫停定位
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
//設(shè)置允許在后臺定位
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
//設(shè)置定位超時時間
[self.locationManager setLocationTimeout:DefaultLocationTimeout];
//設(shè)置逆地理超時時間
[self.locationManager setReGeocodeTimeout:DefaultReGeocodeTimeout];
//設(shè)置開啟虛擬定位風(fēng)險監(jiān)測弟塞,可以根據(jù)需要開啟
[self.locationManager setDetectRiskOfFakeLocation:NO];
//進(jìn)行單次帶逆地理定位請求
[self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
[self.mapView setZoomLevel:17.5];
}
4、顯示當(dāng)前的位置
#pragma mark - Initialization
- (void)initCompleteBlock {
__weak ShowPostionViewController *weakSelf = self;
self.completionBlock = ^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
if (error != nil && error.code == AMapLocationErrorLocateFailed) {
//定位錯誤:此時location和regeocode沒有返回值我抠,不進(jìn)行annotation的添加
// 重新進(jìn)行,單次定位
[weakSelf.locationManager requestLocationWithReGeocode:YES completionBlock:weakSelf.completionBlock];
NSLog(@"定位錯誤:{%ld - %@};", (long)error.code, error.userInfo);
return;
} else if (error != nil
&& (error.code == AMapLocationErrorReGeocodeFailed
|| error.code == AMapLocationErrorTimeOut
|| error.code == AMapLocationErrorCannotFindHost
|| error.code == AMapLocationErrorBadURL
|| error.code == AMapLocationErrorNotConnectedToInternet
|| error.code == AMapLocationErrorCannotConnectToHost))
{
//逆地理錯誤:在帶逆地理的單次定位中苇本,逆地理過程可能發(fā)生錯誤,此時location有返回值菜拓,regeocode無返回值瓣窄,進(jìn)行annotation的添加
NSLog(@"逆地理錯誤:{%ld - %@};", (long)error.code, error.userInfo);
// 重新進(jìn)行,單次定位
[weakSelf.locationManager requestLocationWithReGeocode:YES completionBlock:weakSelf.completionBlock];
} else if (error != nil && error.code == AMapLocationErrorRiskOfFakeLocation) {
//存在虛擬定位的風(fēng)險:此時location和regeocode沒有返回值,不進(jìn)行annotation的添加
NSLog(@"存在虛擬定位的風(fēng)險:{%ld - %@};", (long)error.code, error.userInfo);
return;
} else {
//沒有錯誤:location有返回值纳鼎,regeocode是否有返回值取決于是否進(jìn)行逆地理操作康栈,進(jìn)行annotation的添加
}
//有無逆地理信息,annotationView的標(biāo)題顯示的字段不一樣
if (regeocode) {
weakSelf.positonLabel.text = [NSString stringWithFormat:@"%@", regeocode.formattedAddress];
weakSelf.location = location;
} else {
}
};
}
5喷橙、在mapView被拖動時啥么,更新位置發(fā)起翻解析搜索請求
#pragma mark--移動地圖的時候點(diǎn)擊事件
- (void)mapView:(MAMapView *)mapView mapDidMoveByUser:(BOOL)wasUserAction {
if (!wasUserAction) {
return;
}
CGPoint center = CGPointMake(self.mapView.bounds.size.width * 0.5, self.mapView.bounds.size.height * 0.5);
CLLocationCoordinate2D coor2d = [mapView convertPoint:center toCoordinateFromView:self.mapView];
self.lastLocation = coor2d;
// ReGEO解析
AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
request.location = [AMapGeoPoint locationWithLatitude:coor2d.latitude longitude:coor2d.longitude];
[self.searchAPI AMapReGoecodeSearch:request];
}
6、更新位置
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
NSString *address = response.regeocode.formattedAddress;
self.positonLabel.text = address;
// 定位結(jié)果
NSLog(@"location:{lat:%f; lon:%f}", request.location.latitude, request.location.longitude);
self.pinView.hidden = NO;
[self.mapView setCenterCoordinate:self.lastLocation animated:YES];
}
案例demo如下:
https://github.com/Wululu6/MapKitDemo1.git
參考內(nèi)容:
http://www.reibang.com/p/0cd5260153c9
http://www.reibang.com/p/2ede1fc70fe2
查看地圖更多內(nèi)容請點(diǎn)擊以下鏈接:
定位