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,738
  • 2
  • 14
  • 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,612
  • 35
  • 133
  • 221
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
  • 172,303
  • 28
  • 230
  • 274
  • hi thanx for your answer so the results will be in parts[0] and parts[1| i quess ..i will try this out – Frank Mar 02 '15 at 02:13