-1

I would like to check, If I'm getting the lat and lng from a URL, and it that URL, the lat and lng is known as FLOAT.

In my own code, I declare them as string, which is

    static final String KEY_LATITUDE = "latitude";
    static final String KEY_LONGITUDE = "longitude";

When I pass the lat and lng into my code and display it out as a text view,it will be shown. Now, if I would like to pass the value of lat and lng into google map , do I have to declare them as FLOAT in my code or can I remain as string?

randomize
  • 137
  • 1
  • 2
  • 11
  • What do you mean with 'parse the lat and lng' – ihsan kocak Aug 14 '13 at 02:11
  • meaning, when I declare it as string altho it is float, the value can be display out. Now, if I want to pass the value into google map, do I have to re declare it as float? – randomize Aug 14 '13 at 02:20

4 Answers4

1

If you want to convert the string into float use:

float d=Float.parseFloat(KEY_LATITUDE);

This will convert your string into float and store it in 'd'.

Otherwise if you don't want to perform any operations using them then you may let it remain as String.

Abhishek
  • 874
  • 2
  • 8
  • 23
  • you can pass the exact string to Google maps, it automatically performs the necessary parsing... – Abhishek Aug 14 '13 at 02:40
  • does it mean that, I don't have to change to float as of the URL? – randomize Aug 14 '13 at 02:42
  • previous was wrong; use above line to convert into float and only then you can pass to Google maps. You have to convert string to float. – Abhishek Aug 14 '13 at 02:46
  • static final float KEY_LATITUDE = "latitude"; it ask me to change to string, why is it so? – randomize Aug 14 '13 at 02:47
  • you have defined KEY_LATITUDE as float data type and you are storing string type in it. remove the double quotes and replace latitude with a number value. example: static final float KEY_LATITUDE= 45.215; – Abhishek Aug 14 '13 at 02:49
  • let me explain. In my code, androidXMLParsing.java, this is the place that is used to retrieve the code from URL as a textview. In my another code, singleMenu.java, this is the place where I click on "map" it will link me to google map with the lat and lng declare. I try out the coding, I couldn't change in just singlemenu.java as it is link with androidXMLParsing.java .. if I just in just one place,the other will not shown – randomize Aug 14 '13 at 02:53
  • Can I change if(KEY_LATITUDE.toString().equals("1.29306") && KEY_LONGITUDE.toString().equals("103.856")){ to float? – randomize Aug 14 '13 at 02:56
  • What you have written is correct. You need not change it. But you may also try : if(Float.parseFloat(KEY_LATITUDE)==1.29306 && Float.parseFloat(KEY_LONGITUDE)==103.856){ – Abhishek Aug 14 '13 at 02:57
  • because previous is string. but I need to make at float in order to pass it into google map – randomize Aug 14 '13 at 03:00
  • Here is your answer: float lat=Float.parseFloat(KEY_LATITUDE); float long =Float.parseFloat(KEY_LONGITUDE); if(lat==1.29306 && long==103.856){ Here long and lat are the new variables that store longitude and latitude in float format respectively. and you may pass lat and long to Google map., – Abhishek Aug 14 '13 at 03:05
  • thank you so much, but i'm having force close error.. http://stackoverflow.com/questions/18222649/having-force-close-error-with-open-new-intent-for-google-map – randomize Aug 14 '13 at 03:14
  • you are welcome,please accept this answer if it helped, i l try the other question in the meanwhile... – Abhishek Aug 14 '13 at 03:16
1

Do you need to parse the data for this (e.g. perform some logic on the float values)? If you're just looking to display the latitude and longitude values, you don't need to convert them into floats.

textView.setText(longitudeString);

Is perfectly valid.

Phillip Carter
  • 4,895
  • 16
  • 26
1

It depends. If you are parsing to store them in float datatype, you have to. Like-

float aFloatValue = Float.parseFloat("100"); // 100 here is a string. It can be your lat or lng

The opposite would be-

String aStringValue = Float.toString(100f); // 100 here is a float converting to a string
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
  • hmm, I'm getting the value as a float, how should I declare the value as? because I need to declare the value as float before I'm able to change it to string, right – randomize Aug 14 '13 at 02:23
  • @randomize If you are getting it as float, you can store it as a float like: float myLatValue = yourLatValue; – Sajal Dutta Aug 14 '13 at 02:25
1

if you just want to display lat and long, keep it as String. If you want to do some calculation, like the shortest route or sth, keep it as double.

For my coding style in java, normally I don't use float, but use double. cause when you calculate, it will convert to double and Bigdecimal use double, and ...

But if you can make sure float is enough for precision, definitely float can save you a little bit memory.

What i am doing is building some web app, so double and float doesn't make huge difference. If you doing mobile app or something, consider use float

Seabook
  • 137
  • 3