Map and Map-Pin Xamarin.Forms

As always I will first show the NuGet package that will help us to get a map. I will add "Xamarin.Forms.Maps" into my solution.

Xamarin.Forms.Maps uses the native map APIs on each platform. This provides a fast, familiar maps experience for users, but means that some configuration is necessary to adhere to each platform-specific API requirement. Once configured, the Map control works just like any other Xamarin.Forms element in common code.

include xamarin

Now we want to set some permissions, so that we will use "AndroidManifest.xml" and set some of these user permissions. I want a key for accessing Google Maps. You can click here to learn more about it.

Now we can open "WMAppManifest.xml" for setting permissions for Windows Phone.

setting permissions

Now we want to initialize the Xamarin.Forms.Maps in each platform-specific startup class (for example, AppDelegate.cs for iOS, MainActivity.cs for Android and MainPage.xaml.cs in Windows Phone) by calling "Xamarin.FormsMaps.Init()" before calling "LoadApplication()".

Now we can provide the following code to get a map on our page:

  1. var map = new Map { HorizontalOptions = LayoutOptions.FillAndExpand };  
  2. var mapPosition = new Position(10.020921, 76.337919);  
  3. map.MoveToRegion ( MapSpan.FromCenterAndRadius(mapPosition, Distance.FromMiles(3)));  
Now we can add a small code snippet to add a pin to own map.
  1. var mapPin = new Pin {  
  2.    Type = PinType.Place,  
  3.    Position = mapPosition,  
  4.    Label = "Mazsoft Technologies",  
  5.    Address = "Kakkanad - Kerala"  
  6. };  
  7. map.Pins.Add(mapPin);  
For more details refer to my sample application from here.

Similar Articles