1
votes

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

...

}
1
The Renderer would not have those methods, the native control (Control) within the renderer would. - SushiHangover
Thanks SushiHangover, I remember that you've come to my rescue in the past as well! I am quite new to Xamarin, what would be a solution - Barney Chambers
Why are you trying to use Googe Maps on top of Xamarin.Forms.Maps? on iOS? By default Xamarin.Forms.Maps uses the iOS Map which is what iOS users would expect to see. - Steve Chadbourne
@SteveChadbourne because I have to add extra features such as Polygons and other things to the map - Barney Chambers
You can add polygons to the native maps - Steve Chadbourne

1 Answers

1
votes

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

  ...
}