-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bing maps to demo of geolocation bits.
- Loading branch information
1 parent
b0ae83b
commit 75faacc
Showing
8 changed files
with
113 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@inject IJSInProcessRuntime JavaScript | ||
|
||
<div id="map" style="width: 100%; height: 45vh;"></div> | ||
|
||
@code { | ||
[Parameter, EditorRequired] | ||
public GeolocationPosition Position { get; set; } = null!; | ||
|
||
protected override void OnParametersSet() | ||
{ | ||
if (Position is null or { Coords: null }) | ||
{ | ||
return; | ||
} | ||
|
||
JavaScript.InvokeVoid( | ||
"app.loadMap", "map", Position.Coords.Latitude, Position.Coords.Longitude); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
global using System.Text.Json; | ||
global using System.Text.Json.Serialization; | ||
global using Microsoft.JSInterop; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
samples/Blazor.ExampleConsumer/Pages/ClientPosition.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace Blazor.ExampleConsumer.Pages; | ||
|
||
public partial class ClientPosition | ||
{ | ||
readonly JsonSerializerOptions _opts = new() | ||
{ | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull | ||
}; | ||
readonly PositionOptions _options = new() | ||
{ | ||
EnableHighAccuracy = true, | ||
MaximumAge = 0, | ||
Timeout = 15_000 | ||
}; | ||
|
||
GeolocationPosition? _position; | ||
GeolocationPositionError? _positionError; | ||
bool _isLoading = true; | ||
|
||
protected override void OnInitialized() => | ||
Geolocation.GetCurrentPosition( | ||
component: this, | ||
onSuccessCallbackMethodName: nameof(OnPositionRecieved), | ||
onErrorCallbackMethodName: nameof(OnPositionError), | ||
options: _options); | ||
|
||
[JSInvokable] | ||
public void OnPositionRecieved(GeolocationPosition position) | ||
{ | ||
_isLoading = false; | ||
_position = position; | ||
StateHasChanged(); | ||
} | ||
|
||
[JSInvokable] | ||
public void OnPositionError(GeolocationPositionError positionError) | ||
{ | ||
_isLoading = false; | ||
_positionError = positionError; | ||
StateHasChanged(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,34 @@ | ||
| ||
let _map = null; | ||
|
||
const loadMap = (mapId, latitude, longitude) => { | ||
const element = document.getElementById(mapId); | ||
if (!!element) { | ||
const navigationBarMode = Microsoft.Maps.NavigationBarMode; | ||
const location = new Microsoft.Maps.Location(latitude, longitude); | ||
_map = new Microsoft.Maps.Map(element, { | ||
center: location, | ||
navigationBarMode: navigationBarMode.compact, | ||
supportedMapTypes: [ | ||
Microsoft.Maps.MapTypeId.road, | ||
Microsoft.Maps.MapTypeId.aerial, | ||
Microsoft.Maps.MapTypeId.canvasLight | ||
] | ||
}); | ||
_map.setView({ zoom: 18 }); | ||
Microsoft.Maps.loadModule('Microsoft.Maps.Search', () => { | ||
var searchManager = new Microsoft.Maps.Search.SearchManager(_map); | ||
var reverseGeocodeRequestOptions = { | ||
location: location, | ||
callback: (answer, userData) => { | ||
_map.setView({ bounds: answer.bestView }); | ||
_map.entities.push(new Microsoft.Maps.Pushpin(reverseGeocodeRequestOptions.location)); | ||
} | ||
}; | ||
searchManager.reverseGeocode(reverseGeocodeRequestOptions); | ||
}); | ||
} | ||
}; | ||
|
||
window.app = { | ||
loadMap | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters