如何在Xamarn.forms中显示地图的全局视图(3D视图)

时间:2017-07-18 08:04:21

标签: xamarin xamarin.forms

我们需要在Android,iOS和&amp ;;的3D地图上显示选定的城市。 Windows.UWP使用Xamarin.Forms。目前我们正在使用Xamarin.Forms.Maps,但它只显示地球的2D地图。

我们如何选择地球的3D地图?

注意:我们还需要放大地图功能。

1 个答案:

答案 0 :(得分:1)

如果你想要Google Earth之类的东西,你可能需要创建自己的实现。但是,您在Xamarin Forms中可以使用现有的Xamarin.Forms.Maps控件并添加称为相机的内容。基本上这是一个观察地图的视点。这些可以在3D空间中,因此看起来您有3D地图。您可以使用自定义渲染器创建它。

在这些自定义渲染器中,您会遇到俯仰,航向和距离等问题。此图片显示了什么:

enter image description here

iOS自定义渲染器

[assembly: ExportRenderer(typeof(Map3d), typeof(MapView3dRenderer))]
namespace MyApp.iOS.Renderers
{
    public class MapView3dRenderer : MapRenderer
    {
        MKMapView _nativeMap;

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

            if (e.NewElement != null && Control != null)
            {
                _nativeMap = Control as MKMapView;
            }
        }

        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (_nativeMap == null)
                return;

            if (e.PropertyName == "VisibleRegion")
                UpdateCameraView();
        }

        void UpdateCameraView()
        {
            var target = new CLLocationCoordinate2D(50.890119f, 5.857798f);

            //Enable 3D buildings
            _nativeMap.ShowsBuildings = true;
            _nativeMap.PitchEnabled = true;

            // Attach the camera
            var camera = MKMapCamera.CameraLookingAtCenterCoordinate(target, 650, 60, 0);
            _nativeMap.Camera = camera;
        }
    }
}

<强>的Android

对于Android,我没有准备好自定义渲染器,但您应该能够弄明白。它还涉及附加Camera对象。这次您将其添加到GoogleMap

的实例中
// Create the camera
CameraPosition cameraPosition = new CameraPosition.Builder()
                                                  .Target(location)
                                                  .Tilt(45)
                                                  .Zoom(10)
                                                  .Bearing(0)
                                                  .Build();
// Convert to an update object
CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition);

// Attach the camera
map.MoveCamera(cameraUpdate); // map is of type GoogleMap

查看有关其工作原理的Android docs