IOS Android 地图 取点 支持地图的缩放,平移,旋转
废话不多说 先看效果
直接上代码
———IOS———
主要介绍2个方法
需要把屏幕横屏
平移,缩放 (旋转未做)
// 屏幕宽高
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGTH [UIScreen mainScreen].bounds.size.height
//状态栏大小
#define SCREEN_OFFSET 64
//先定义几个变量 一会要用到
float screenHight , screenWidth , gridHight , gridWidth ,X,Y;
float scale , centerX , centerY, oldDX , oldDY,angle;
float MAX_SCALE,MIN_SCALE;
初始化时调用
//初始化
[self startGridHW:image.size.height :image.size.width andScreenHW:SCREEN_WIDTH- SCREEN_OFFSET :SCREEN_HEIGTH];
imgv1 = [[UIImageView alloc]initWithFrame:CGRectMake(centerX, centerY,scale*image.size.width,scale*image.size.height)];
imgv1.userInteractionEnabled = YES;
[self.view addSubview:imgv1];
//平移
-(void)doMoveAction:(UIPanGestureRecognizer *)recognizer{
// Figure out where the user is trying to drag the view.
CGPoint translation = [recognizer translationInView:imgv1];//得到偏移量
float distanceX = translation.x;
float distanceY = translation.y;
//计算一下地图上的xy坐标进行限制
float tX = ((X-distanceX) / scale);
float tY = ((Y+distanceY) / scale);
if(tX >0 && tX < gridWidth){
X = X - distanceX;
centerX += distanceX;
oldDX += distanceX;//记录总的偏移量,缩放时使用
}else{
distanceX = 0;
}
if(tY >0 && tY < gridHight){
Y = Y + distanceY;
centerY += distanceY;
oldDY += distanceY;
}else{
distanceY = 0;
}
[imgv1 setCenter:(CGPoint){ imgv1.center.x+distanceX,imgv1.center.y +distanceY}];
[recognizer setTranslation:CGPointZero inView:imgv1.superview];
// self.label.text = [NSString stringWithFormat:@"(%.f,%.f)",X/scale,Y/scale];
}
缩放
-(void)onScale:(UIPinchGestureRecognizer*)gesture
{
//临时变量保存 缩放比例 如果符合把缩放比例赋值
float scaleFactor = gesture.scale;
float tempScale =scale * scaleFactor;
if(tempScale>=MIN_SCALE&&tempScale<=MAX_SCALE){
float currentX = ((gridWidth * tempScale / 2 - oldDX)/tempScale);
float currentY = ((gridHight * tempScale / 2 + oldDY)/tempScale);
if(currentX > 0 && currentX < gridWidth && currentY > 0 && currentY < gridHight){
scale = scale*scaleFactor;
centerX = (screenWidth - gridWidth * scale) / 2 + oldDX;
centerY = (screenHight - gridHight * scale) / 2 + oldDY;
//计算缩放后新的坐标点
X = gridWidth * scale / 2 - oldDX;
Y = gridHight * scale / 2 + oldDY;
//图像的平移
imgv1.transform = CGAffineTransformScale(imgv1.transform, scaleFactor, scaleFactor);
//画出坐标
// self.label.text = [NSString stringWithFormat:@"(%.f,%.f)",X/scale,Y/scale];
}
}
//及时处理手势缩放
gesture.scale = 1;
}
初始化
//初始化 在draw 调用
-(void)startGridHW:(int)gridH :(int)gridW andScreenHW:(int)screenH :(int)screenW
{
//设置最大 最小缩放比例
MAX_SCALE = 3;
MIN_SCALE = 0.2;
//设置屏幕 宽高 绘制区域
screenHight = screenH;
screenWidth = screenW;
//地图 的宽高
gridWidth = gridW;