当前位置: 首页 > 知识库问答 >
问题:

请问在加工过程中,有没有人可以把mouseX和mouseY转换成经纬度?

山疏珂
2023-03-14

我正在使用来自Till Nagel的代码来投影世界地图,然后基于lat和lon坐标绘制数据。但现在我要检索鼠标位置的lat和lon坐标。通过给mouseX和mouseY,它应该返回地图上的经度和纬度。谁能帮帮忙吗?下面是我使用的代码:

/**
 * Utility class to convert between geo-locations and Cartesian screen coordinates.
 * Can be used with a bounding box defining the map section.
 *
 * (c) 2011 Till Nagel, tillnagel.com
 */
public class MercatorMap {

  public static final float DEFAULT_TOP_LATITUDE = 80;
  public static final float DEFAULT_BOTTOM_LATITUDE = -80;
  public static final float DEFAULT_LEFT_LONGITUDE = -180;
  public static final float DEFAULT_RIGHT_LONGITUDE = 180;

  /** Horizontal dimension of this map, in pixels. */
  protected float mapScreenWidth;
  /** Vertical dimension of this map, in pixels. */
  protected float mapScreenHeight;

  /** Northern border of this map, in degrees. */
  protected float topLatitude;
  /** Southern border of this map, in degrees. */
  protected float bottomLatitude;
  /** Western border of this map, in degrees. */
  protected float leftLongitude;
  /** Eastern border of this map, in degrees. */
  protected float rightLongitude;

  private float topLatitudeRelative;
  private float bottomLatitudeRelative;
  private float leftLongitudeRadians;
  private float rightLongitudeRadians;

  public MercatorMap(float mapScreenWidth, float mapScreenHeight) {
    this(mapScreenWidth, mapScreenHeight, DEFAULT_TOP_LATITUDE, DEFAULT_BOTTOM_LATITUDE, DEFAULT_LEFT_LONGITUDE, DEFAULT_RIGHT_LONGITUDE);
  }

  /**
   * Creates a new MercatorMap with dimensions and bounding box to convert between geo-locations and screen coordinates.
   *
   * @param mapScreenWidth Horizontal dimension of this map, in pixels.
   * @param mapScreenHeight Vertical dimension of this map, in pixels.
   * @param topLatitude Northern border of this map, in degrees.
   * @param bottomLatitude Southern border of this map, in degrees.
   * @param leftLongitude Western border of this map, in degrees.
   * @param rightLongitude Eastern border of this map, in degrees.
   */
  public MercatorMap(float mapScreenWidth, float mapScreenHeight, float topLatitude, float bottomLatitude, float leftLongitude, float rightLongitude) {
    this.mapScreenWidth = mapScreenWidth;
    this.mapScreenHeight = mapScreenHeight;
    this.topLatitude = topLatitude;
    this.bottomLatitude = bottomLatitude;
    this.leftLongitude = leftLongitude;
    this.rightLongitude = rightLongitude;
    this.topLatitudeRelative = getScreenYRelative(topLatitude);
    this.bottomLatitudeRelative = getScreenYRelative(bottomLatitude);
    this.leftLongitudeRadians = getRadians(leftLongitude);
    this.rightLongitudeRadians = getRadians(rightLongitude);
  }

  /**
   * Projects the geo location to Cartesian coordinates, using the Mercator projection.
   *
   * @param geoLocation Geo location with (latitude, longitude) in degrees.
   * @returns The screen coordinates with (x, y).
   */
  public PVector getScreenLocation(PVector geoLocation) {
    float latitudeInDegrees = geoLocation.x;
    float longitudeInDegrees = geoLocation.y;
    return new PVector(getScreenX(longitudeInDegrees), getScreenY(latitudeInDegrees));

  }


  private float getScreenYRelative(float latitudeInDegrees) {
    return log(tan(latitudeInDegrees / 360f * PI + PI / 4));
  }

  protected float getScreenY(float latitudeInDegrees) {
    return mapScreenHeight * (getScreenYRelative(latitudeInDegrees) - topLatitudeRelative) / (bottomLatitudeRelative - topLatitudeRelative);
  }

  private float getRadians(float deg) {
    return deg * PI / 180;
  }


  protected float getScreenX(float longitudeInDegrees) {
    float longitudeInRadians = getRadians(longitudeInDegrees);
    return mapScreenWidth * (longitudeInRadians - leftLongitudeRadians) / (rightLongitudeRadians - leftLongitudeRadians);
  }
}

共有1个答案

何兴邦
2023-03-14
void setup(){
  size(500, 500);
}

void draw(){
  PVector p = getLatLong(mouseX, mouseY);
  println("longitude: " + p.x + ", latitude: " + p.y); 
}

PVector getLatLong(int x, int y){
  return new PVector(
    map(x, 0, width-1, -180, 180),
    map(y, 0, height-1, 90, -90));
}
 类似资料:
  • 我使用Android架构组件(ViewModel,LiveData),并使用Room从使用存储库的本地Sqlite数据库中获取数据。最外面的观察者很好,问题只出在内部观察者身上。 参见代码: 我用命名和匿名观察者都试过了。我不明白这里有什么问题。 有时它会插入NullPointerExceptions。有时无法更改区域设置。 还有一个问题是,我不确定应该在片段中使用哪一个作为观察者的所有者,或(我

  • 是否有人设法让spring-data-neo4j在直接数据访问模式下使用Grails,如此处所述? 我无法让Grails尊重下面的@Autowired标记。下面的代码是spring在其文档中给出的示例的一个非常简单的版本: 我在执行grails run app时收到以下错误消息(这是一系列更长的嵌套异常,但这是主要原因): 创建在类路径资源[hello/TestGraph.class]中定义了名为

  • 问题内容: 我正在开发一个新项目(),并创建了一个包含大量变量的Object。由于我打算为所有这些添加吸气剂和设置器,所以我想知道:是否存在在给定类中自动生成吸气剂和设置器的捷径? 问题答案: 在所需类的源代码窗口中弹出上下文菜单(即右键单击)。然后选择子菜单;从该菜单中进行选择将导致出现向导窗口。 选择您要为其创建getter和setter的变量,然后单击。

  • 问题内容: 如何在Java中将经纬度转换为北向和东向? 问题答案: 我假设您的意思是英国OSGB向东和向北。这个三角函数背后的三角函数很有趣,但是您可以为此使用JCoord库,这很容易(如果占用大量CPU)。 为了跟进下面@DD的评论,JCoord有一个问题,因为在从Easting / Northing转换为Lat / Long时必须确保使用正确的基准,反之亦然。 使用@DD的代码: 这将返回使用

  • 本文向大家介绍请问你有没有做过压力测试相关面试题,主要包含被问及请问你有没有做过压力测试时的应答技巧和注意事项,需要的朋友参考一下 参考回答: 在软件工程中,压力测试是对系统不断施加压力的测试,是通过确定一个系统的瓶颈或者不能接收的性能点,来获得系统能提供的最大服务级别的测试。例如测试一个Web 站点在大量的负荷下,何时系考察公司:网易 统的响应会退化或失败。网络游戏中也常用到这个词汇。

  • 问题内容: 我正在尝试将字符串转换为。我不是在尝试解析主机名:字符串是ipv4地址。有工作吗?还是我必须手动解析它? 问题答案: 这样做更好,因为无论传递什么字符串,它都不会进行DNS查找。