How to add to a gps coordinate mathematicaly?

mistermentality

Active Member
Licensed User
Longtime User
A simple question maybe but one which trial and error is not helping me figure out.

I'm working on a gps app and I use the following code (where the variables are strings) to get the location from the gps sensor:

B4X:
Latitude = Location1.Latitude
Longitude = Location1.Longitude

It works fine getting the current coordinates, the problem I am having is that I need to take the longitude and add a small amount to it to make a second longitude variable so that the app shows the current gps position with a marker and then a second marker using the current latitude and newly calculated second longitude as the second coordinate set.

My current code (I have tried lots of alternatives) to do this is:

B4X:
If IsNumber(Longitude) Then LongTest = Longitude - 0.0000001

LongTest is a float variable. However instead of taking a coordinate like -0.0995281 and making it equal 0.0995280 it ends up making a much longer number sometimes with an E in it and I am puzzled as to where I am going wrong. So at the risk of embarrassing myself can someone point out my error for me, please?

Thanks.

Dave
 

mistermentality

Active Member
Licensed User
Longtime User
I have tried it as numbers instead of strings and what happens is that coordinates such as -0.0995281 become twice as long, nothing I have tried will simply add that small amount without making the number inordinantly longer.

I have just rewritten the code to try and use numeric variables but as before it has the same effect so I will rewrite and try your suggestion. Thanks.

Dave
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
Okay I am really puzzled now, have tried using numbers instead of strings, but regardless of what type of numeric variable I use I find that I cannot make a coordinate like -0.0995281 become one digit less such as -0.0995280.

As I do have memory problems I may have missed something but I have tried using numeric variables and removing 1, 0.0000001, 0.001 etc and still the number gets just much much longer. All I want is to take the number and make the last digit of the coordinate 1 less than it is but I don't see how to achieve that.

So using strings I thought of using regex to split the string and that way I could find the number string that follows the first "." in the coordinate and do something with that using number format.

But I use the code

Dim components() As String
components = Regex.Split(".", Longitude)
Log("Longitude = " & Longitude)

with the example number string of "0.0995281"

But components(0) never has any content nor does content(1) and if I log the value of components the log returns the following line:

components= [Ljava.lang.String;@2fa4e648

I cannot see why the above code is not splitting the Latitude variable (which is a string) so that components(0) contains "0" and components(1) contains "0995281". Why is this happening?

Dave
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
I understand what you mean, and tried numberformat but couldn't seem to get it shortening as the number still stayed longer even when I tried a very low number of fractions such as 2.

So I decided to set about trying a different method which would involve splitting the string variable holding the coordinate so that the numbers left of the decimal were in components(0) and those right of it in components(1) using the code:

B4X:
Dim components() As String
components = Regex.Split(".", Longitude)
Log("Longitude = " & Longitude)


with the example number string of "0.0995281". However even this simple code fails because even though the variable Longitude is a string and definitely contains a "." character and numbers the code refuses to populate the components variable even if I replace the variable name with actual text in quotes such as "12.34" and so I get an array out of bounds error for components(0) when I try to access it but I don't see why the above code fails as I have checked the docs and as far as I can see it is correct?

Dave
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
As "." is a Regex metacharacter it needs escaping to use it in a string expression
B4X:
Dim components(0) As String
components = Regex.Split("\.", "0.0995281")
Msgbox(components(0) & " " & components(1),"Result")
Other than that I am totally failing to understand why you can't use NumberFormat to get the length of string you want. The lack of an example doesn't help.
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
Thank you, that solved the problem. I can't remember exactly what variations of options I tried with NumberFormat but I took your advice on adding the \ character with the regex split command and that works great. Thank you

Dave
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…