Another no less useful feature is to display a map, possibly with a set of pins identifying certain locations. With Xamarin.Forms’ Maps module this can be easily done in shared code. We only have to initialize FormsMaps in OnCreate on Android
Xamarin.FormsMaps.Init(this, savedInstanceState);
and in FinishedLaunching on iOS:
Xamarin.FormsMaps.Init();
Then, in the constructor of our App class, we create an example map with one pin.
First, we define a new Position:
var position = new Position(52.516432, 13.377693);
This object is used to position a new Pin. The Label will be unveiled after clicking on the pin.
var pin = new Pin {
Position = position,
Label = "Brandenburg Gate",
};
Now we create the example map. Therefore, we need a mapSpan, which is the visible area. It can be defined with a position and a radius. Here we use the same position as for the pin and a radius of 250 meters.
var mapSpan = MapSpan.FromCenterAndRadius(position, Distance.FromMeters(250));
Using the mapSpan we create the map. Two additional properties set the map type to Street (i.e. no satellite imagery) and add the previously defined pin.
var map = new Map(mapSpan) {
MapType = MapType.Street,
Pins = { pin },
};
Finally, we add the map to our MainPage.
MainPage = new ContentPage { Content = map };