Skip to content

Commit

Permalink
add support for Google-maps urls
Browse files Browse the repository at this point in the history
  • Loading branch information
ntruchsess committed Feb 16, 2020
1 parent c2c09ff commit 7978dad
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.iml
.gradle
/local.properties
/.idea
/app/build
/app/release
/build
1 change: 1 addition & 0 deletions app/src/main/assets/nominatim.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cafe Cafe
car_rental Autovermietung
car_wash Autowaschanlage
casino Spielkasino
charging_station Ladestation
cinema Kino
clinic Krankenhaus
club Klub
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/com/truchsess/send2car/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,9 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin

if (Intent.ACTION_VIEW.equals(action)) {
final Uri uri = intent.getData();
final String scheme = uri.getScheme();
if (scheme.equals("geo") || scheme.equals("google.navigation")) {
mServiceMessageController.setGeoUrlFromUri(uri);
}

mServiceMessageController.setGeoUrlFromUri(uri);

final GeoUrl geoUrl = mServiceMessageController.getGeoUrl();

if (mGeoFragment != null) {
Expand Down
48 changes: 39 additions & 9 deletions app/src/main/java/com/truchsess/send2car/geo/GeoUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.regex.Pattern;

/**********************************************************************************************
Copyright (C) 2018 Norbert Truchsess [email protected]
Copyright (C) 2020 Norbert Truchsess [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -37,34 +37,45 @@ public GeoUrl(final double lat, final double lon, final String description) {
this.description = description;
}

public void fromUri(Uri uri) {
public void fromUri(final Uri uri) {

String data = uri.getSchemeSpecificPart();
final String scheme = uri.getScheme();

if (scheme.equals("geo") || scheme.equals("google.navigation")) {
fromGeoUri(uri);
} else if (scheme.equals("http") || scheme.equals("https")) {
fromHttpUri(uri);
}
}

public void fromGeoUri(final Uri uri) {

final String data = uri.getSchemeSpecificPart();

// format is:
// <lat>,<lon>?z=<zoom>
// 0,0?q=<lat>,<lon>(label)

int qpos = data.indexOf("?");
String resource = qpos < 0 ? data : data.substring(0, qpos);
String query = data.substring(qpos+1);
final String resource = qpos < 0 ? data : data.substring(0, qpos);
final String query = data.substring(qpos+1);

Matcher resMatcher = Pattern.compile("^(\\d++\\.?\\d*),(\\d++\\.?\\d*)$").matcher(resource);
final Matcher resMatcher = Pattern.compile("^(\\d++\\.?\\d*),(\\d++\\.?\\d*)$").matcher(resource);
boolean resMatches = resMatcher.matches();

lat = resMatches ? Double.parseDouble(resMatcher.group(1)) : Double.NaN;
lon = resMatches ? Double.parseDouble(resMatcher.group(2)) : Double.NaN;

description = null;

StringTokenizer queryTokenizer = new StringTokenizer(query,"&");
final StringTokenizer queryTokenizer = new StringTokenizer(query,"&");
while (queryTokenizer.hasMoreTokens()) {
final String nextToken = queryTokenizer.nextToken();
Matcher paramMatcher = Pattern.compile("^q=(.*)$",Pattern.DOTALL).matcher(nextToken);
final Matcher paramMatcher = Pattern.compile("^q=(.*)$",Pattern.DOTALL).matcher(nextToken);
if (paramMatcher.matches()) {
final String q0 = paramMatcher.group(1);
final String q1 = q0.replaceAll("[\\n\\r\\t\\f]",", ");
Matcher locationMatcher = Pattern.compile("^(\\d++\\.?\\d*),(\\d++\\.?\\d*)(\\((.*)\\)|)+$").matcher(q1);
final Matcher locationMatcher = Pattern.compile("^(\\d++\\.?\\d*),(\\d++\\.?\\d*)(\\((.*)\\)|)+$").matcher(q1);
if (locationMatcher.matches()) {
lat = Double.parseDouble(locationMatcher.group(1));
lon = Double.parseDouble(locationMatcher.group(2));
Expand All @@ -77,6 +88,25 @@ public void fromUri(Uri uri) {
}
}

public void fromHttpUri(final Uri uri) {
// formats of google-maps uris:
// https://www.google.com/maps?q=<description>&ll=<lat>,<lon>
// https://maps.google.com/?q=<description>&ll=<lat>,<lon>
// https://www.google.com/maps/@<lat>,<lon>,<zoom>z

if (uri.getHost().contains("google")) {
description = uri.getQueryParameter("q");
final String ll = uri.getQueryParameter("ll");
if (ll != null && !ll.isEmpty()) {
final Matcher locationMatcher = Pattern.compile("^(\\d++\\.?\\d*),(\\d++\\.?\\d*)$").matcher(ll);
if (locationMatcher.matches()) {
lat = Double.parseDouble(locationMatcher.group(1));
lon = Double.parseDouble(locationMatcher.group(2));
}
}
}
}

public boolean isValid() {
return !Double.isNaN(lat) && !Double.isNaN(lon);
}
Expand Down

0 comments on commit 7978dad

Please sign in to comment.