Android:LatLng对象为String格式

时间:2017-04-11 11:14:35

标签: android coordinate-systems

我正在使用函数place.getLatLng(),我需要它为“31°47'0”N 35°13'0“E”, 这有功能吗?

1 个答案:

答案 0 :(得分:1)

您可以使用位置Api和格式:

从LatLang对象获取纬度和经度,然后传入以下方法 -

private String convert(double latitude, double longitude) {
StringBuilder builder = new StringBuilder();

if (latitude < 0) {
    builder.append("S ");
} else { 
    builder.append("N ");
} 

String latitudeDegrees = Location.convert(Math.abs(latitude), Location.FORMAT_SECONDS);
String[] latitudeSplit = latitudeDegrees.split(":");
builder.append(latitudeSplit[0]);
builder.append("°");
builder.append(latitudeSplit[1]);
builder.append("'");
builder.append(latitudeSplit[2]);
builder.append("\"");

builder.append(" ");

if (longitude < 0) {
    builder.append("W ");
} else { 
    builder.append("E ");
} 

String longitudeDegrees = Location.convert(Math.abs(longitude), Location.FORMAT_SECONDS);
String[] longitudeSplit = longitudeDegrees.split(":");
builder.append(longitudeSplit[0]);
builder.append("°");
builder.append(longitudeSplit[1]);
builder.append("'");
builder.append(longitudeSplit[2]);
builder.append("\"");

return builder.toString();

}