.h文件
#import <UIKit/UIKit.h>
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>
@interface MyUserLocation : UIViewController
{
void (^Location)(AMapReGeocode *reGeocode);
}
- (void)location:(void (^)(AMapReGeocode * reGeocode))block;
@end
.m文件
#import "MyUserLocation.h"
#import "Method.h"
#define SPAN MACoordinateSpanMake(0.025, 0.025)
@interface MengUserLocation()<MAMapViewDelegate,AMapSearchDelegate>
@property (nonatomic ,strong) MAMapView * mapView;
@property (nonatomic ,strong) AMapSearchAPI * mapSearchAPI;
@property (nonatomic ,strong) MAUserLocation * currentLocation;
@property (nonatomic ,strong) NSMutableDictionary * userLocationDict;
@end
@implementation MyUserLocation
- (instancetype)init
{
self = [super init];
if (self)
{
self.mapView = [[MAMapView alloc] init];
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
self.mapSearchAPI = [[AMapSearchAPI alloc] initWithSearchKey:[MAMapServices sharedServices].apiKey Delegate:self];
self.userLocationDict = [NSMutableDictionary dictionary];
}
return self;
}
- (void)mapViewWillStartLocatingUser:(MAMapView *)mapView
{
if(![CLLocationManager locationServicesEnabled]){
UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失败" message:@"请在手机设置中开启定位功能\n开启步骤:设置 > 隐私 > 位置 > 定位服务" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
self.mapView = nil;
self.mapView.delegate = nil;
return;
}else{
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) {
UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失败" message:@"请在手机设置中开启定位功能\n开启步骤:设置 > 隐私 > 位置 > 定位服务下《***》应用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
self.mapView = nil;
self.mapView.delegate = nil;
return;
}
}
}
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
if (!isEqualCoordinate(self.currentLocation.coordinate, userLocation.coordinate, 0.001)) {
[self.userLocationDict setObject:[NSNumber numberWithFloat:userLocation.location.coordinate.longitude] forKey:@"longitude"];
[self.userLocationDict setObject:[NSNumber numberWithFloat:userLocation.location.coordinate.latitude] forKey:@"latitude"];
self.currentLocation = userLocation;
[self.mapView setRegion:MACoordinateRegionMake(self.currentLocation.coordinate, SPAN) animated:YES];
CLLocation * location = userLocation.location;
AMapReGeocodeSearchRequest * request = [[AMapReGeocodeSearchRequest alloc] init];
request.searchType = AMapSearchType_ReGeocode;
request.requireExtension = YES;
AMapGeoPoint * point = [AMapGeoPoint locationWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
request.location = point;
[self.mapSearchAPI AMapReGoecodeSearch:request];
}
}
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
NSLog(@"response = %@",response);
if (Location) {
Location(response.regeocode);
}
}
3 个解决方案
#1
人工置顶
#2
刚刚UINavigationController推到这个地图类的时候,定位回调居然调用了.这是什么情况呀?如果我这个类不是继承UIViewController,而是继承NSObject,那岂不是永远都不能调用地图了?
#3
需要代理的订阅者去实现地图的代理方法。你上面的控制器中,指明了要实现的协议<MAMapViewDelegate,AMapSearchDelegate>
self.mapView.delegate = self; ///////这句指明了使用当前控制器作为代理的接收者
self.mapView.delegate = self; ///////这句指明了使用当前控制器作为代理的接收者
#1
人工置顶
#2
刚刚UINavigationController推到这个地图类的时候,定位回调居然调用了.这是什么情况呀?如果我这个类不是继承UIViewController,而是继承NSObject,那岂不是永远都不能调用地图了?
#3
需要代理的订阅者去实现地图的代理方法。你上面的控制器中,指明了要实现的协议<MAMapViewDelegate,AMapSearchDelegate>
self.mapView.delegate = self; ///////这句指明了使用当前控制器作为代理的接收者
self.mapView.delegate = self; ///////这句指明了使用当前控制器作为代理的接收者