首页 文章

如何检查设备iPhone是否在Xamarin.Forms中设置了App Google Maps?

提问于
浏览
0

我有一个问题是使用Google Map 打开 Map :

我的设备iPhone已经设置了应用谷歌 Map ,但我的应用程序按下按钮链接到 Map 应用程序打开 Map . 设备打开Safari,不要打开Google Map .

我这样试试: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 构建 .

那么,还有其他方法,如何检查设备iPhone在Xamarin.Forms中设置应用谷歌 Map ?

谢谢!

1 回答

  • 0

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

    您可以使用依赖服务从共享代码中调用特定于平台的代码 . 请按照以下步骤创建依赖服务,以检查iPhone是否安装了Google Map :

    Step 1 : 在共享代码中创建一个接口

    public interface IMapService
    {
         bool HasGoogleMapAvailable();
    }
    

    Step 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;
            }
        }
    }
    

    Step 3 : 在共享代码中,您可以使用该依赖关系服务:

    IMapService mapService = DependencyService.Get<IMapService>();
    var isInstalled = mapService.HasGoogleMapAvailable();
    
    Console.WriteLine("Google Map is installed :" + isInstalled);
    

    Step 4 : 实际打开 Map ;您可以使用Device.OpenUri,它将向用户显示弹出窗口,以便从设备上的所有已安装的 Map 应用程序中选择任何一个:

    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);
    

相关问题