1

im making a gps tracking program and i receive an sms message with the following content

smsMessage="http://maps.google.com/maps?q=42.396068,13.45201,17 phone is outside the area"

Could anybody be so kind and tell me how i can split out only the needed longitude and the latitude from the sms string ?

thanx in advance

Frank
  • 2,478
  • 2
  • 12
  • 19

2 Answers2

0

You need regular expression to get your latitude and longitude from the given text. Here it is:

[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)  

Matches

  • +90.0, -127.554334 45, 180
  • -90, -180
  • -90.000, -180.0000
  • +90, +180
  • 47.1231231, 179.99999999

Doesn't Match

  • -90., -180.
  • +90.1, -100.111
  • -91, 123.456
  • 045, 180

Source: Regular expression for matching latitude/longitude coordinates?

Community
  • 1
  • 1
An SO User
  • 24,046
  • 31
  • 127
  • 207
0

You could do like this,

String s = "http://maps.google.com/maps?q=42.396068,13.45201,17 phone is outside the area";
String parts[] = s.replaceAll("^.*?=|\\s.*", "").split(",(?=[-+]?\\d+\\.\\d+)");
System.out.println(Arrays.toString(parts));

output:

[42.396068, 13.45201,17]
Avinash Raj
  • 166,785
  • 24
  • 204
  • 249