Manolis Karamanis
Manolis Karamanis

Reputation: 788

How to build a string with map coordinates?

I use foursquare API and in order to search some places i have to pass as parameter a string with latitude and longitude like this: 37.963790,23.683090. I also want to add some values to latitude and longitude if i want. After trying with doubles and bigDecimals with no success, i tried :

Long lat1 = Long.valueOf("37");
Long lat2 = Long.valueOf("963790");
Long long1 = Long.valueOf("23");
Long long2 = Long.valueOf("683090");
StringBuilder sb = new StringBuilder();
sb.append(lat1);
sb.append(".");
sb.append(lat2);
sb.append(",");
sb.append(long1);
sb.append(".");
sb.append(long2);
String ll = sb.toString();

I have tried all these longs as strings sb.append(lat1.toString()). The string i get in every case seems to be what i want, but the result is not. I don't get an error. If this this parameter is not correctly set you get this error:

Error occured: 
[0m[0m13:25:34,463 INFO  [stdout] (http-/0.0.0.0:8080-1)   code: 400
[0m[0m13:25:34,463 INFO  [stdout] (http-/0.0.0.0:8080-1)   type: param_error
[0m[0m13:25:34,463 INFO  [stdout] (http-/0.0.0.0:8080-1)   detail: ll must be of the form
XX.XX,YY.YY (received java.nio.HeapByteBuffer[pos=0 lim=19 cap=20])

This error appeared when i tried to add utf-8:

ByteBuffer a =Charset.forName("UTF-8").encode(sb.toString());
String ll = a.toString();

Any suggestions?

Upvotes: 1

Views: 320

Answers (1)

abcdef
abcdef

Reputation: 441

Why not just do

String iWant = lat1 + "." + lat2 + "," + long1 + "." + long2

And if you want to format the numbers differently you use the NumberFormat class

Upvotes: 1

Related Questions