Custom Google Maps marker info view on Xamarin.iOS

半世苍凉 提交于 2021-02-08 03:43:16

问题


The IMapViewDelegate apparently isn't a complete C# implementation of MapViewDelegate from objC. That inhibits the access to markerInfoContents delegate method.
I'd like to do something like this allowing me to custom the contentView layout and the tap action from raywenderlich.com

got from: http://www.raywenderlich.com/81103/introduction-google-maps-ios-sdk-swift


回答1:


I think i can explan its from scratch to add a Custom MarkerInfo Window on the Tap of a Marker. Iam using Xamarin.Google.iOS.Maps package from Official Nuget Library for adding Google Map on my Xamarin MVVM Cross Touch Project.

private void SetupMap()
            {
                if (_mapView != null)
                    _mapView.RemoveFromSuperview ();

            //Init Map wiht Camera
            var camera = new CameraPosition(new CLLocationCoordinate2D(36.069082, -94.155976), 15, 30, 0);
            _mapView = MapView.FromCamera(RectangleF.Empty, camera);
            _mapView.MyLocationEnabled = true;
            //Add button to zoom to location
            _mapView.Settings.MyLocationButton = true;
            _mapView.MyLocationEnabled = true;
            _mapView.Settings.SetAllGesturesEnabled(true);

            var xamMarker = new Marker () {
                Title = "Sample",
                Snippet = "Sample Location",
                Position = new CLLocationCoordinate2D (36.069082, -94.155976),
                Map = _mapView
            };
            var xamMarker1 = new Marker () {
                Title = "Sample1",
                Snippet = "Sample Location2",
                Position = new CLLocationCoordinate2D (35.069082, -94.155976),
                Map = _mapView
            };

            _mapView.TappedMarker = (map, marker) => {
                Console.WriteLine("Marker tapped:"+ map +"::"+marker);

                _mapView.MarkerInfoWindow = new GMSInfoFor(markerInfoWindow);
                return false;
            };
            _mapView.Frame = this.contentViewOutlet.Bounds;
            this.contentViewOutlet.AddSubview (_mapView);

            _mapView.InfoTapped += (object sender, GMSMarkerEventEventArgs e) => {
                Console.WriteLine ("Marker Info tapped:"+e+"::"+sender);

                UIAlertView alert = new UIAlertView () { 
                    Title = "Alert", Message = sampleLongandLat
                };
                alert.AddButton("OK");
                alert.Show ();

            };
        }

you can call this SetupMap() method from where ever you want. for example in ViewDidLoad or from any where you have to add the google map.

On click or Tap of the marker we are creating a custom marker window

_mapView.TappedMarker = (map, marker) => {
                Console.WriteLine("Marker tapped:"+ map +"::"+marker);

                _mapView.MarkerInfoWindow = new GMSInfoFor(markerInfoWindow);
                return false;
            };

the above code is included in the SetupMap method.

mapView.MarkerInfoWindow = new GMSInfoFor(markerInfoWindow); these above line of code will allow us to load a custom Marker Window instead of the default one

    UIView markerInfoWindow(UIView view, Marker marker)
            {
// use this method to return the custom view u have already created to load as a subview in Google Map as Custom Marker Info Windo
                UIView v;
                v = MarkerInfoView.Create(marker);
                sampleLongandLat = MarkerInfoView.markerInfoString;
                sampleLongandLat = MarkerInfoView.locationIDString;
                return v;
            }

for adding an custom UIView you can follow the example provided in the xamarin web site loading an xib or UIView in another ViewController as a sub view

_mapView.InfoTapped += (object sender, GMSMarkerEventEventArgs e) => {
                Console.WriteLine ("Marker Info tapped:"+e+"::"+sender);

                UIAlertView alert = new UIAlertView () { 
                    Title = "Alert", Message = sampleLongandLat
                };
                alert.AddButton("OK");
                alert.Show ();

            };

the above code snippet you can use for detecting tap on the Marker Info Window



来源:https://stackoverflow.com/questions/28508125/custom-google-maps-marker-info-view-on-xamarin-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!