【问题标题】:Xamarin Forms - iOS can't find Google.Maps functions to overrideXamarin Forms - iOS 找不到要覆盖的 Google.Maps 函数
【发布时间】:2018-02-14 12:58:51
【问题描述】:

我正在尝试使用 this Google.Maps nuget 包和示例教程在我的 Xamarin Forms iOS 应用程序中实现 Google 地图。我已经安装了 nuget 包,并在我的项目的 iOS 部分中包含了带有 using Google.Maps 的包,但是当我尝试覆盖 Google.Maps 函数时,我收到一个错误,即找不到这些函数:

MyCoolClass.LoadView() : no suitable method found to override MyCoolClass.ViewWillAppear(bool) : no suitable method found to override

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using CoreGraphics;
using CustomRenderer;
using CustomRenderer.iOS;
using MapKit;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Maps.iOS;
using Xamarin.Forms.Platform.iOS;
using Google.Maps;

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace CustomRenderer.iOS
{

    public class CustomMapRenderer : MapRenderer
    {
        MapView gMapView;

        public override void LoadView()
        {
            base.LoadView();
            CameraPosition camera = CameraPosition.FromCamera(latitude: 37.797865,
                                                   longitude: -122.402526,
                                                   zoom: 6);
            mapView = MapView.FromCamera(CGRect.Empty, camera);
            mapView.MyLocationEnabled = true;

            gMapView = mapView;
    }

...

}

【问题讨论】:

  • Renderer 不会有这些方法,渲染器中的本机控件 (Control) 会。
  • 谢谢 SushiHangover,我记得你过去也来救过我!我对 Xamarin 很陌生,有什么解决方案
  • 为什么要在 Xamarin.Forms.Maps 之上使用 Googe 地图?在 iOS 上?默认情况下,Xamarin.Forms.Maps 使用 iOS 用户希望看到的 iOS 地图。
  • @SteveChadbourne 因为我必须向地图添加额外的功能,例如多边形和其他东西
  • 您可以在原生地图中添加多边形

标签: c# xamarin xamarin.ios xamarin.forms


【解决方案1】:

这是我使用 iOS 地图设置初始位置的 iOS 自定义地图渲染的一部分。

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace Test.iOS.CustomRenderers
{
    public class CustomMapRenderer : MapRenderer
    {
        private CustomMap FormsMap => Element as CustomMap;
        private MKMapView Map => Control as MKMapView;


        protected override void OnElementChanged(ElementChangedEventArgs<View> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                MoveToCenter();
            }
        }

        private void MoveToCenter()
        {
            if (FormsMap != null && FormsMap.MapRegion != null)
            {
                MoveToMapRegion(FormsMap.MapRegion, false);
            }
        }

        public void MoveToMapRegion(MapSpan region, bool animate)
        {
            var locationCoordinate = new CLLocationCoordinate2D(region.Center.Latitude, region.Center.Longitude);

            var coordinateRegion = MKCoordinateRegion.FromDistance(
                locationCoordinate,
                region.Radius.Meters * 2,
                region.Radius.Meters * 2);

            BeginInvokeOnMainThread(() =>
            {
                Map.SetRegion(coordinateRegion, animate);
            });
        }

  ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 2021-05-13
    • 2019-09-08
    • 1970-01-01
    相关资源
    最近更新 更多