我正在完成一项任务,并在某一时刻陷入困境。
我收到一个带有一系列电台的.txt
文件,我应该使用给我的GPS坐标来绘制电台名称,但是我无法将这些坐标转换为XY坐标{{{ 1}}使用。此外,GPS坐标以JPanel
s写入,但double
s中的XY坐标。
我怎样才能做到这一点?
这里的代码不起作用。
int
我试图使用以下方式绘制它们:
void extents() {
minLong = Double.MAX_VALUE;
minLat = Double.MAX_VALUE;
maxLong = -Double.MIN_VALUE;
maxLat = -Double.MIN_VALUE;
for(Station station : stations){
if(station.getLatitude()>maxLat){
maxLat = station.getLongitude();
maxLong = station.getLongitude();
}
if(station.getLatitude()<minLat){
minLat = station.getLongitude();
minLong = station.getLongitude();
}
if(station.getLongitude()>maxLong){
maxLong = station.getLongitude();
}
if(station.getLongitude()<minLong){
minLong = station.getLongitude();
}
}
我现在为每个电台获得不同的坐标,但是它们仍然太近,这意味着它们没有缩放到 for (int i = 0; i < stations.size(); i++) {
double scalingFactorx = width/(maxLat - minLat);
double scalingFactory = height/(maxLong - minLong);
double scalingFactor = Math.min(scalingFactorx, scalingFactory);
double drawingX = stations.get(i).getLatitude() * scalingFactor;
double drawingY = stations.get(i).getLongitude() * scalingFactor;
double drawingY2 = height - drawingY;
gr.drawString(stations.get(i).getName(), (int)drawingX/100, (int)drawingY2/200);
}
。
谢谢!
答案 0 :(得分:2)
嗯,首先,您应该将最小值和最大值设为实际的最小值和最大值:
double minLong = Double.MAX_VALUE;
double minLat = Double.MAX_VALUE;
double maxLong = -Double.MIN_VALUE;
double maxLat = -Double.MIN_VALUE;
for(Station station : stations){
if(station.getLatitude()>maxLat){
maxLat = station.getLongitude();
maxLong = station.getLongitude();
}
if(station.getLatitude()<minLat){
minLat = station.getLongitude();
minLong = station.getLongitude();
}
if(station.getLongitude()>maxLong){
maxLong = station.getLongitude();
}
if(station.getLongitude()<minLong){
minLong = station.getLongitude();
}
}
然后你可以在你的绘制方法中做到这一点:
for (int i = 0; i < stations.size(); i++) {
double scalingFactorx = width/(maxLat - minLat);
double scalingFactory = height/(maxLong - minLong);
double scalingFactor = Math.min(scalingFactorx, scalingFactory);
double drawingX = (stations.get(i).getLatitude() - minLat)/(maxLat-minLat);
double drawingY = (stations.get(i).getLongitude() - minLong)/(maxLong-minLong);
//double drawingY2 = height - drawingY; This line isn't needed. It causes the text to be drawn outside the jpanel.
gr.drawString(stations.get(i).getName(), (int)drawingX, (int)drawingY);
}
现在,它的作用是找到最右边和最左边的站点以及最高和最低位置,然后根据需要调整所有内容。
答案 1 :(得分:0)
for (int i = 0; i < stations.size(); i++) {
extents();
scalingFactorx = width/(maxLat - minLat);
scalingFactory = height/(maxLong - minLong);
scalingFactor = Math.min(scalingFactorx, scalingFactory);
drawingX = (stations.get(i).getLatitude() - minLat) * scalingFactorx;
drawingY = (stations.get(i).getLongitude() - minLong) * scalingFactory;
drawingY2 = height - drawingY;
gr.drawString(stations.get(i).getName(), (int)drawingX, (int)drawingY2);
这样的事情应该解决它。