Skip to content

Commit

Permalink
FMD-183 fix geo URI for different platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
robo-w committed Dec 30, 2019
1 parent 4b27670 commit 7c325e0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 26 deletions.
6 changes: 1 addition & 5 deletions GeoClient/GeoClient/Services/Utils/PrerequisitesChecking.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace GeoClient.Services.Utils
namespace GeoClient.Services.Utils
{
public delegate bool BooleanDelegate();

Expand Down
41 changes: 35 additions & 6 deletions GeoClient/GeoClient/Views/ItemDetailPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using GeoClient.ViewModels;
using GeoClient.Views.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
Expand Down Expand Up @@ -84,11 +85,11 @@ private async void OpenLocation_Clicked(object sender, EventArgs e)
{
var item = _viewModel.IncidentItem;

var geoUri = GeoPointUtil.CreateGeoUri(item.Location, "GeoClient: Berufungsort");
var uriList = GeoPointUtil.CreateUrisForGeoPoint(item.Location, "GeoClient: Berufungsort");

if (geoUri != null)
if (uriList != null)
{
await Launcher.OpenAsync(geoUri);
await OpenFirstSupportedUri(uriList);
}
else
{
Expand All @@ -100,24 +101,52 @@ private async void OpenDestination_Clicked(object sender, EventArgs e)
{
var item = _viewModel.IncidentItem;

var geoUri = GeoPointUtil.CreateGeoUri(item.Destination, "GeoClient: Zielort");
var uriList = GeoPointUtil.CreateUrisForGeoPoint(item.Destination, "GeoClient: Zielort");

if (geoUri != null)
if (uriList != null)
{
await Launcher.OpenAsync(geoUri);
await OpenFirstSupportedUri(uriList);
}
else
{
await ShowGeoUriNotAvailableError();
}
}

private async Task OpenFirstSupportedUri(List<Uri> uriList)
{
bool uriOpened = false;
foreach (Uri uri in uriList)
{
var uriSupported = await Launcher.CanOpenAsync(uri);
if (uriSupported)
{
await Launcher.OpenAsync(uri);
uriOpened = true;
break;
}
}

if (!uriOpened)
{
await ShowNoAppForGeoUriError();
}
}

private async Task ShowGeoUriNotAvailableError()
{
await DisplayAlert(
"Adresse nicht verortet",
"Die Adresse konnte leider nicht automatisch verortet werden.",
"OK");
}

private async Task ShowNoAppForGeoUriError()
{
await DisplayAlert(
"Keine passende App gefunden",
"Es konnte keine passende App für den Link gefunden werden. Bitte installiere ein Kartenprogramm wie z.B. Google Maps.",
"OK");
}
}
}
74 changes: 59 additions & 15 deletions GeoClient/GeoClient/Views/Utils/GeoPointUtil.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,85 @@
using System;
using GeoClient.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using GeoClient.Models;
using Xamarin.Forms;

namespace GeoClient.Views.Utils
{
public class GeoPointUtil
{
public static Uri CreateGeoUri(GeoPoint geoPoint, string geoPointerTag)
public static bool NotBlank(string inputString)
{
return inputString != null && inputString.Trim() != "";
}

public static List<Uri> CreateUrisForGeoPoint(GeoPoint geoPoint, string geoPointerTag)
{
Uri geoUri = null;
List<Uri> uriList = null;

if (GeoPointUtil.IsGeoPointValid(geoPoint))
{
var request = string.Format(
"geo:{0},{1}?q={0},{1}({2})",
geoPoint.Latitude.ToString(CultureInfo.InvariantCulture),
geoPoint.Longitude.ToString(CultureInfo.InvariantCulture),
HttpUtility.UrlEncode(geoPointerTag));
geoUri = new Uri(request);
uriList = new List<Uri>();
string latitudeString = geoPoint.Latitude.ToString(CultureInfo.InvariantCulture);
string longitudeString = geoPoint.Longitude.ToString(CultureInfo.InvariantCulture);

uriList.Add(CreateGeoUri(latitudeString, longitudeString, geoPointerTag));
if (IsRunningOnIOS())
{
uriList.Add(CreateAppleMapsUri(latitudeString, longitudeString, geoPointerTag));
uriList.Add(CreateGoogleMapsUri(latitudeString, longitudeString));
} else
{
uriList.Add(CreateGoogleMapsUri(latitudeString, longitudeString));
uriList.Add(CreateAppleMapsUri(latitudeString, longitudeString, geoPointerTag));
}
}
else
{
Console.WriteLine("Cannot create geo URI for geo point: " + geoPoint);
}

return geoUri;
return uriList;
}

private static bool IsGeoPointValid(GeoPoint geoPoint)
private static Uri CreateGeoUri(string latitudeString, string longitudeString, string geoPointerTag)
{
return geoPoint != null;
var uriString = string.Format(
"geo:{0},{1}?q={0},{1}({2})",
latitudeString,
longitudeString,
HttpUtility.UrlEncode(geoPointerTag));
return new Uri(uriString);
}

public static bool NotBlank(string inputString)
private static Uri CreateGoogleMapsUri(string latitudeString, string longitudeString)
{
return inputString != null && inputString.Trim() != "";
var uriString = string.Format(
"https://www.google.com/maps/search/?api=1&query={0},{1}",
latitudeString,
longitudeString);
return new Uri(uriString);
}

private static Uri CreateAppleMapsUri(string latitudeString, string longitudeString, string geoPointerTag)
{
var uriString = string.Format(
"http://maps.apple.com/?ll={0},{1}&q={2}",
latitudeString,
longitudeString,
HttpUtility.UrlEncode(geoPointerTag));
return new Uri(uriString);
}

private static bool IsRunningOnIOS()
{
return Device.RuntimePlatform == Device.iOS;
}

private static bool IsGeoPointValid(GeoPoint geoPoint)
{
return geoPoint != null;
}
}
}

0 comments on commit 7c325e0

Please sign in to comment.