【问题标题】:How to check Device iPhone has set up App Google Maps in Xamarin.Forms?如何检查设备 iPhone 是否在 Xamarin.Forms 中设置了 App Google Maps?
【发布时间】:2019-02-11 20:51:02
【问题描述】:

我有一个问题是用谷歌地图打开地图:

我的设备 iPhone 已设置 App Google Maps,但我的应用在按下按钮时会链接到地图应用以打开地图。设备打开 Safari,不要打开 Google 地图。

我试试这个方法:Xamarin Forms - Maps - Is possible to call Google Maps from a button?

但在行代码:

var canOpenNative = UIApplication.SharedApplication.CanOpenUrl(NSUrl.FromString("comgooglemaps-x-callback://"));

我不能使用库:`using Foundation;

(构建应用程序将无法使用库。)

我尝试这种方式:Xamarin.Forms - 'Foundation' could not be found

但是在构建应用程序时,我无法使用库 Foundation 构建。

那么,还有其他方法,How to check Device iPhone setups App Google Maps in Xamarin.Forms?

谢谢!

【问题讨论】:

  • 您可以通过 Forms 的依赖服务调用原生代码:docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…
  • 是否已将 Google 地图设置为安装在设备上或设置为默认地图应用程序?
  • @Adriani6 我想检查设备 (iPhone) 是否已设置 Google 地图。如果设备没有应用谷歌地图,设备将打开地图到 Safari 或其他应用地图。

标签: c# .net xamarin xamarin.forms


【解决方案1】:

如果您尝试从共享代码中使用 Foundation 程序集,那么您只能从您的 iOS 平台特定项目中访问它。

您可以使用依赖服务从共享代码中调用平台特定代码。按照以下步骤创建依赖服务以检查 iPhone 是否安装了谷歌地图:

第 1 步:在您的共享代码中创建一个接口

public interface IMapService
{
     bool HasGoogleMapAvailable();
}

第 2 步: 现在在您的平台特定项目中;现在它是你的 iOS 项目。创建一个将实现您创建的接口的服务:

[assembly: Dependency(typeof(MapService))]
namespace WorkingWithMaps.iOS
{
    public class MapService:IMapService
    {
        public MapService()
        {
        }

        public bool HasGoogleMapAvailable()
        {
            var result=UIApplication.SharedApplication.CanOpenUrl(NSUrl.FromString("comgooglemaps-x-callback://"));
            return result;
        }
    }
}

第 3 步:在您的共享代码中,您可以使用该依赖服务:

IMapService mapService = DependencyService.Get<IMapService>();
var isInstalled = mapService.HasGoogleMapAvailable();

Console.WriteLine("Google Map is installed :" + isInstalled);

第四步:真正打开地图;您可以使用 Device.OpenUri 向用户显示弹出窗口以从设备上所有已安装的地图应用程序中选择任何一个:

var uri = new Uri("http://maps.google.com/maps?saddr=Google+Inc,+8th+Avenue,+New+York,+NY&daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York&directionsmode=transit");
Device.OpenUri(uri);

【讨论】:

    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 2017-12-04
    • 2012-11-03
    • 1970-01-01
    相关资源
    最近更新 更多