使用旋转/缩放/翻译在Google地图上定位图像

 玛丽 发布于 2023-01-30 15:55

我正在开发用于在谷歌地图上定位图像的用户界面.我从:http://overlay-tiler.googlecode.com/svn/trunk/upload.html开始,这非常接近我想要的.

但是我想要一个旋转工具,一个缩放工具和一个翻译工具(后者存在),而不是3个接触点.

我试图添加一个旋转工具,但它不能像我预期的那样工作:

我在左下角放了一个点来控制旋转(围绕图像的中心).鼠标拖动控制点,我计算其他3个点.

我的代码基于移动器对象,但我更改了onMouseMove函数:

overlaytiler.Rotater.prototype.rotateDot_ = function(dot, theta, origin) {
  dot.x = ((dot.x - origin.x) * Math.cos(theta) - (dot.y - origin.y) * Math.sin(theta)) + origin.x;
  dot.y = ((dot.x - origin.x) * Math.sin(theta) + (dot.y - origin.y) * Math.cos(theta)) + origin.y;
  dot.render();
};

overlaytiler.Rotater.prototype.onMouseMove_ = function(e) {
  var dots = this.controlDots_;
  var center = overlaytiler.Rotater.prototype.getCenter_(dots);

  // Diagonal length
  var r = Math.sqrt(Math.pow(this.x - center.x, 2) + Math.pow(this.y - center.y, 2));
  var old = {
    x: this.x,
    y: this.y
  };

  // Real position
  var newPos = {
    x: this.x + e.clientX - this.cx,
    y: this.y + e.clientY - this.cy
  }

  var newR = Math.sqrt(Math.pow(newPos.x - center.x, 2) + Math.pow(newPos.y - center.y, 2));
  var theta = - Math.acos((2 * r * r - (Math.pow(newPos.x - old.x, 2) + Math.pow(newPos.y - old.y, 2))) / (2 * r * r));

  // Fixed distance position
  this.x = (newPos.x - center.x) * (r / newR) + center.x;
  this.y = (newPos.y - center.y) * (r / newR) + center.y;

  dots[1].x = center.x + (center.x - this.x);
  dots[1].y = center.y + (center.y - this.y);
  dots[1].render();

  overlaytiler.Rotater.prototype.rotateDot_(dots[2], theta, center);
  overlaytiler.Rotater.prototype.rotateDot_(dots[0], theta, center);

  // Render
  this.render();

  this.cx = e.clientX;
  this.cy = e.clientY;
};

不幸的是,精度和角度符号存在问题.

http://jsbin.com/iQEbIzo/4/

在几次旋转之后,图像高度失真,并且仅在一个方向上支持旋转.

我想知道如何才能达到很高的精度并且没有任何失真.

也许我的方法在这里没用(尝试在正确的坐标处移动角落),我试图用画布旋转图像,但我的尝试没有成功.

编辑:完整的工作版本:http://jsbin.com/iQEbIzo/7/

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有