1

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

Bora
  • 1,933
  • 3
  • 28
  • 54

1 Answers1

5

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;
RamonBoza
  • 8,898
  • 6
  • 36
  • 48
  • yes thanks, before that i have situation to parse the string i am getting . – Bora Oct 17 '13 at 10:02
  • I get string like 25°21 N and when i try to log it is show some special char in between . How it came ? – Bora Oct 17 '13 at 10:03
  • updated the answer to show you, an ugly way to parse your string – RamonBoza Oct 17 '13 at 10:06
  • it show incorrect characters because you are not using UTF-8 when showing them, String has no encoding, you have to define the encoding when retrieving an array from it, like using getBytes method, String.getBytes("UTF-8") – RamonBoza Oct 17 '13 at 10:12
  • If latitude contains 'S' instead of 'N' or if longitude contains 'W' instead of 'E', then result should be multiplied by (-1). – Stanislau Fink Apr 15 '16 at 15:40