Parse coordinates from google url

This commit is contained in:
Victor Shcherb 2020-08-20 16:38:41 +02:00
parent fcf8f94daa
commit d21386168e
2 changed files with 33 additions and 4 deletions

View file

@ -252,7 +252,6 @@ public class GeoPointParserUtil {
}
final String postf = "\\s\\((\\p{L}|\\p{M}|\\p{Z}|\\p{S}|\\p{N}|\\p{P}|\\p{C})*\\)$";
opath = opath.replaceAll(postf, "");
System.out.println("opath=" + opath);
return parseGoogleMapsPath(opath, params);
}
if (fragment != null) {
@ -262,15 +261,34 @@ public class GeoPointParserUtil {
return new GeoParsedPoint(m.group(1));
}
}
String DATA_PREFIX = "/data=";
String[] pathPrefixes = new String[]{"/@", "/ll=",
"loc:", "/"};
"loc:", DATA_PREFIX, "/"};
for (String pref : pathPrefixes) {
if (path.contains(pref)) {
path = path.substring(path.lastIndexOf(pref) + pref.length());
if (path.contains("/")) {
path = path.substring(0, path.indexOf('/'));
}
if (pref.equals(DATA_PREFIX)) {
String[] vls = path.split("!");
String lat = null;
String lon = null;
for (String v : vls) {
if (v.startsWith("3d")) {
lat = v.substring(2);
} else if (v.startsWith("4d")) {
lon = v.substring(2);
}
}
if (lat != null && lon != null) {
return new GeoParsedPoint(Double.valueOf(lat), Double.valueOf(lon));
}
} else {
return parseGoogleMapsPath(path, params);
}
}
}
} else if (host.endsWith(".amap.com")) {
/* amap (mis)uses the Fragment, which is not included in the Scheme Specific Part,
* so instead we make a custom "everything but the Authority subString */

View file

@ -33,6 +33,17 @@ public class GeoPointParserUtilTest {
assertGeoPoint(actual, new GeoParsedPoint(46.853582, 9.529903));
}
@Test
public void testGoogleMapsData() {
// https://www.google.com/maps?daddr=Bahnhofplatz+3,+7000+Chur@46.853582,9.529903
GeoParsedPoint actual = GeoPointParserUtil.parse(
"https://www.google.co.in/maps/place/10%C2%B007'16.8%22N+76%C2%B020'54.2%22E/@10.1213253,76.3478427,247m/data=!3m2!1e3!4b1!4m6!3m5!1s0x0:0x0!7e2!8m2!3d10.1213237!4d76.348392?shorturl=1");
assertGeoPoint(actual, new GeoParsedPoint(10.1213253, 76.3478427));
actual = GeoPointParserUtil.parse(
"https://www.google.co.in/maps/place/data=!3m2!1e3!4b1!4m6!3m5!1s0x0:0x0!7e2!8m2!3d10.1213237!4d76.348392?shorturl=1");
assertGeoPoint(actual, new GeoParsedPoint(10.1213237, 76.348392));
}
@Test
public void testGeoPoint() {
final int ilat = 34, ilon = -106;