From d21386168e20b73f2331ea84ab2f2cd63a9367a1 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 20 Aug 2020 16:38:41 +0200 Subject: [PATCH] Parse coordinates from google url --- .../net/osmand/util/GeoPointParserUtil.java | 26 ++++++++++++++++--- .../osmand/util/GeoPointParserUtilTest.java | 11 ++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/util/GeoPointParserUtil.java b/OsmAnd-java/src/main/java/net/osmand/util/GeoPointParserUtil.java index d560d36332..23eae8ae64 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/GeoPointParserUtil.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/GeoPointParserUtil.java @@ -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,13 +261,32 @@ 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()); - return parseGoogleMapsPath(path, params); + 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")) { diff --git a/OsmAnd-java/src/test/java/net/osmand/util/GeoPointParserUtilTest.java b/OsmAnd-java/src/test/java/net/osmand/util/GeoPointParserUtilTest.java index 206617ab8f..d00b595132 100644 --- a/OsmAnd-java/src/test/java/net/osmand/util/GeoPointParserUtilTest.java +++ b/OsmAnd-java/src/test/java/net/osmand/util/GeoPointParserUtilTest.java @@ -32,6 +32,17 @@ public class GeoPointParserUtilTest { System.out.println(actual); 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() {