【问题标题】:with xamarin forms maps, how can i center the map in the user current location?使用 xamarin 表单地图,我如何将地图置于用户当前位置的中心?
【发布时间】:2017-07-26 10:21:20
【问题描述】:

在我的 xamarin 跨平台应用程序中,当应用程序启动时,我试图将地图居中到当前用户位置,但我无法调整示例我发现我下载了 xamarin 的地理定位器插件,但我无法做到工作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms.Maps;
using Xamarin.Forms;
using Plugin.Geolocator;

namespace Map_Test
{

public partial class App : Application
{
    public App()
    {

        Map mapa = new Map();
        mapa.IsShowingUser = true;

        var locator = CrossGeolocator.Current;
        var position = locator.GetPositionAsync();

        mapa.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(position.Latitude, position.Longitude), Distance.FromMiles(1)));



        var rootPage = new ContentPage();
        rootPage.Content = mapa;

        MainPage = rootPage;

    }

    protected override void OnStart()
    {
        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}
}

我添加了一个新的 ContentPage 并执行了此操作,但我不知道如何从 updatePosition 方法访问 position.Latitude 和 position.Longitude 值。

using Plugin.Geolocator;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Plugin.Geolocator;

namespace Map_Test
{
public class MapTest : ContentPage
{
    Label locationLabel = new Label();
    public MapTest()
    {
        Map mapa = new Map();
        mapa.IsShowingUser = true;
        mapa.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(position.Latitude, position.Longitude),
                                         Distance.FromMiles(1)));

        Content = new StackLayout
        {
            Children = {
                mapa
            }
        };
    }

    public async void updatePosition()
    {
        var locator = CrossGeolocator.Current;
        var position = await locator.GetPositionAsync();
    }
}
}

【问题讨论】:

  • 你用了什么例子?会发生什么?
  • 'Task' 不包含'Latitude' 的定义,并且找不到接受'Task' 类型的第一个参数的扩展方法'Latitude'(您是否缺少使用指令或程序集引用?)“任务”不包含“经度”的定义,并且找不到接受“任务”类型的第一个参数的扩展方法“经度”(您是否缺少使用指令还是程序集引用?)
  • 这是我使用的示例:stackoverflow.com/a/36630649/7945025
  • 您忘记了 await 关键字。代码应该运行异步,但是你不能从你的构造函数运行异步代码,所以看起来你需要做一些重构并且可能阅读异步/等待。

标签: android xamarin xamarin.forms maps cross-platform


【解决方案1】:

我是这样解决的:

using Plugin.Geolocator;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Maps;

namespace Map_Test
{
public class MapTest : ContentPage
{
    public Map mapa = new Map();

    private async void findMe()
    {           
        var locator = CrossGeolocator.Current;
        Plugin.Geolocator.Abstractions.Position position = new Plugin.Geolocator.Abstractions.Position();

        position = await locator.GetPositionAsync();
        mapa.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(position.Latitude, position.Longitude),
                                        Distance.FromMiles(1)));
    }

    public MapTest()
    {
        mapa.IsShowingUser = true;
        findMe();


        var position = new Position(-38.743146, -72.615408);
        var pin1 = new Pin
        {
            Type = PinType.Generic,

            Position = position,
            Label = "Estacionamiento en Pasaje A",
            Address = "custom detail info"
        };

        mapa.Pins.Add(pin1);

        Content = new StackLayout
        {

            Children = {
                mapa
            }
        };
    }


}
}

【讨论】:

    猜你喜欢
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 2016-02-26
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多