将GPS坐标(经度和纬度)转换为十进制 [英] Converting GPS coordinates (latitude and longitude) to decimal

查看:677
本文介绍了将GPS坐标(经度和纬度)转换为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从GPS设备获取经度和纬度。他们看起来像21081686N,079030977E



如果我手动将它们转换为21°08'16.86N,79°03'09.77E并检查Google地图,几乎是正确的。



如何在java中将这些值转换并准确地将它们转换为十进制数?任何JAVA库?



谢谢,

解决方案

需要一个库,而只是将字符串分解成它们的组成部分。也许这会有所帮助。请注意我是不是的Java程序员,所以这个语法可能非常。 (也许你的问题重申Java)



小数点后,我认为你的意思是十进制度数而不是ddd.mmss

  //对于Lat / Lon对
公共类LatLonDecimal
{
public float lat = 0.0;
public float lon = 0.0;
}

//转换字符串例如21081686N,079030977Eto Lat / Lon pair
public LatLonDecimal MyClass :: convert(String latlon)
{
String [] parts = latlon.split(,);

LatLonDecimal position = new LatLonDecimal();
position.lat = convertPart(parts [0]);
position.lon = convertPart(parts [1]);
回报头寸;
}

//转换子串例如(角度。长度()<10)
角度=字符串缓冲器(角度)
(角度)21081686N到小数角度
私人浮动MyClass :: convertPart .insert(0,0)。toString();

int deg = Integer.parseInt(angle.substring(0,2));
int min = Integer.parseInt(angle.substring(3,4));
int sec = Integer.parseInt(angle.substring(5,6));
int sub = Integer.parseInt(angle.substring(7,8));
String hem = angle.substring(9);

浮点值= deg + min / 60.0f + sec / 3600.0f + sub / 360000.0f;
float sign =(hem ==S)? -1.0:1.0; //负南半球纬度
返回符号*值;
}


I am getting the latitude and longitude from GPS device. They look like 21081686N,079030977E

If I manually convert them to 21°08'16.86"N, 79°03'09.77"E and check on Google maps, the location is almost correct.

How do I convert these values in java and convert them to decimal accurately? Any JAVA libraries?

Thanks,

解决方案

You shouldn't need a library, but rather just break up the strings into their component parts. Perhaps this will help. Please note I'm not a Java programmer so this syntax could very well be wrong. (Perhaps retag your question to Java)

By decimal I presume you mean decimal degrees and not ddd.mmss

// Object for Lat/Lon pair
public class LatLonDecimal
{
    public float lat = 0.0;
    public float lon = 0.0;
}

// Convert string e.g. "21081686N,079030977E" to Lat/Lon pair
public LatLonDecimal MyClass::convert(String latlon)
{
    String[] parts = latlon.split(",");

    LatLonDecimal position = new LatLonDecimal();
    position.lat = convertPart(parts[0]);
    position.lon = convertPart(parts[1]);
    return position;
}

// Convert substring e.g. "21081686N" to decimal angle
private float MyClass::convertPart(String angle)
{
    while (angle.length() < 10)
        angle = StringBuffer(angle).insert(0, "0").toString();

    int deg = Integer.parseInt( angle.substring(0,2) );
    int min = Integer.parseInt( angle.substring(3,4) );
    int sec = Integer.parseInt( angle.substring(5,6) );
    int sub = Integer.parseInt( angle.substring(7,8) );
    String hem = angle.substring(9);

    float value = deg + min / 60.0f + sec / 3600.0f + sub / 360000.0f;
    float sign = (hem == "S") ? -1.0 : 1.0; // negative southern hemisphere latitudes
    return sign * value;
}

这篇关于将GPS坐标(经度和纬度)转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆