用的是系统的mapview,因返回的是火星坐标,在进行反地理编码时出现偏差,用网上的一些坐标转换发现还是不准确,无奈只能引入高德地图的AMapSearch进行反地理编码。想想前两天出去面试,面试官一本正经否定我,说mapview是地球坐标 CLLocation是火星坐标 我也是醉的不行不行的了。。。废话不多说了直接上代码
(一)
#import "AijeeSuperViewController.h"
#import "AMapSearchKit.h"
@interface LocateForYourSelfVC : AijeeSuperViewController
@property (nonatomic, copy) void (^callBack)(id obc);
@property (nonatomic , copy) AMapSearchAPI * search;
@property (nonatomic, strong) CLLocation * location;
@property (nonatomic, strong) NSString * strBusinnessName;
@end
(二)
#import "LocateForYourSelfVC.h"
#import
#import "AMapSearchKit.h"
#import "KCAnnotation.h"//自定义大头针
@interface LocateForYourSelfVC ()
{
MKMapView* mapview;
CLLocationCoordinate2D coordi;
CLLocationCoordinate2D coor2self;//纪录自己的坐标位置
UILabel * addressLabel;
BOOL isOK;//是否可以点击确定 上传定位信息
BOOL isFirst;
BOOL isOut;//判断用户是否滑动地图出了边界
// AMapReGeocodeSearchRequest *regeo;
UIImageView * imagvi;
UILabel * labelAlertUp;
UIImageView * addressImageview;
UIButton * clickBackSelf;
KCAnnotation *annotation1;
CLLocationCoordinate2D coords;//记录商户详情天转过来时 商户的坐标
}
@end
@implementation LocateForYourSelfVC
- (void)viewDidLoad {
//配置用户Key
[AMapSearchServices sharedServices].apiKey = @"";
//初始化检索对象
_search = [[AMapSearchAPI alloc] init];
_search.delegate = self;
[super viewDidLoad];
self.titleText.text = @"地图定位";
self.navigationItem.leftBarButtonItem = ({
UIImage* image= [UIImage imageNamed:@"aijeeBtnbackimage"];
CGRect backframe= CGRectMake(0, 2, 40, 40);
UIButton* dismissButton= [UIButton buttonWithType:UIButtonTypeCustom];
dismissButton.frame = backframe;
[dismissButton setBackgroundImage:image forState:UIControlStateNormal];
[dismissButton setBackgroundColor:ColoClear];
[dismissButton addTarget:self action:@selector(dismissButtonAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *dismissBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:dismissButton];
dismissBarButtonItem;
});
//初始化地图视图
mapview = [[MKMapView alloc]initWithFrame:self.view.bounds];
//地图的代理方法
mapview.delegate = self;
//是否显示当前的位置
mapview.showsUserLocation = YES;
//地图的类型, iOS开发中自带的地图
/* MKMapTypeStandard 标准地图 MKMapTypeSatellite 卫星地图 MKMapTypeHybrid 混合地图 */
mapview.mapType = MKMapTypeStandard;
[self.view addSubview:mapview];
mapview.userTrackingMode = MKUserTrackingModeFollow;
//设置不允许地图旋转
mapview.rotateEnabled = NO;
if (!self.location) {
self.navigationItem.rightBarButtonItem = ({
UIBarButtonItem *noneBarButtonitem;
noneBarButtonitem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(buttonClicked:)];
[noneBarButtonitem setTintColor:ColoWhite];
[noneBarButtonitem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.f],NSForegroundColorAttributeName:ColoWhite} forState:UIControlStateNormal];
noneBarButtonitem;
});
labelAlertUp = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, SizeWidth, 40)];
labelAlertUp.text = @"移动地图,点击确定后重新定位";
labelAlertUp.font = [UIFont systemFontOfSize:14];
labelAlertUp.textAlignment = NSTextAlignmentCenter;
labelAlertUp.backgroundColor = [UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:0.50f];
labelAlertUp.textColor = ColoWhite;
[self.view addSubview:labelAlertUp];
addressImageview = [[UIImageView alloc]initWithFrame:CGRectMake(10, Sizeheight-50, SizeWidth-20, 40)];
addressImageview.image = [UIImage imageNamed:@"LocateDonwIV"];
[self.view addSubview:addressImageview];
imagvi = [[UIImageView alloc]initWithFrame:CGRectMake((SizeWidth-27)/2.0, Sizeheight/2.0, 27, 37.5)];
imagvi.image = [UIImage imageNamed:@"LocateForSelf"];
[self.view addSubview:imagvi];
UIImageView * addrIV = [[UIImageView alloc]initWithFrame:CGRectMake(12, 10, 16, 20)];
addrIV.image = [UIImage imageNamed:@"LocateSmallIV"];
[addressImageview addSubview:addrIV];
addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(addrIV.frame)+10, 0, SizeWidth-20-28-10, 40)];
[addressImageview addSubview:addressLabel];
}
[NSTimer scheduledTimerWithTimeInterval:3.f target:self selector:@selector(ClickCreartime) userInfo:nil repeats:NO];
if (self.location)//用一个参数判断是从哪里跳转此页面的
{
//只在第一次进这个控制器的时候 在这个指定位置添加大头针
//用于其他界面复用此界面 定位指定的坐标位置
coords = CLLocationCoordinate2DMake(self.location.coordinate.latitude,self.location.coordinate.longitude);//39.9884490052,116.4315448786
float zoomLevel = 0.02;
MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
[mapview setRegion:[mapview regionThatFits:region] animated:YES];
annotation1 = [[KCAnnotation alloc]init];
if (!self.strBusinnessName) {
self.strBusinnessName = @"商户";
}
annotation1.title = self.strBusinnessName;
annotation1.subtitle = @"地址";
annotation1.coordinate = coords;
annotation1.image=[UIImage imageNamed:@"LocateForBusiness"];
[mapview addAnnotation:annotation1];
[mapview selectAnnotation:annotation1 animated:YES];
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:coords.latitude longitude:coords.longitude];
regeo.radius = 10000;
regeo.requireExtension = YES;
//发起逆地理编码
[_search AMapReGoecodeSearch: regeo];
}
}
-(void)ClickCreartime
{
clickBackSelf = [UIButton buttonWithType:UIButtonTypeCustom];
[clickBackSelf setFrame:CGRectMake(SizeWidth-55, Sizeheight-110, 45, 45)];
[clickBackSelf addTarget:self action:@selector(clickBackSelf:) forControlEvents:UIControlEventTouchUpInside];
[clickBackSelf setBackgroundImage:[UIImage imageNamed:@"LocateSelf"] forState:UIControlStateNormal];
[self.view addSubview:clickBackSelf];
if (self.location) {
clickBackSelf.frame = CGRectMake(SizeWidth-55, Sizeheight-87.5, 45, 45);
}
}
#pragma mark - click button
- (void)clickBackSelf:(UIButton *)sender{
[mapview setCenterCoordinate:mapview.userLocation.location.coordinate animated:YES];
}
- (void)dismissButtonAction:(UIButton *)sender
{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)buttonClicked:(UIButton *)sender{
if (isOK) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
}];
NSString *strLat = [NSString stringWithFormat:@"%f",coordi.latitude];
NSString *strLong = [NSString stringWithFormat:@"%f",coordi.longitude];
NSMutableDictionary *dicLotion = [[NSMutableDictionary alloc] init];
[dicLotion setValue:strLat forKey:@"latitude"];
[dicLotion setValue:strLong forKey:@"longitude"];
[dicLotion setValue:addressLabel.text forKey:@"strAddress"];
// SBJson4Writer *writer = [[SBJson4Writer alloc]init];
self.callBack(dicLotion);
}else{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请选用附近的位置" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
[alert show];
}
}
#pragma mark - deleget
//更新用户位置,只要用户位置改变则调用此方法(包括第一次定位到用户位置)
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
if (!isFirst)
{
if (!self.location)//用一个参数判断是从哪里跳转此页面的
{
coordi = (CLLocationCoordinate2D){userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude};
coor2self = coordi;
MKCoordinateSpan span = {0.005,0.005};
MKCoordinateRegion region = {coordi,span};
//mapview.zoomEnabled = NO;
//mapview.scrollEnabled = NO;
[mapview setRegion:region];
//构造AMapReGeocodeSearchRequest对象
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:coordi.latitude longitude:coordi.longitude];
regeo.radius = 10000;
regeo.requireExtension = YES;
//发起逆地理编码
[_search AMapReGoecodeSearch: regeo];
}
}
isFirst = YES;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation{
//由于当前位置的标注也是一个大头针,所以此时需要判断,此代理方法返回nil使用默认大头针视图
if ([annotation isKindOfClass:[KCAnnotation class]])
{
if (self.location)
{
static NSString *key1=@"AnnotationKey1";
MKAnnotationView *annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:key1];
//如果缓存池中不存在则新建
if (!annotationView) {
annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:key1];
annotationView.canShowCallout = true;//允许交互点击
annotationView.calloutOffset = CGPointMake(0, -10);//定义详情视图偏移量
//annotationView.leftCalloutAccessoryView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gift_bag"]];//定义详情左侧视图
}
//修改大头针视图
//重新设置此类大头针视图的大头针模型(因为有可能是从缓存池中取出来的,位置是放到缓存池时的位置)
annotationView.annotation = annotation;
annotationView.image = ((KCAnnotation *)annotation).image;//设置大头针视图的图片
return annotationView;
}
else
{
return nil;
}
}
else
{
return nil;
}
}
//可以打印经纬度的跨度,用来测试当前视图下地经纬度跨度是多少,然后用于上面的MKCoordinateSpanMake方法中
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
if (!self.location) {
double x = mapView.center.x;
double y = mapView.center.y;
CGPoint point=CGPointMake( x, y);
coordi = [mapview convertPoint:point toCoordinateFromView:mapview];
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:coordi.latitude longitude:coordi.longitude];
regeo.radius = 10000;
regeo.requireExtension = YES;
//发起逆地理编码
[_search AMapReGoecodeSearch: regeo];
//第一个坐标
CLLocation *current=[[CLLocation alloc] initWithLatitude:coordi.latitude longitude:coordi.longitude];
//第二个坐标
CLLocation *before=[[CLLocation alloc] initWithLatitude:coor2self.latitude longitude:coor2self.longitude];
// 计算距离
CLLocationDistance meters=[current distanceFromLocation:before];
// MKMapPoint point1 = MKMapPointForCoordinate(CLLocationCoordinate2DMake(coordi.latitude,coordi.longitude));
//
// MKMapPoint point2 = MKMapPointForCoordinate(CLLocationCoordinate2DMake(coor2self.latitude,coor2self.longitude));
// //2.计算距离
// CLLocationDistance distance = MKMetersBetweenMapPoints(point1,point2);
DLog(@"%f",meters);
if (meters > 1000) {
if (isOK) {
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请选用附近的位置" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
[alert show];
}
isOK = NO;
}
else{
isOK = YES;
}
}
}
//实现逆地理编码的回调函数
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
if(response.regeocode != nil)
{
//通过AMapReGeocodeSearchResponse对象处理搜索结果
AMapReGeocode *regeocode=response.regeocode;
NSLog(@"%@",regeocode.formattedAddress);
addressLabel.text = regeocode.formattedAddress;
if (self.location)
{
annotation1.subtitle = regeocode.formattedAddress;
}
}
}
(三)
#import
#import
@interface KCAnnotation : NSObject
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
#pragma mark 自定义一个图片属性在创建大头针视图时使用
@property (nonatomic,strong) UIImage *image;
@end
用的是系统的mapview,因返回的是火星坐标,在进行反地理编码时出现偏差,用网上的一些坐标转换发现还是不准确,无奈只能引入高德地图的AMapSearch进行反地理编码。想想前两天出去面试,面试官一本正经否定我,说mapview是地球坐标 CLLocation是火星坐标 我也是醉的不行不行的了。。。废话不多说了直接上代码
(一)
#import "AijeeSuperViewController.h"
#import "AMapSearchKit.h"
@interface LocateForYourSelfVC : AijeeSuperViewController
@property (nonatomic, copy) void (^callBack)(id obc);
@property (nonatomic , copy) AMapSearchAPI * search;
@property (nonatomic, strong) CLLocation * location;
@property (nonatomic, strong) NSString * strBusinnessName;
@end
(二)
#import "LocateForYourSelfVC.h"
#import
#import "AMapSearchKit.h"
#import "KCAnnotation.h"//自定义大头针
@interface LocateForYourSelfVC ()
{
MKMapView* mapview;
CLLocationCoordinate2D coordi;
CLLocationCoordinate2D coor2self;//纪录自己的坐标位置
UILabel * addressLabel;
BOOL isOK;//是否可以点击确定 上传定位信息
BOOL isFirst;
BOOL isOut;//判断用户是否滑动地图出了边界
// AMapReGeocodeSearchRequest *regeo;
UIImageView * imagvi;
UILabel * labelAlertUp;
UIImageView * addressImageview;
UIButton * clickBackSelf;
KCAnnotation *annotation1;
CLLocationCoordinate2D coords;//记录商户详情天转过来时 商户的坐标
}
@end
@implementation LocateForYourSelfVC
- (void)viewDidLoad {
//配置用户Key
[AMapSearchServices sharedServices].apiKey = @"";
//初始化检索对象
_search = [[AMapSearchAPI alloc] init];
_search.delegate = self;
[super viewDidLoad];
self.titleText.text = @"地图定位";
self.navigationItem.leftBarButtonItem = ({
UIImage* image= [UIImage imageNamed:@"aijeeBtnbackimage"];
CGRect backframe= CGRectMake(0, 2, 40, 40);
UIButton* dismissButton= [UIButton buttonWithType:UIButtonTypeCustom];
dismissButton.frame = backframe;
[dismissButton setBackgroundImage:image forState:UIControlStateNormal];
[dismissButton setBackgroundColor:ColoClear];
[dismissButton addTarget:self action:@selector(dismissButtonAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *dismissBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:dismissButton];
dismissBarButtonItem;
});
//初始化地图视图
mapview = [[MKMapView alloc]initWithFrame:self.view.bounds];
//地图的代理方法
mapview.delegate = self;
//是否显示当前的位置
mapview.showsUserLocation = YES;
//地图的类型, iOS开发中自带的地图
/* MKMapTypeStandard 标准地图 MKMapTypeSatellite 卫星地图 MKMapTypeHybrid 混合地图 */
mapview.mapType = MKMapTypeStandard;
[self.view addSubview:mapview];
mapview.userTrackingMode = MKUserTrackingModeFollow;
//设置不允许地图旋转
mapview.rotateEnabled = NO;
if (!self.location) {
self.navigationItem.rightBarButtonItem = ({
UIBarButtonItem *noneBarButtonitem;
noneBarButtonitem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(buttonClicked:)];
[noneBarButtonitem setTintColor:ColoWhite];
[noneBarButtonitem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.f],NSForegroundColorAttributeName:ColoWhite} forState:UIControlStateNormal];
noneBarButtonitem;
});
labelAlertUp = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, SizeWidth, 40)];
labelAlertUp.text = @"移动地图,点击确定后重新定位";
labelAlertUp.font = [UIFont systemFontOfSize:14];
labelAlertUp.textAlignment = NSTextAlignmentCenter;
labelAlertUp.backgroundColor = [UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:0.50f];
labelAlertUp.textColor = ColoWhite;
[self.view addSubview:labelAlertUp];
addressImageview = [[UIImageView alloc]initWithFrame:CGRectMake(10, Sizeheight-50, SizeWidth-20, 40)];
addressImageview.image = [UIImage imageNamed:@"LocateDonwIV"];
[self.view addSubview:addressImageview];
imagvi = [[UIImageView alloc]initWithFrame:CGRectMake((SizeWidth-27)/2.0, Sizeheight/2.0, 27, 37.5)];
imagvi.image = [UIImage imageNamed:@"LocateForSelf"];
[self.view addSubview:imagvi];
UIImageView * addrIV = [[UIImageView alloc]initWithFrame:CGRectMake(12, 10, 16, 20)];
addrIV.image = [UIImage imageNamed:@"LocateSmallIV"];
[addressImageview addSubview:addrIV];
addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(addrIV.frame)+10, 0, SizeWidth-20-28-10, 40)];
[addressImageview addSubview:addressLabel];
}
[NSTimer scheduledTimerWithTimeInterval:3.f target:self selector:@selector(ClickCreartime) userInfo:nil repeats:NO];
if (self.location)//用一个参数判断是从哪里跳转此页面的
{
//只在第一次进这个控制器的时候 在这个指定位置添加大头针
//用于其他界面复用此界面 定位指定的坐标位置
coords = CLLocationCoordinate2DMake(self.location.coordinate.latitude,self.location.coordinate.longitude);//39.9884490052,116.4315448786
float zoomLevel = 0.02;
MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
[mapview setRegion:[mapview regionThatFits:region] animated:YES];
annotation1 = [[KCAnnotation alloc]init];
if (!self.strBusinnessName) {
self.strBusinnessName = @"商户";
}
annotation1.title = self.strBusinnessName;
annotation1.subtitle = @"地址";
annotation1.coordinate = coords;
annotation1.image=[UIImage imageNamed:@"LocateForBusiness"];
[mapview addAnnotation:annotation1];
[mapview selectAnnotation:annotation1 animated:YES];
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:coords.latitude longitude:coords.longitude];
regeo.radius = 10000;
regeo.requireExtension = YES;
//发起逆地理编码
[_search AMapReGoecodeSearch: regeo];
}
}
-(void)ClickCreartime
{
clickBackSelf = [UIButton buttonWithType:UIButtonTypeCustom];
[clickBackSelf setFrame:CGRectMake(SizeWidth-55, Sizeheight-110, 45, 45)];
[clickBackSelf addTarget:self action:@selector(clickBackSelf:) forControlEvents:UIControlEventTouchUpInside];
[clickBackSelf setBackgroundImage:[UIImage imageNamed:@"LocateSelf"] forState:UIControlStateNormal];
[self.view addSubview:clickBackSelf];
if (self.location) {
clickBackSelf.frame = CGRectMake(SizeWidth-55, Sizeheight-87.5, 45, 45);
}
}
#pragma mark - click button
- (void)clickBackSelf:(UIButton *)sender{
[mapview setCenterCoordinate:mapview.userLocation.location.coordinate animated:YES];
}
- (void)dismissButtonAction:(UIButton *)sender
{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)buttonClicked:(UIButton *)sender{
if (isOK) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
}];
NSString *strLat = [NSString stringWithFormat:@"%f",coordi.latitude];
NSString *strLong = [NSString stringWithFormat:@"%f",coordi.longitude];
NSMutableDictionary *dicLotion = [[NSMutableDictionary alloc] init];
[dicLotion setValue:strLat forKey:@"latitude"];
[dicLotion setValue:strLong forKey:@"longitude"];
[dicLotion setValue:addressLabel.text forKey:@"strAddress"];
// SBJson4Writer *writer = [[SBJson4Writer alloc]init];
self.callBack(dicLotion);
}else{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请选用附近的位置" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
[alert show];
}
}
#pragma mark - deleget
//更新用户位置,只要用户位置改变则调用此方法(包括第一次定位到用户位置)
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
if (!isFirst)
{
if (!self.location)//用一个参数判断是从哪里跳转此页面的
{
coordi = (CLLocationCoordinate2D){userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude};
coor2self = coordi;
MKCoordinateSpan span = {0.005,0.005};
MKCoordinateRegion region = {coordi,span};
//mapview.zoomEnabled = NO;
//mapview.scrollEnabled = NO;
[mapview setRegion:region];
//构造AMapReGeocodeSearchRequest对象
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:coordi.latitude longitude:coordi.longitude];
regeo.radius = 10000;
regeo.requireExtension = YES;
//发起逆地理编码
[_search AMapReGoecodeSearch: regeo];
}
}
isFirst = YES;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation{
//由于当前位置的标注也是一个大头针,所以此时需要判断,此代理方法返回nil使用默认大头针视图
if ([annotation isKindOfClass:[KCAnnotation class]])
{
if (self.location)
{
static NSString *key1=@"AnnotationKey1";
MKAnnotationView *annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:key1];
//如果缓存池中不存在则新建
if (!annotationView) {
annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:key1];
annotationView.canShowCallout = true;//允许交互点击
annotationView.calloutOffset = CGPointMake(0, -10);//定义详情视图偏移量
//annotationView.leftCalloutAccessoryView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gift_bag"]];//定义详情左侧视图
}
//修改大头针视图
//重新设置此类大头针视图的大头针模型(因为有可能是从缓存池中取出来的,位置是放到缓存池时的位置)
annotationView.annotation = annotation;
annotationView.image = ((KCAnnotation *)annotation).image;//设置大头针视图的图片
return annotationView;
}
else
{
return nil;
}
}
else
{
return nil;
}
}
//可以打印经纬度的跨度,用来测试当前视图下地经纬度跨度是多少,然后用于上面的MKCoordinateSpanMake方法中
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
if (!self.location) {
double x = mapView.center.x;
double y = mapView.center.y;
CGPoint point=CGPointMake( x, y);
coordi = [mapview convertPoint:point toCoordinateFromView:mapview];
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:coordi.latitude longitude:coordi.longitude];
regeo.radius = 10000;
regeo.requireExtension = YES;
//发起逆地理编码
[_search AMapReGoecodeSearch: regeo];
//第一个坐标
CLLocation *current=[[CLLocation alloc] initWithLatitude:coordi.latitude longitude:coordi.longitude];
//第二个坐标
CLLocation *before=[[CLLocation alloc] initWithLatitude:coor2self.latitude longitude:coor2self.longitude];
// 计算距离
CLLocationDistance meters=[current distanceFromLocation:before];
// MKMapPoint point1 = MKMapPointForCoordinate(CLLocationCoordinate2DMake(coordi.latitude,coordi.longitude));
//
// MKMapPoint point2 = MKMapPointForCoordinate(CLLocationCoordinate2DMake(coor2self.latitude,coor2self.longitude));
// //2.计算距离
// CLLocationDistance distance = MKMetersBetweenMapPoints(point1,point2);
DLog(@"%f",meters);
if (meters > 1000) {
if (isOK) {
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请选用附近的位置" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
[alert show];
}
isOK = NO;
}
else{
isOK = YES;
}
}
}
//实现逆地理编码的回调函数
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
if(response.regeocode != nil)
{
//通过AMapReGeocodeSearchResponse对象处理搜索结果
AMapReGeocode *regeocode=response.regeocode;
NSLog(@"%@",regeocode.formattedAddress);
addressLabel.text = regeocode.formattedAddress;
if (self.location)
{
annotation1.subtitle = regeocode.formattedAddress;
}
}
}
(三)
#import
#import
@interface KCAnnotation : NSObject
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
#pragma mark 自定义一个图片属性在创建大头针视图时使用
@property (nonatomic,strong) UIImage *image;
@end