|
2 | 2 | @using GoogleMapsComponents |
3 | 3 | @using GoogleMapsComponents.Maps |
4 | 4 | @using GoogleMapsComponents.Maps.Data |
| 5 | +@using System.Runtime.InteropServices |
| 6 | + |
| 7 | +@implements IAsyncDisposable |
| 8 | + |
5 | 9 | @* |
6 | 10 | The code for this sample was taken from below on 09/02/2020: |
7 | 11 | https://developers.google.com/maps/documentation/javascript/earthquakes#maps_earthquake_circles-typescript |
|
13 | 17 | <button @onclick="@GetMapDataFeature">Get MapData (Feature)</button> |
14 | 18 | <button @onclick="@GetMapDataGeoJsonLine">Get MapData (GeoJson Line)</button> |
15 | 19 | <button @onclick="@GetMapDataGeoJsonPolygon">Get MapData (GeoJson Polygon)</button> |
| 20 | +<button @onclick="@UseSetStyleInJs">Set Global Style in JS function</button> |
| 21 | +<button @onclick="@UseSetStyleInWASM">Set Global Style in Blazor (WASM)</button> |
16 | 22 | <button @onclick="@RemoveFeatures">Remove All Features</button> |
17 | 23 |
|
18 | 24 | <div> |
|
34 | 40 | }; |
35 | 41 | } |
36 | 42 |
|
37 | | - private Task OnAfterMapInit() |
| 43 | + private async Task OnAfterMapInit() |
| 44 | + { |
| 45 | + await _map1.InteropObject.Data.AddListener<GoogleMapsComponents.Maps.Data.MouseEvent>("click", async e => OnClick(e)); |
| 46 | + await _map1.InteropObject.Data.AddListener<GoogleMapsComponents.Maps.Data.MouseEvent>("mouseover", async e => OnMouseOver(e)); |
| 47 | + await _map1.InteropObject.Data.AddListener<GoogleMapsComponents.Maps.Data.MouseEvent>("mouseout", async e => OnMouseOut(e)); |
| 48 | + } |
| 49 | + |
| 50 | + private async Task UseSetStyleInJs() |
| 51 | + { |
| 52 | + await _map1.InteropObject.Data.SetStyle("window.mapDataPage.getFeatureStyle"); |
| 53 | + } |
| 54 | + |
| 55 | + private async Task UseSetStyleInWASM() |
| 56 | + { |
| 57 | + if (RuntimeInformation.ProcessArchitecture == Architecture.Wasm) |
| 58 | + await _map1.InteropObject.Data.SetStyle(GetStyle); |
| 59 | + } |
| 60 | + |
| 61 | + StyleOptions GetStyle(Feature feature) |
| 62 | + { |
| 63 | + return new StyleOptions |
| 64 | + { |
| 65 | + FillColor = "orange", |
| 66 | + StrokeColor = "purple", |
| 67 | + StrokeWeight = 2, |
| 68 | + StrokeOpacity = 1, |
| 69 | + FillOpacity = 0.5f |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + private async Task OnClick(GoogleMapsComponents.Maps.Data.MouseEvent e) |
| 74 | + { |
| 75 | + if (e.Feature == null) |
| 76 | + return; |
| 77 | + string fill = await e.Feature.GetProperty<string>("fillColor"); |
| 78 | + await e.Feature.SetProperty("fillColor", fill == "orange" ? "green" : "orange"); |
| 79 | + } |
| 80 | + |
| 81 | + private async Task OnMouseOver(GoogleMapsComponents.Maps.Data.MouseEvent e) |
| 82 | + { |
| 83 | + await _map1.InteropObject.Data.RevertStyle(); |
| 84 | + await _map1.InteropObject.Data.OverrideStyle(e.Feature, new StyleOptions() { |
| 85 | + StrokeWeight = 8f |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + private async Task OnMouseOut(GoogleMapsComponents.Maps.Data.MouseEvent e) |
38 | 90 | { |
39 | | - return Task.CompletedTask; |
| 91 | + await _map1.InteropObject.Data.RevertStyle(); |
40 | 92 | } |
41 | 93 |
|
42 | 94 | private async Task GetMapDataFeature() |
|
122 | 174 | " \"features\": [" + |
123 | 175 | "{ \"type\": \"Feature\"," + |
124 | 176 | " \"id\": 3," + |
125 | | - " \"properties\": { \"stroke\": \"#555555\", \"stroke-width\": 2, \"stroke-opacity\": 1 }, " + |
126 | 177 | " \"geometry\": {\"type\": \"Polygon\", \"coordinates\": [ [ [ 151.219, -33.888], [151.23, -33.888], [151.23, -33.869], [ 151.219, -33.869], [ 151.219, -33.888]]]}}]}"; |
127 | 178 |
|
128 | | - await _map1.InteropObject.Data.AddGeoJson(jsonData); |
129 | | - await _map1.InteropObject.Data.SetStyle(new StyleOptions { StrokeColor = "magenta", StrokeWeight = 2, FillColor = "magenta", FillOpacity = 0.3f }); |
| 179 | + var features = await _map1.InteropObject.Data.AddGeoJson(jsonData); |
| 180 | + foreach(Feature feature in features) { |
| 181 | + await feature.SetProperty("fillColor", "red"); |
| 182 | + await feature.SetProperty("strokeColor", "cyan"); |
| 183 | + } |
130 | 184 | } |
131 | 185 |
|
132 | 186 | private async Task RemoveFeatures() |
133 | 187 | { |
134 | 188 | await _map1.InteropObject.Data.RemoveAll(); |
135 | 189 | } |
| 190 | + |
| 191 | + public async ValueTask DisposeAsync() |
| 192 | + { |
| 193 | + await _map1.DisposeAsync(); |
| 194 | + } |
136 | 195 | } |
137 | 196 |
|
0 commit comments