🕓 12 MIN Welcome to 2024 and …
Google Maps is the leading location data and visualization platform for the web, but the functionality of Google Maps extends well beyond its mobile app — if you are building a mobile, web, or desktop app and want to include location displays in any way, Google Maps offers  developer APIs that give access to Google’s geographical data.Â
Google Maps API allows you to display and leverage interactive maps to help users create itineraries, routes, and more. Its data also refreshes in real-time, meaning that maps you create with Google API will always be up to date for visitors.Â
Recently, a new Uno Platform user was looking to implement Google Maps into his Uno Platform WASM application but was running into some blockers since MapControl does not provide WASM support yet. Luckily, you can integrate Uno-WASM Web Applications with existing JavaScript components and it’s quite simple. Â
In this article, Martin Zickmund works through a step-by-step guide on how you can easily implement Google Maps into your WASM application.  Â
You can follow any tutorial available on the web like here on how to get an API key via the Google Developer Console. It involves creating a project, adding required billing information, creating an API key, and then enabling Google Maps API for it. Make sure to restrict the API key when you plan to go public so that someone does not misuse your key for their purposes and use up your billing this way.Â
Because Google Maps requires loading an external JavaScript library, it is useful to load it ahead of time when your app first loads. to do that, add an index.html file to the root of your WASM project with the following content:Â
$(ADDITIONAL_CSS)
$(ADDITIONAL_HEAD)
Note: that most of the code is boilerplate which is the default in Uno WASM projects (see here). Replace MYAPIKEY with your API key from the developer console.Â
Now you need to instruct the WASM bootstrapper to use this custom index.html. Double-click the .csproj in the Solution Explorer and add the following:Â
index.html
Now that the Google Maps API is available on the JavaScript side, we can create a managed component that will represent the mapping control in our Uno Platform application:Â
#if __WASM__
using System;
using Uno.UI.Runtime.WebAssembly;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
namespace UnoWasmGoogleMaps
{
[HtmlElement("div")]
public class GoogleMapView : FrameworkElement
{
private readonly string _mapObjectName = "map_" + Guid.NewGuid().ToString().Replace("-", "");
public GoogleMapView()
{
Background = new SolidColorBrush(Colors.Transparent);
LoadMap();
}
private void LoadMap()
{
var javascript = $@"var {_mapObjectName} = new google.maps.Map(element, {{ center: {{lat: -34.397, lng: 150.644}}, zoom: 8 }});";
this.ExecuteJavascript(javascript);
}
}
}
#endif
Note: We are using a custom unique variable name to store the map object created by Google Maps API. You can use this variable name to manipulate the map and call some functions on it.Â
Finally, you can use the element in your app like this:
If you are new to Uno Platform, the best way to get started is to follow our official getting started guide. (5 min to complete) For better discoverability and ease of adopting Uno Platform, we have brushed up and published various working examples for Uno Platform, ranging from small single-feature samples to larger showcase applications.
Tags:
🕓 8 MIN Share on twitter Share …
🕓 3 MIN Share on twitter Share …
Uno Platform
360 rue Saint-Jacques, suite G101,
Montréal, Québec, Canada
H2Y 1P5
USA/CANADA toll free: +1-877-237-0471
International: +1-514-312-6958
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
Uno Platform 5.2 LIVE Webinar – Today at 3 PM EST – Watch