Xamarin.iOS上的自定义Google地图标记信息视图

时间:2015-02-13 20:21:42

标签: ios google-maps xamarin

IMapViewDelegate显然不是来自objC的MapViewDelegate的完整C#实现。这禁止访问markerInfoContents委托方法。
我想做这样的事情,允许我自定义contentView布局和点按动作from raywenderlich.com

来自:http://www.raywenderlich.com/81103/introduction-google-maps-ios-sdk-swift

1 个答案:

答案 0 :(得分:3)

我想我可以从头开始解释,在点击标记时添加自定义标记信息窗口。我使用官方Nuget Library中的 Xamarin.Google.iOS.Maps 包在我的Xamarin MVVM Cross Touch项目中添加Google Map。

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

            };
        }

您可以从任何地方调用此SetupMap()方法。例如,在ViewDidLoad或任何你必须添加谷歌地图的地方。

点击或点按标记我们正在创建自定义标记窗口

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

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

以上代码包含在SetupMap方法中。

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

要添加自定义UIView,您可以按照xamarin网站loading an xib or UIView in another ViewController as a sub view enter image description here

中提供的示例进行操作
_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 ();

            };

您可以使用上面的代码段来检测标记信息窗口中的点击