How to display globe view (3D view) of Maps in Xamarn.forms - xamarin

We need to show selected cities on 3D map of earth in Android, iOS & Windows.UWP using Xamarin.Forms. Currently we are using Xamarin.Forms.Maps, but it only shows 2D map of earth.
How do we go for 3D map of earth?
Note: We will require zoom-in feature of map as well.

If you want something like Google Earth you're probably going to have to create your own implementation. What you can do in Xamarin Forms however is use the existing Xamarin.Forms.Maps controls and add what's called a camera. Basically this is a viewpoint from which you look upon the map. These can be in 3D space so it looks like you have a 3D map. You can create this using custom renderers.
Within these custom renderers you will come across things like pitch, heading and distance. This image show what's what:
iOS custom renderer
[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
For Android I don't have a custom renderer ready but you should be able to figure it out. It also involves attaching a Camera object. This time you add it to an instance of 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
Check out the Android docs on how this works.

Related

Xamarin Native map point open infowindow by default

Im loading a native map application (android,IOS) with a custom pin location and with a custom marker.What i need is when i load the map i need to open the map pin marker info window by default.
currently what is happening is when user tap the pin info then only the pin info window shows up.
I have seen some code there markerOpt1.showInfoWindow() there is no method like this.
As per the documentation i know only one info window can pops up.In my scenario only one pin is there
Android implementation
public void OnMapReady(GoogleMap map)
{
MarkerOptions markerOpt1 = new MarkerOptions();
markerOpt1.SetPosition(new LatLng(50.379444, 2.773611));
markerOpt1.SetTitle("Vimy Ridge");
var bmDescriptor = BitmapDescriptorFactory.DefaultMarker (BitmapDescriptorFactory.HueCyan);
markerOpt1.InvokeIcon(bmDescriptor);
map.AddMarker(markerOpt1);
}
I have write custom renders for Android native application markers and ios mkmapview how can i show up the information view when loading the map
Here is the official sample of customrenderers-map-pin, to open infowindow by default,
In iOS:
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
//...
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
var nativeMap = Control as MKMapView;
customPins = formsMap.CustomPins;
//...
//add MapLoaded Event
nativeMap.MapLoaded += NativeMap_MapLoaded;
}
}
And in NativeMap_MapLoaded:
private void NativeMap_MapLoaded(object sender, EventArgs e)
{
annotationView.SetSelected(true,true);
}
In Android, try this:
protected override void OnMapReady(GoogleMap map)
{
base.OnMapReady(map);
MarkerOptions markerOpt1 = new MarkerOptions();
markerOpt1.SetPosition(new LatLng(50.379444, 2.773611));
markerOpt1.SetTitle("Vimy Ridge");
var bmDescriptor = BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueCyan);
Marker marker = map.AddMarker(markerOpt1);
marker.ShowInfoWindow();
}

Xamarin Forms Redraw StackLayout on Device Orientation Change

In my MainPage.xaml, which is a ContentPage, I have a GradientColorStack.
<local:GradientColorStack x:Name="ColorStack" StartColor="#FF0000" EndColor="#FFFF00">
This class has StackLayout as a base class and it presents a background that has a gradient color fill. I got this idea from here.
public class GradientColorStack : StackLayout
The problem is that on device orientation change, the background color fill is not re-drawn. Thus in landscape orientation there is a big gap filled with black where you can see through the GradientColorStack to the ContentPage background.
Is there a way to force the GradientColorStack to re-draw on device orientation change?
iOS Version
I added the following method to my renderer class:
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
SetNeedsDisplay();
base.OnElementPropertyChanged(sender, e);
}
This causes the Draw() method of the same class to be called on a screen orientation event. In Draw() I now find the existing CAGradientLayer (if it exists) and remove it from the super layer and then just let the Draw() routine then calculate a new CAGradientLayer as usual, which it then adds to the NativeView.Layer.Sublayers collection as usual.
Thanks to #SHUBHAM SHARMA for the tip of using SetNeedsDisplay() but this post was also required to get the solution working.
Android Version
It turns out the Android version was already working to correctly redraw the gradient layer (see tutorial). I think this is because of the following method in my renderer class:
protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
var stack = e.NewElement as GradientColorStack;
this.StartColor = stack.StartColor;
this.EndColor = stack.EndColor;
}
You can use ForceLayout() in android Renderer and SetNeedDisplay() in iOS Renderer because these function are forcefully refresh the layout.

How to move a Helix sphere object in WPF?

I'm new to Helix and liking it so far, I'm building a WPF app that loads the Helix viewport window inside a panel, I then instantiate a cube (CubedVisual3D) via a create button in WPF and it creates the cube, however when I go to click or drag/move it, it doesn't move around. How do I do this? Best approach?
example image
private void Helix_ViewPort_MouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Mouse down.");
Point mousePos = e.GetPosition(MyViewPort);
PointHitTestParameters hitParams = new PointHitTestParameters(mousePos);
HitTestResult result = VisualTreeHelper.HitTest(MyViewPort, mousePos);
RayMeshGeometry3DHitTestResult rayMeshResult = result as
RayMeshGeometry3DHitTestResult;
if (rayMeshResult != null)
{
MeshGeometry3D mesh = new MeshGeometry3D(); mesh.Positions.Add(rayMeshResult.MeshHit.Positions[rayMeshResult.VertexIndex1]);
mesh.Positions.Add(rayMeshResult.MeshHit.Positions[rayMeshResult.VertexIndex2]);
mesh.Positions.Add(rayMeshResult.MeshHit.Positions[rayMeshResult.VertexIndex3]);
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(2);
GeometryModel3D marker = new GeometryModel3D(mesh, new DiffuseMaterial(Brushes.Blue));
}
Console.WriteLine(result);
}
Please refer to the manipulator demo. There are many demos under helixtoolkit github source.

How to use Android AutoCompleteTextView on Xamarin.Forms

I'm working on a Xamarin.forms project but i need to use Android.Widget.AutoCompleteTextView how can i apply that?
When i am trying to add AutoCompleteTextView UserNameAutoComplete; to ContentPage i get the following error:
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Padding = new Thickness(25),
Children =
{
UserNameAutoComplete,
passwordEditor,
}
};
cannot convert from 'Android.Widget.AutoCompleteTextView' to
'Xamarin.Forms.View'
Android.Widget.AutoCompleteTextView is a View from Android.
Solution for PCL:
You can't use platform specific View's on Xamarin Forms (PCL) ContentPage.
To use platform specific View you should use a custom render.
There is a blog post from #JamesMontemagno that shows how to do what you need.
This code is draft exemple please use it as such.
1 - Create your own custom Xamarin.Forms control that will be renderered in Android as an AutoCompleteTextView:
public class AutoCompleteView : View
{
// Place need properties here.
}
2 - In Android project add a renderer for AutoCompleteView:
[assembly: ExportRenderer(typeof(AutoCompleteView), typeof(AutoCompleteViewRenderer))]
namespace App.Droid
{
public class AutoCompleteViewRenderer : ViewRenderer<AutoCompleteView, AutoCompleteTextView>
{
// Initialize the AutoCompleteTextView
protected override void OnElementChanged (ElementChangedEventArgs<AutoComplete> e)
{
base.OnElementChanged (e);
if (e.OldElement != null || this.Element == null)
return;
var autoComplete = new AutoCompleteTextView(Forms.Context);
SetNativeControl (autoComplete);
}
// Use the control here.
protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e) {
base.OnElementPropertyChanged (sender, e);
if (this.Element == null || this.Control == null)
return;
// variable this.Control is the AutoCompleteTextView, so you an manipulate it.
}
}
}
Solution for Shared Project:
When using Shared Project there is a possibility to use Native Embedding, like:
...
var textView = new TextView (Forms.Context) { Text = originalText };
stackLayout.Children.Add (textView);
contentView.Content = textView.ToView();
...

How to customize entry field in xamarin forms in windows phone

I've been trying to customize the entry field so that i can apply corner radiusin windows phone but i've been searching how to apply style template in custom renderer .I've used Custom renderer for android and set the background but for windows i didnt find any way to do it.I dont want to use the extended entry from X labs.
Please suggest?
Below is my Custom Renderer:
public class CustomEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Entry> e)
{
base.OnElementChanged(e);
if (this.Control == null) return;
var border = new Border {
CornerRadius = new System.Windows.CornerRadius(10),
Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 130, 186, 132)),
BorderBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 45, 176, 51)),
BorderThickness = new System.Windows.Thickness(0.8),
};
}
}
I'm searching out for an option so that I set the corner radius for entry field.

Resources