Skip to content

Commit

Permalink
Support all bootstrap parameters available for initMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickztar committed Aug 28, 2024
1 parent a27f5d4 commit 950e455
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
4 changes: 1 addition & 3 deletions GoogleMapsComponents/Maps/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public static async Task<Map> CreateAsync(
{
MapApiLoadOptions apiOpts = opts.ApiLoadOptions;
await jsRuntime.InvokeVoidAsync("blazorGoogleMaps.objectManager.initMap",
apiOpts.Key,
apiOpts.Version,
apiOpts.Libraries);
apiOpts);
}
var jsObjectRef = await JsObjectRef.CreateAsync(jsRuntime, "google.maps.Map", mapDiv, opts);
var dataObjectRef = await jsObjectRef.GetObjectReference("data");
Expand Down
54 changes: 53 additions & 1 deletion GoogleMapsComponents/Maps/MapApiLoadOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace GoogleMapsComponents.Maps;
using System.Text.Json.Serialization;

namespace GoogleMapsComponents.Maps;

/// <summary>
/// All options
Expand All @@ -10,13 +12,63 @@ public class MapApiLoadOptions(string key)
/// Default is weekly.
/// https://developers.google.com/maps/documentation/javascript/versions
/// </summary>
[JsonPropertyName("v")]
public string Version { get; set; } = "weekly";

/// <summary>
/// Comma separated list of libraries to load.
/// https://developers.google.com/maps/documentation/javascript/libraries
/// </summary>
public string? Libraries { get; set; } = "places,visualization,drawing,marker";

/// <summary>
/// The language to use. This affects the names of controls, copyright notices, driving directions, and control labels, and the responses to service requests. See the list of supported languages.
/// https://developers.google.com/maps/documentation/javascript/localization
/// https://developers.google.com/maps/faq#languagesupport
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Language { get; set; }

/// <summary>
/// The region code to use. This alters the map's behavior based on a given country or territory.
/// https://developers.google.com/maps/documentation/javascript/localization#Region
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Region { get; set; }

/// <summary>
/// Maps JS customers can configure HTTP Referrer Restrictions in the Cloud Console to limit which URLs are allowed
/// to use a particular API Key. By default, these restrictions can be configured to allow only certain paths to
/// use an API Key. If any URL on the same domain or origin may use the API Key, you can set
/// authReferrerPolicy: "origin" to limit the amount of data sent when authorizing requests from the Maps
/// JavaScript API. When this parameter is specified and HTTP Referrer Restrictions are enabled on Cloud Console,
/// Maps JavaScript API will only be able to load if there is an HTTP Referrer Restriction that matches the
/// current website's domain without a path specified.
/// https://developers.google.com/maps/documentation/javascript/get-api-key#restrict_key
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? AuthReferrerPolicy { get; set; }

/// <summary>
/// An array of map Ids. Causes the configuration for the specified map Ids to be preloaded.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string[]? MapIds { get; set; }

/// <summary>
/// See Usage tracking per channel.
/// https://developers.google.com/maps/reporting-and-monitoring/reporting#usage-tracking-per-channel
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Channel { get; set; }

/// <summary>
/// Google Maps Platform provides many types of sample code to help you get up and running quickly. To track
/// adoption of our more complex code samples and improve solution quality, Google includes the solutionChannel
/// query parameter in API calls in our sample code.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? SolutionChannel { get; set; }

/// <summary>
/// Synchronous key provider function used by the default implementation
Expand Down
14 changes: 8 additions & 6 deletions GoogleMapsComponents/wwwroot/js/objectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,15 @@
return {
objectManager: {
get mapObjects() { return mapObjects; },
initMap: async function (key, version, libraries) {
(g => { var h, a, k, p = "The Google Maps JavaScript API", c = "google", l = "importLibrary", q = "__ib__", m = document, b = window; b = b[c] || (b[c] = {}); var d = b.maps || (b.maps = {}), r = new Set, e = new URLSearchParams, u = () => h || (h = new Promise(async (f, n) => { await (a = m.createElement("script")); e.set("libraries", [...r] + ""); for (k in g) e.set(k.replace(/[A-Z]/g, t => "_" + t[0].toLowerCase()), g[k]); e.set("callback", c + ".maps." + q); a.src = `https://maps.${c}apis.com/maps/api/js?` + e; d[q] = f; a.onerror = () => h = n(Error(p + " could not load.")); a.nonce = m.querySelector("script[nonce]")?.nonce || ""; m.head.append(a) })); d[l] ? console.warn(p + " only loads once. Ignoring:", g) : d[l] = (f, ...n) => r.add(f) && u().then(() => d[l](f, ...n)) })({
key: key,
v: version,
});
initMap: async function (apiOptions) {
const librariesToLoad = apiOptions["libraries"];
delete apiOptions["libraries"];

(g => { var h, a, k, p = "The Google Maps JavaScript API", c = "google", l = "importLibrary", q = "__ib__", m = document, b = window; b = b[c] || (b[c] = {}); var d = b.maps || (b.maps = {}), r = new Set, e = new URLSearchParams, u = () => h || (h = new Promise(async (f, n) => { await (a = m.createElement("script")); e.set("libraries", [...r] + ""); for (k in g) e.set(k.replace(/[A-Z]/g, t => "_" + t[0].toLowerCase()), g[k]); e.set("callback", c + ".maps." + q); a.src = `https://maps.${c}apis.com/maps/api/js?` + e; d[q] = f; a.onerror = () => h = n(Error(p + " could not load.")); a.nonce = m.querySelector("script[nonce]")?.nonce || ""; m.head.append(a) })); d[l] ? console.warn(p + " only loads once. Ignoring:", g) : d[l] = (f, ...n) => r.add(f) && u().then(() => d[l](f, ...n)) })(
apiOptions
);

var libArray = libraries.split(',');
var libArray = librariesToLoad.split(',');
for (var i = 0; i < libArray.length; i++) {
var library = libArray[i];
await google.maps.importLibrary(library);
Expand Down
3 changes: 2 additions & 1 deletion ServerSideDemo/EnvVariableBlazorGoogleMapsKeyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public Task<MapApiLoadOptions> GetApiOptions()

return Task.FromResult(new MapApiLoadOptions(key)
{
Version = "beta"
Version = "beta",
// Language = "ja"
});
}

Expand Down

0 comments on commit 950e455

Please sign in to comment.