Android Question Extract lat lon gps coordonates

hookshy

Well-Known Member
Licensed User
Longtime User
I somehow find it very difficult to get the gps coordonates from a string ...

Example :

<My adres is ... Street 24, Oxford 33 please find me here are my coord 45.3456786,34.98485885>

if I try to cut the text to number and "." and "," ..I will get something like ...24,3345.3456786,34.98485885
and it not an easy task ..regex.split does not help much in this case exception
Any ideas ?
 

derez

Expert
Licensed User
Longtime User
B4X:
Dim st, str(), res() As String
st = "My adress is ... Street 24, Oxford 33 please find me here are my coord 45.3456786,34.98485885"
str = Regex.Split(" ",st)
st = str(str.Length-1)
res = Regex.Split(",",st)
Log(res(0) & " " & res(1))
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Derez,

if I correctly read your code, you assume the lat/lon data follows a space in the original string and there's no further space between lat and lon.
Another possible solution, using only one array, could be:
B4X:
Dim st, st2, res() AsString
st = "My adress is ... Street 24, Oxford 33 please find me here are my coord 45.3456786,34.98485885"
st2 = st.substring(st.LastIndexOf(" ")+1) 'returns 45.3456786,34.98485885
res = Regex.Split(",",st2)
Log(res(0) & " " & res(1))

udg
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
Derez you provided me some usefull information ... I do find the regex.split by " " a excellent idea ...

My question is very usefull because not much sms activities knows to parse and interpret gps coordonate...as you may receive sms with your frineds locations it is easy to navigate to them ..as telephone numbers are interpreted and html tags...



udg ...Please note that your method will not work in all cases ..the example I have put int this thread is particular ...all other app in the market could send the gps coordonate in their own stile ...
EX: ... (45.3456786,34.98485885) please find me now


For example you may find
(45.3456786,34.98485885)
in this case you must add a code to keep numbers , points ,and comma..after using derez code

Thank you guys!
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi hookshy,

fully agree with you about my solution being not general; in fact it was based on the example given.
Anyway, a more general "numbers only" extracting solution could be:
B4X:
Dim pattern, st as string
st= "(45.3456786,34.98485885) please find me now" 'or the one from first post
pattern="[0-9][0-9.]*"
Dim m As Matcher
m = Regex.Matcher(pattern, st)
Pattern above will match any sequence of digits and decimal points where first char in sequence is a digit (so 1.23 pass but .23 fails)
So your example string in post#1 will return --> 24 33 45.3456786 34.98485885
while example string in post #4 will return --> 45.3456786 34.98485885

udg
 
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
B4X:
Dim text As String = "My adress is ... Street 24, Oxford 33 please find me here are my coord 45.3456786,34.98485885"
Dim coords As Matcher = Regex.Matcher("([+-]?\d+\.\d+),([+-]?\d+\.\d+)", text)
If coords.Find Then
    Log(coords.Group(1))
    Log(coords.Group(2))
End If
 
Upvote 0
Top