Bora
Bora

Reputation: 1925

Convert Latitude and Longitude Values (Degrees) to Double . Java

I am trying to convert latitude and longitude values which are in degrees to double. the values are like this

 "latitude":"25°21 N",
        "longitude":"55°23 E"

When i try to log this in android it is coming like this. enter image description here

What is this "A^" special char there . How it came. Also when i try to save the log it was like 25°21 N

How to convert the degree values to double for latitude and longitude ?

Thanks

Upvotes: 1

Views: 4884

Answers (1)

RamonBoza
RamonBoza

Reputation: 9038

for your current example, you have to parse your input, one time it is parsed assign to that formula.

Parsing the input

Map<String,String> yourMap; //imagine is your input 
                            //"latitude":"25°21 N",
                            //"longitude":"55°23 E"

String latitude = yourMap.get("latitude");
String hour = latitude.split("º")[0];
String minute = latitude.split("º")[1].split(" ")[0];

// This is a very ugly way to parse it, better do with regular expressions, 
// but I'm not an expert on them and cannot figure them.


//Parse result
String hour = "25";
String minute = "21";
String second = "0";

//Formula
double result = Integer.intValue(hour) + 
                Integer.intValue(minute) / 60 + 
                Integer.intValue(second) / 3600;

Upvotes: 5

Related Questions