Barney Chambers
Barney Chambers

Reputation: 2783

Xamarin Forms - iOS can't find Google.Maps functions to override

I am trying to use this Google.Maps nuget package and example tutorial to implement Google Maps in my Xamarin Forms iOS application. I have installed the nuget package and included the package with using Google.Maps in the iOS section of my project, but when I try to override Google.Maps functions, I am receiving an error that these functions cannot be found:

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

Here is my code:

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

...

}

Upvotes: 1

Views: 355

Answers (1)

Steve Chadbourne
Steve Chadbourne

Reputation: 6953

Here is part of my custom map render for iOS using the iOS map setting an initial location.

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

  ...
}

Upvotes: 1

Related Questions