IOS Android 地图 取点 支持地图的缩放,平移,旋转

这篇博客介绍了如何在iOS和Android平台上实现地图的取点功能,同时支持地图的缩放、平移操作。对于iOS,文章重点讲解了2个关键方法,并指出需将屏幕横屏。而对于Android,提到了地图的缩放操作和取点的相关操作。
摘要由CSDN通过智能技术生成

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;
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值