如何获取Geo位置Blazor Server

发布于 2025-02-13 02:37:27 字数 84 浏览 0 评论 0原文

我目前正在基于.NET 6的Blazor Server项目。需要将GEO位置进入应用程序。我已经尝试了几种方法,但它们不起作用。在那里有人知道吗?请帮我。

I am currently working on a blazor server project based on .NET 6. there is a requirement to get the geo location into the application. I have tried several ways but they don't work.is there anyone who knows about it ?? please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2025-02-20 02:37:27

这显示了如何调用浏览器API以获取当前位置。

geolocationjsinterop.js(wwwroot \ scripts)

export function getCurrentPosition(dotNetHelper) {

    const options = {
        enableHighAccuracy: true,
        timeout: 5000,
        maximumAge: 0
    };

    function success(pos) {

        const coord = {
            latitude: pos.coords.latitude,
            longitude: pos.coords.longitude,
            accuracy: pos.coords.accuracy
        };

        dotNetHelper.invokeMethodAsync('OnSuccessAsync', coord);
    }

    function error(err) {
        console.warn(`ERROR(${err.code}): ${err.message}`);
    }

    navigator.geolocation.getCurrentPosition(success, error, options);
}

geolocation.razor

@using Microsoft.JSInterop
@implements IAsyncDisposable
<button @onclick=@GetLocationAsync>Get Location</button>

@if (geoCoordinates is not null)
{
    <div>
        Latitude : @geoCoordinates.Latitude <br />
        Longitude : @geoCoordinates.Longitude<br />
        Accuracy : @geoCoordinates.Accuracy
    </div>
}

@code {
    private readonly Lazy<Task<IJSObjectReference>> moduleTask = default!;
    private readonly DotNetObjectReference<GeoLocation> dotNetObjectReference;
    private GeoCoordinates? geoCoordinates = null;

    [Inject]
    private IJSRuntime jsRuntime { get; set; } = default!;

    public GeoLocation()
    {
        moduleTask = new(() => jsRuntime!.InvokeAsync<IJSObjectReference>(
            identifier: "import",
            args: "./_content/ChatClient.Core/scripts/geoLocationJsInterop.js")
        .AsTask());

        dotNetObjectReference = DotNetObjectReference.Create(this);
    }

    public async Task GetLocationAsync()
    {
        var module = await moduleTask.Value;
        await module.InvokeVoidAsync(identifier: "getCurrentPosition", dotNetObjectReference);
    }

    [JSInvokable]
    public async Task OnSuccessAsync(GeoCoordinates geoCoordinates)
    {
        this.geoCoordinates = geoCoordinates;
        await InvokeAsync(StateHasChanged);
    }

    public async ValueTask DisposeAsync()
    {
        if (moduleTask.IsValueCreated)
        {
            var module = await moduleTask.Value;
            await module.DisposeAsync();
        }
    }

    public class GeoCoordinates
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public double Accuracy { get; set; }
    }
}

注意:更改.js文件的路径以适合您的项目。在这种情况下,我引用了称为chatclient.core的RCL。

args:“ ./scripts/geolocationjsinterop.js” for root Project。

该组件在WASM或Blazor-Server上运行。

api docs

This shows how to call the browser API to get the current position.

geoLocationJsInterop.js (wwwroot\scripts)

export function getCurrentPosition(dotNetHelper) {

    const options = {
        enableHighAccuracy: true,
        timeout: 5000,
        maximumAge: 0
    };

    function success(pos) {

        const coord = {
            latitude: pos.coords.latitude,
            longitude: pos.coords.longitude,
            accuracy: pos.coords.accuracy
        };

        dotNetHelper.invokeMethodAsync('OnSuccessAsync', coord);
    }

    function error(err) {
        console.warn(`ERROR(${err.code}): ${err.message}`);
    }

    navigator.geolocation.getCurrentPosition(success, error, options);
}

GeoLocation.razor

@using Microsoft.JSInterop
@implements IAsyncDisposable
<button @onclick=@GetLocationAsync>Get Location</button>

@if (geoCoordinates is not null)
{
    <div>
        Latitude : @geoCoordinates.Latitude <br />
        Longitude : @geoCoordinates.Longitude<br />
        Accuracy : @geoCoordinates.Accuracy
    </div>
}

@code {
    private readonly Lazy<Task<IJSObjectReference>> moduleTask = default!;
    private readonly DotNetObjectReference<GeoLocation> dotNetObjectReference;
    private GeoCoordinates? geoCoordinates = null;

    [Inject]
    private IJSRuntime jsRuntime { get; set; } = default!;

    public GeoLocation()
    {
        moduleTask = new(() => jsRuntime!.InvokeAsync<IJSObjectReference>(
            identifier: "import",
            args: "./_content/ChatClient.Core/scripts/geoLocationJsInterop.js")
        .AsTask());

        dotNetObjectReference = DotNetObjectReference.Create(this);
    }

    public async Task GetLocationAsync()
    {
        var module = await moduleTask.Value;
        await module.InvokeVoidAsync(identifier: "getCurrentPosition", dotNetObjectReference);
    }

    [JSInvokable]
    public async Task OnSuccessAsync(GeoCoordinates geoCoordinates)
    {
        this.geoCoordinates = geoCoordinates;
        await InvokeAsync(StateHasChanged);
    }

    public async ValueTask DisposeAsync()
    {
        if (moduleTask.IsValueCreated)
        {
            var module = await moduleTask.Value;
            await module.DisposeAsync();
        }
    }

    public class GeoCoordinates
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public double Accuracy { get; set; }
    }
}

Note: Change the path of the .js file to suite your project. In this case I was referencing a RCL called ChatClient.Core.

args: "./scripts/geoLocationJsInterop.js" for your root project.

This component runs on either wasm or blazor-server.

API Docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文