首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从纬度、经度转换为x、y

从纬度、经度转换为x、y
EN

Stack Overflow用户
提问于 2013-04-29 03:23:49
回答 4查看 112.1K关注 0票数 37

我想把GPS的位置(纬度,经度)转换成x,y坐标。我找到了许多关于这个主题的链接,并应用了它,但它没有给我正确的答案!

我遵循以下步骤来测试答案:(1)首先,我采取两个位置,并使用地图计算它们之间的距离。(2)然后将两个位置转换为x,y坐标。(3)然后再次计算x,y坐标中两点之间的距离,看看它是否在点(1)中给出了相同的结果。

我找到了下面的解决方案之一,但它没有给我正确的答案!

代码语言:javascript
复制
latitude = Math.PI * latitude / 180;
longitude = Math.PI * longitude / 180;

// adjust position by radians
latitude -= 1.570795765134; // subtract 90 degrees (in radians)

// and switch z and y 
xPos = (app.radius) * Math.sin(latitude) * Math.cos(longitude);
zPos = (app.radius) * Math.sin(latitude) * Math.sin(longitude);
yPos = (app.radius) * Math.cos(latitude);

我也尝试了这个link,但仍然不能很好地与我一起工作!

如何将(纬度,经度)转换为(x,y)?

谢谢,

EN

回答 4

Stack Overflow用户

发布于 2018-12-18 14:15:23

我想和你分享我是如何处理这个问题的。我使用了等角投影,就像@MvG说的那样,但这种方法给出了与地球(或整个地图)相关的X和Y位置,这意味着你得到了全局位置。在我的例子中,我想转换一小块区域(大约500米见方)的坐标,所以我将投影点关联到另外两个点,获得全局位置并与本地(屏幕上)位置相关,就像这样:

首先,我在想要投影的区域周围选择两个点(左上角和右下角),就像下面的图片:

一旦我有了lat和lng格式的全局引用区域,我就可以对屏幕位置执行相同的操作。包含此数据的对象如下所示。

代码语言:javascript
复制
//top-left reference point
var p0 = {
    scrX: 23.69,        // Minimum X position on screen
    scrY: -0.5,         // Minimum Y position on screen
    lat: -22.814895,    // Latitude
    lng: -47.072892     // Longitude
}

//bottom-right reference point
var p1 = {
    scrX: 276,          // Maximum X position on screen
    scrY: 178.9,        // Maximum Y position on screen
    lat: -22.816419,    // Latitude
    lng: -47.070563     // Longitude
}

var radius = 6371;      //Earth Radius in Km

//## Now I can calculate the global X and Y for each reference point ##\\

// This function converts lat and lng coordinates to GLOBAL X and Y positions
function latlngToGlobalXY(lat, lng){
    //Calculates x based on cos of average of the latitudes
    let x = radius*lng*Math.cos((p0.lat + p1.lat)/2);
    //Calculates y based on latitude
    let y = radius*lat;
    return {x: x, y: y}
}

// Calculate global X and Y for top-left reference point
p0.pos = latlngToGlobalXY(p0.lat, p0.lng);
// Calculate global X and Y for bottom-right reference point
p1.pos = latlngToGlobalXY(p1.lat, p1.lng);

/*
* This gives me the X and Y in relation to map for the 2 reference points.
* Now we have the global AND screen areas and then we can relate both for the projection point.
*/

// This function converts lat and lng coordinates to SCREEN X and Y positions
function latlngToScreenXY(lat, lng){
    //Calculate global X and Y for projection point
    let pos = latlngToGlobalXY(lat, lng);
    //Calculate the percentage of Global X position in relation to total global width
    pos.perX = ((pos.x-p0.pos.x)/(p1.pos.x - p0.pos.x));
    //Calculate the percentage of Global Y position in relation to total global height
    pos.perY = ((pos.y-p0.pos.y)/(p1.pos.y - p0.pos.y));

    //Returns the screen position based on reference points
    return {
        x: p0.scrX + (p1.scrX - p0.scrX)*pos.perX,
        y: p0.scrY + (p1.scrY - p0.scrY)*pos.perY
    }
}

//# The usage is like this #\\

var pos = latlngToScreenXY(-22.815319, -47.071718);
$point = $("#point-to-project");
$point.css("left", pos.x+"em");
$point.css("top", pos.y+"em");

如你所见,我是用javascript写的,但是计算结果可以翻译成任何语言。

附注:我将转换后的位置应用于id为"point- to -project“的HTML元素。要在您的项目中使用这段代码,您应该创建此元素(样式为position absolute)或更改"usage“块。

票数 12
EN

Stack Overflow用户

发布于 2018-08-08 19:12:36

由于当我搜索同样的问题时,这个页面显示在google的顶部,所以我想提供一个更实用的答案。MVG的答案是正确的,但却是理论上的。

我用javascript为fitbit ionic做了一个轨迹绘制应用程序。下面的代码是我如何解决这个问题的。

代码语言:javascript
复制
//LOCATION PROVIDER
index.js
var gpsFix = false;
var circumferenceAtLat = 0;
function locationSuccess(pos){
  if(!gpsFix){
    gpsFix = true;
    circumferenceAtLat = Math.cos(pos.coords.latitude*0.01745329251)*111305;
  }
  pos.x:Math.round(pos.coords.longitude*circumferenceAtLat),
  pos.y:Math.round(pos.coords.latitude*110919), 
  plotTrack(pos);
}

plotting.js

代码语言:javascript
复制
plotTrack(position){

let x = Math.round((this.segments[i].start.x - this.bounds.minX)*this.scale);
let y = Math.round(this.bounds.maxY - this.segments[i].start.y)*this.scale; //heights needs to be inverted

//redraw?
let redraw = false;

//x or y bounds?
 if(position.x>this.bounds.maxX){
   this.bounds.maxX = (position.x-this.bounds.minX)*1.1+this.bounds.minX; //increase by 10%
   redraw = true;
 }
 if(position.x<this.bounds.minX){
   this.bounds.minX = this.bounds.maxX-(this.bounds.maxX-position.x)*1.1;
    redraw = true;
 };
 if(position.y>this.bounds.maxY){
   this.bounds.maxY = (position.y-this.bounds.minY)*1.1+this.bounds.minY; //increase by 10%
    redraw = true;
 }
 if(position.y<this.bounds.minY){
   this.bounds.minY = this.bounds.maxY-(this.bounds.maxY-position.y)*1.1;
    redraw = true;
 }
 if(redraw){
   reDraw();
 }
}


function reDraw(){

let xScale = device.screen.width / (this.bounds.maxX-this.bounds.minX);
let yScale = device.screen.height / (this.bounds.maxY-this.bounds.minY); 
if(xScale<yScale) this.scale = xScale; 
else this.scale = yScale;

//Loop trough your object to redraw all of them
}
票数 4
EN

Stack Overflow用户

发布于 2019-11-12 21:04:36

为了完整起见,我喜欢添加我的python改编的@allexrm代码,它工作得非常好。再次感谢!

代码语言:javascript
复制
radius = 6371    #Earth Radius in KM

class referencePoint:
    def __init__(self, scrX, scrY, lat, lng):
        self.scrX = scrX
        self.scrY = scrY
        self.lat = lat
        self.lng = lng


# Calculate global X and Y for top-left reference point        
p0 = referencePoint(0, 0, 52.526470, 13.403215)
# Calculate global X and Y for bottom-right reference point
p1 = referencePoint(2244, 2060, 52.525035, 13.405809) 


# This function converts lat and lng coordinates to GLOBAL X and Y positions
def latlngToGlobalXY(lat, lng):
    # Calculates x based on cos of average of the latitudes
    x = radius*lng*math.cos((p0.lat + p1.lat)/2)
    # Calculates y based on latitude
    y = radius*lat
    return {'x': x, 'y': y}


# This function converts lat and lng coordinates to SCREEN X and Y positions
def latlngToScreenXY(lat, lng):
    # Calculate global X and Y for projection point
    pos = latlngToGlobalXY(lat, lng)
    # Calculate the percentage of Global X position in relation to total global width
    perX = ((pos['x']-p0.pos['x'])/(p1.pos['x'] - p0.pos['x']))
    # Calculate the percentage of Global Y position in relation to total global height
    perY = ((pos['y']-p0.pos['y'])/(p1.pos['y'] - p0.pos['y']))

    # Returns the screen position based on reference points
    return {
        'x': p0.scrX + (p1.scrX - p0.scrX)*perX,
        'y': p0.scrY + (p1.scrY - p0.scrY)*perY
    }


pos = latlngToScreenXY(52.525607, 13.404572);

位置x和位置y包含经纬度的转换x&y坐标(52.525607,13.404572)

我希望这对像我这样的人有帮助,以便正确地解决将lat lng转换为本地参考坐标系的问题。

最好的

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16266809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档