Skip to content

Commit 6d062f7

Browse files
committed
AddPolyline feature
1 parent 47ce2dd commit 6d062f7

File tree

8 files changed

+80
-54
lines changed

8 files changed

+80
-54
lines changed

src/Majorsoft.Blazor.Components.Maps/Google/Drawings/GoogleMapPolylineOptions.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace Majorsoft.Blazor.Components.Maps.Google
84
{
@@ -44,27 +40,27 @@ public class GoogleMapPolylineOptions
4440
/// <summary>
4541
/// The ordered sequence of coordinates of the Polyline.
4642
/// </summary>
47-
public GoogleMapLatLng Path { get; set; }
43+
public GoogleMapLatLng[] Path { get; set; }
4844

4945
/// <summary>
5046
/// The stroke color. All CSS3 colors are supported except for extended named colors.
5147
/// </summary>
52-
public string StrokeColor { get; set; }
48+
public string StrokeColor { get; set; } = "black";
5349

5450
/// <summary>
5551
/// The stroke opacity between 0.0 and 1.0.
5652
/// </summary>
57-
public double StrokeOpacity { get; set; }
53+
public double StrokeOpacity { get; set; } = 1.0;
5854

5955
/// <summary>
6056
/// The stroke width in pixels.
6157
/// </summary>
62-
public double StrokeWeight { get; set; }
58+
public double StrokeWeight { get; set; } = 2;
6359

6460
/// <summary>
6561
/// Whether this polyline is visible on the map. Defaults to true.
6662
/// </summary>
67-
public bool Visible { get; set; }
63+
public bool Visible { get; set; } = true;
6864

6965
/// <summary>
7066
/// The zIndex compared to other polys.

src/Majorsoft.Blazor.Components.Maps/GoogleMapLatLng.cs renamed to src/Majorsoft.Blazor.Components.Maps/Google/GoogleMapLatLng.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Majorsoft.Blazor.Components.Maps
1+
using System.Text.Json.Serialization;
2+
3+
namespace Majorsoft.Blazor.Components.Maps.Google
24
{
35
/// <summary>
46
/// A LatLng is a point in geographical coordinates: latitude and longitude.
@@ -9,12 +11,14 @@ public class GoogleMapLatLng
911
/// Latitude ranges between -90 and 90 degrees, inclusive. Values above or below this range will be clamped to the range [-90, 90].
1012
/// This means that if the value specified is less than -90, it will be set to -90. And if the value is greater than 90, it will be set to 90.
1113
/// </summary>
14+
[JsonPropertyName("lat")]
1215
public double Latitude { get; set; }
1316

1417
/// <summary>
1518
/// Longitude ranges between -180 and 180 degrees, inclusive. Values above or below this range will be wrapped so that they fall within the range.
1619
/// For example, a value of -190 will be converted to 170. A value of 190 will be converted to -170. This reflects the fact that longitudes wrap around the globe.
1720
/// </summary>
21+
[JsonPropertyName("lng")]
1822
public double Longitude { get; set; }
1923
}
2024
}

src/Majorsoft.Blazor.Components.Maps/Google/GoogleMapService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.JSInterop;
1+

2+
using Microsoft.JSInterop;
23

34
using System;
45
using System.Collections.Generic;
@@ -258,7 +259,7 @@ public async ValueTask<IJSObjectReference> GetDivAsync()
258259
public async Task AddPolyline(params GoogleMapPolylineOptions[] googleMapPolylineOptions)
259260
{
260261
await CheckJsObjectAsync();
261-
await _mapsJs.InvokeAsync<IJSObjectReference>("polylineSetMap", MapContainerId, googleMapPolylineOptions);
262+
await _mapsJs.InvokeVoidAsync("polylineSetMap", MapContainerId, googleMapPolylineOptions);
262263
}
263264

264265

src/Majorsoft.Blazor.Components.Maps/Google/IGoogleMapService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Task InitMapAsync(string apiKey,
167167
Task CreateCustomControlsAsync(IEnumerable<GoogleMapCustomControl> mapCustomControls);
168168

169169
/// <summary>
170-
/// Creates markers on the Map with InfoWindows on the given position with event callbacks.
170+
/// Creates and removes Markers on the Map with InfoWindows on the given position with event callbacks.
171171
/// </summary>
172172
/// <param name="newMarkers">Enumerable new markers to add</param>
173173
/// <param name="markers">Enumerable markers removed or replaced</param>
@@ -197,7 +197,7 @@ Task InitMapAsync(string apiKey,
197197
ValueTask<IJSObjectReference> GetDivAsync();
198198

199199
/// <summary>
200-
///
200+
/// Creates and removes Polyline on the Map with given values and event callbacks.
201201
/// </summary>
202202
/// <param name="googleMapPolylineOptions"></param>
203203
/// <returns></returns>

src/Majorsoft.Blazor.Components.Maps/Majorsoft.Blazor.Components.Maps.xml

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Majorsoft.Blazor.Components.Maps/wwwroot/googleMaps.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ window.initGoogleMaps = () => {
5656
restrict =
5757
{
5858
latLngBounds: {
59-
south: mapInfo.restriction.latLngBounds.southWest.latitude,
60-
west: mapInfo.restriction.latLngBounds.southWest.longitude,
61-
north: mapInfo.restriction.latLngBounds.northEast.latitude,
62-
east: mapInfo.restriction.latLngBounds.northEast.longitude
59+
south: mapInfo.restriction.latLngBounds.southWest.lat,
60+
west: mapInfo.restriction.latLngBounds.southWest.lng,
61+
north: mapInfo.restriction.latLngBounds.northEast.lat,
62+
east: mapInfo.restriction.latLngBounds.northEast.lng
6363
},
6464
strictBounds: mapInfo.restriction.strictBounds,
6565
};
@@ -397,7 +397,7 @@ export function getDiv(elementId) {
397397
}
398398
}
399399
function convertToLatLng(latLngObject) {
400-
let ret = { Latitude: latLngObject.lat(), Longitude: latLngObject.lng() };
400+
let ret = { lat: latLngObject.lat(), lng: latLngObject.lng() };
401401
return ret;
402402
}
403403

@@ -620,15 +620,10 @@ export function polylineSetMap(elementId, polylineOptions) {
620620
if (mapWithDotnetRef && mapWithDotnetRef.map) {
621621

622622
for (var i = 0; i < polylineOptions.length; i++) {
623-
let markerData = polylineOptions[i];
624-
625-
//_mapsMarkers.forEach((element, index) => {
626-
// if (markerData.id == element.id) {
627-
// element.setMap(null);
628-
// _mapsMarkers.splice(index, 1);
629-
// return;
630-
// }
631-
//});
623+
let options = polylineOptions[i];
624+
625+
let polyline = new google.maps.Polyline(options);
626+
polyline.setMap(mapWithDotnetRef.map);
632627
}
633628
}
634629
}

src/Majorsoft.Blazor.Components.Maps/wwwroot/googleMaps.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Majorsoft.Blazor.Components.TestApps.Common/Components/MapsGoogle.razor

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@
241241
<div class="row pb-2">
242242
<div class="col-12 col-lg-8 col-xl-6">
243243
<p>
244-
<h5>Maps Pre-render settings:</h5>
244+
<h5>Maps Pre-render settings:</h5>
245245
<ul>
246246
<li>Background color: <strong style="color: @_jsMapBackgroundColor;">@_jsMapBackgroundColor</strong></li>
247247
<li>Control size: <strong>@(_jsMapControlSize)px</strong></li>
@@ -317,9 +317,9 @@
317317
ApiKey="@_googleMapsApiKey" />
318318

319319
@*<GoogleMap @bind-Center="_jsMapCenter" @bind-Center:event="OnMapCenterChanged"
320-
@bind-Zoom="_jsMapZoomLevel" @bind-Zoom:event="OnMapZoomLevelChanged"
321-
OnMapInitialized="@(() => {})"
322-
ApiKey="@_googleMapsApiKey" />*@
320+
@bind-Zoom="_jsMapZoomLevel" @bind-Zoom:event="OnMapZoomLevelChanged"
321+
OnMapInitialized="@(() => {})"
322+
ApiKey="@_googleMapsApiKey" />*@
323323

324324
</div>
325325
</div>
@@ -329,6 +329,15 @@
329329
<button class="btn btn-primary" @onclick="@(() => { _jsMarkers.AddRange(_jsMarkersTmp); _jsMarkersTmp.Clear(); })">Show Markers</button>
330330
<button class="btn btn-primary" @onclick="@(() => { _jsMarkers.Clear(); _jsMarkersTmp.Clear(); _jsPhotosAdded = false; })">Remove Markers</button>
331331
<button class="btn btn-primary" @onclick="@AddPhotos">Add photo Markers</button>
332+
333+
</div>
334+
</div>
335+
<div class="row pb-2">
336+
<div class="col-12 col-lg-8 col-xl-6">
337+
<button class="btn btn-primary" @onclick="@AddMapDrawings">Add drawings:</button>
338+
<select style="display: inline" class="form-control selectpicker w-25">
339+
<option>Polyline</option>
340+
</select>
332341
</div>
333342
</div>
334343

@@ -350,7 +359,6 @@
350359
<button class="btn btn-primary mb-2" @onclick="@(CenterMyLocationWithJavaScriptMap)">Center my location with Map</button>
351360
<button class="btn btn-primary mb-2" @onclick="@(CenterMyLocationWithInjectedServiceJavaScript)">Center my location with GeolocationService</button>
352361
</div>
353-
354362
</div>
355363

356364
@implements IAsyncDisposable
@@ -597,6 +605,28 @@
597605
});
598606
}
599607

608+
public async Task AddMapDrawings()
609+
{
610+
var polylines = new GoogleMapPolylineOptions()
611+
{
612+
Path = new GoogleMapLatLng[]
613+
{
614+
new GoogleMapLatLng() { Latitude = 37.772, Longitude = -122.214 },
615+
new GoogleMapLatLng() { Latitude = 21.291, Longitude = -157.821 },
616+
new GoogleMapLatLng() { Latitude = -18.142, Longitude = 178.431 },
617+
new GoogleMapLatLng() { Latitude = -27.467, Longitude = 153.027 },
618+
},
619+
Geodesic = true,
620+
Draggable = true,
621+
Clickable = true,
622+
StrokeColor = "#FF0000",
623+
StrokeOpacity = 0.5,
624+
StrokeWeight = 5
625+
};
626+
627+
await _googleMap.GoogleMapService.AddPolyline(polylines);
628+
}
629+
600630
//Events
601631
private async Task OnMapInitialized(string elementId)
602632
{

0 commit comments

Comments
 (0)