{"id":44311,"date":"2019-06-07T09:26:32","date_gmt":"2019-06-07T16:26:32","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/xamarin\/?p=44311"},"modified":"2019-08-30T08:28:57","modified_gmt":"2019-08-30T15:28:57","slug":"maps-navagation-xamarin-essentials","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/maps-navagation-xamarin-essentials\/","title":{"rendered":"Use Maps to Display and Navigate Users with Xamarin.Essentials"},"content":{"rendered":"<p>A common scenario for mobile apps is to help their users get to a specific location. This may be a restaurant, office, or the trailhead for the most amazing hike in the pacific northwest. We can leverage the existing built-in map application of iOS, Android, and Windows to easily get our users there. This is great because these apps already have the map data, user preferences, and full GPS navigation built right in. Xamarin.Essentials, our cross-platform API library, enables any mobile app to easily launch the map to a specific location or start navigation to that location from the user&#8217;s current position using the <a>Maps API<\/a>.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-44316\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/ShowLocation.png\" alt=\"\" width=\"450\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/ShowLocation.png 954w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/ShowLocation-300x261.png 300w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/ShowLocation-768x668.png 768w\" sizes=\"(max-width: 954px) 100vw, 954px\" \/><\/p>\n<h2>Setup<\/h2>\n<p>to start with the Xamarin.Essentials library, you can create a new Xamarin.Forms project using the latest version of Visual Studio 2019. Xamarin.Essentials now comes bundled with every project. If you have an existing app, you can add Xamarin.Essentials to it by installing the <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Essentials\/\">NuGet package<\/a>. Finally, you can add Xamarin.Essentials to your .NET Standard library if you are using one to share code across your iOS, Android, and UWP app projects.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-38745\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Install.png\" alt=\"\" width=\"764\" height=\"232\" \/><\/p>\n<p>After installing the NuGet package, there is a small amount of code on Android that is required to initialize Xamarin.Essentials.<\/p>\n<p>In the Android project&#8217;s `MainLauncher` or any `Activity` that is launched, Xamarin.Essentials must be initialized in the `OnCreate` method:<\/p>\n<pre class=\"lang:c# decode:true \">protected override void OnCreate(Bundle savedInstanceState) {\r\n  \/\/...\r\n  base.OnCreate(savedInstanceState);\r\n  Xamarin.Essentials.Platform.Init(this, savedInstanceState); \/\/ add this line to your code, it may also be called: bundle\r\n  \/\/...\r\n<\/pre>\n<p>To handle runtime permissions on Android, Xamarin.Essentials must receive an `OnRequestPermissionsResult`. Add the following code to all `Activity` classes:<\/p>\n<pre class=\"lang:c# decode:true \">public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)\r\n{\r\n  Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);\r\n\r\n  base.OnRequestPermissionsResult(requestCode, permissions, grantResults);\r\n}\r\n<\/pre>\n<h2>Open a Specific (Map) Location<\/h2>\n<p>The easiest way to open maps is to use a <code>Location<\/code> that consists of the latitude and longitude to display. This location is passed into the <code>Maps.OpenAsync<\/code> method along with <code>MapLaunchOptions<\/code>, which define additional information to pass to the map app. One property that you may want to set in the options is the <code>Name<\/code>, which the map app will display instead of the latitude and longitude. Here is an example of opening the map app to Microsoft Building 25, the home of Channel 9.<\/p>\n<pre><code>\r\npublic async Task MapBuilding25()\r\n{\r\n   var location = new Location(47.645160, -122.1306032);\r\n   var options =  new MapLaunchOptions { Name = \"Microsoft Building 25\" };\r\n\r\n   await Map.OpenAsync(location, options);\r\n}\r\n<\/code><\/pre>\n<h2>Navigate to Location<\/h2>\n<p>Set the <code>NavigationMode<\/code> property when launching the map if your map needs to give navigation options to the user. There are several navigation modes including driving, walking, bicycling, and transit. There are a few platform differences when it comes to supported modes, so be sure to read the <a href=\"https:\/\/aka.ms\/motz-essentials-maps\">Maps documentation<\/a>.<\/p>\n<pre><code>\r\npublic async Task NavigateToBuilding25()\r\n{\r\n   var location = new Location(47.645160, -122.1306032);\r\n   var options =  new MapLaunchOptions { NavigationMode = NavigationMode.Driving };\r\n\r\n   await Map.OpenAsync(location, options);\r\n}\r\n<\/code><\/pre>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-44317\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/Driving.png\" alt=\"\" width=\"450\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/Driving.png 951w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/Driving-300x263.png 300w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/05\/Driving-768x673.png 768w\" sizes=\"(max-width: 951px) 100vw, 951px\" \/><\/p>\n<h2>Using Placemarks<\/h2>\n<p>In addition to navigation to a specific <code>Location<\/code>, the Maps API can also use <code>Placemarks<\/code>. This property contains information such as street address, city, and country. Here is an example of navigating to Microsoft Building 25 with a <code>Placemark<\/code><\/p>\n<pre><code>\r\npublic class MapTest\r\n{\r\n    public async Task NavigateToBuilding25()\r\n    {\r\n        var placemark = new Placemark\r\n            {\r\n                CountryName = \"United States\",\r\n                AdminArea = \"WA\",\r\n                Thoroughfare = \"Microsoft Building 25\",\r\n                Locality = \"Redmond\"\r\n            };\r\n        var options =  new MapLaunchOptions { Name = \"Microsoft Building 25\" };\r\n\r\n        await Map.OpenAsync(placemark, options);\r\n    }\r\n}\r\n<\/code><\/pre>\n<h2>See It In Action<\/h2>\n<p>Did you know that we have a dedicate The Xamarin Show each week that&#8217;s available both on\u00a0<a href=\"https:\/\/www.youtube.com\/c\/XamarinDevelopers\">YouTube<\/a>, and <a href=\"https:\/\/channel9.msdn.com\/Shows\/XamarinShow\">Channel 9<\/a>\u00a0? There we highlight how to set up, get started, and use the APIs in Xamarin.Essentials. Check out this episode on Maps:<\/p>\n<p><center><iframe width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\" src=\"https:\/\/channel9.msdn.com\/Shows\/XamarinShow\/Maps-XamarinEssentials-API-of-the-Week\/player\"><\/iframe><\/center><\/p>\n<h2>Learn More<\/h2>\n<p>Browse through the <a href=\"https:\/\/aka.ms\/motz-essentials\">Xamarin.Essentials documentation<\/a> to learn more about all of the great cross-platform native APIs. Further more, be sure to check out the <a href=\"https:\/\/aka.ms\/motz-essentials-maps\">Maps documentation<\/a> to learn of the APIs available, additional implementation, and limitation details. Xamarin.Essentials is also <a href=\"https:\/\/github.com\/xamarin\/essentials\">open source on GitHub<\/a> where you can report issues, ask for features, and contribute to the library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Browse the Xamarin.Essentials Maps documentation to learn about all of the great cross-platform native APIs with additional implementation and limitation details. Xamarin.Essentials is open source on GitHub where you can report issues, ask for features, and contribute to the library.<\/p>\n","protected":false},"author":544,"featured_media":44316,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[313,2,303,291,367],"tags":[5539,24],"class_list":["post-44311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-developers","category-ios","category-xamarin-platform","category-xamarin-forms","tag-maps","tag-xamarin-essentials"],"acf":[],"blog_post_summary":"<p>Browse the Xamarin.Essentials Maps documentation to learn about all of the great cross-platform native APIs with additional implementation and limitation details. Xamarin.Essentials is open source on GitHub where you can report issues, ask for features, and contribute to the library.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/44311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=44311"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/44311\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/44316"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=44311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=44311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=44311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}