Using Xamarin.Forms.Maps: You have to Init() first!

The first time I wrote a Xamarin.Forms.Maps program, I couldn’t figure out why my map wasn’t appearing. And then I put a call to Xamarin.FormsMaps.Init() into the AppDelegate (iOS) and MainActivity (Android):

Shared:

[code lang=”csharp”]
public class App
{
public static Page GetMainPage ()
{
return new ContentPage {
Content = new StackLayout {
Children = {
new BoxView { BackgroundColor = Color.Green },
new Map(MapSpan.FromCenterAndRadius(new Position(37,-122), Distance.FromMiles(10))){
VerticalOptions = LayoutOptions.FillAndExpand,
HeightRequest = 100,
WidthRequest = 960,
BackgroundColor = Color.Blue
},
new BoxView { BackgroundColor = Color.Red }
}
}
};
}
}
[/code]

iOS:
[code lang=”csharp”]
namespace HelloMap.iOS
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
Forms.Init ();
FormsMaps.Init ();

window = new UIWindow (UIScreen.MainScreen.Bounds);

window.RootViewController = App.GetMainPage ().CreateViewController ();
window.MakeKeyAndVisible ();

return true;
}
}
}
[/code]

Android:
[code lang=”csharp”]
namespace HelloMap.Android
{
[Activity (Label = "HelloMap.Android.Android", MainLauncher = true)]
public class MainActivity : AndroidActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

Xamarin.Forms.Forms.Init (this, bundle);
FormsMaps.Init(this, bundle);

SetPage (App.GetMainPage ());
}
}
}
[/code]

Happy Cross-Platform Coding!

Screen Shot 2014-05-30 at 1.52.09 PM

9 thoughts on “Using Xamarin.Forms.Maps: You have to Init() first!

  1. I try to show a map but i see this error:
    “Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).”
    I have an API Key.

Comments are closed.