我知道如何通过使用纬度和经度让地图显示在地图上,但我遇到的麻烦就是让当前的位置显示出来。
我已经按照教程: https://www.youtube.com/watch?v=cx5O9GfOnS4
但是,当我尝试做位置。长度"或" position.Latitude",第34行,它表示无法找到该位置。我需要帮助来尝试修复此错误并了解如何使其正常工作。
using Plugin.Geolocator;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
namespace WorkingWithMaps
{
public class PinPage : ContentPage
{
Map map;
public PinPage()
{
map = new Map {
IsShowingUser = true,
HeightRequest = 100,
WidthRequest = 960,
VerticalOptions = LayoutOptions.FillAndExpand
};
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = locator.GetPositionAsync(timeoutMilliseconds: 10000);
map.MoveToRegion (MapSpan.FromCenterAndRadius (
new Position (position.Longitude, position.Latitude), Distance.FromMiles (1))); // current pos
//var position = new Position(-26.077740, 28.010100); // Latitude, Longitude
var pin = new Pin {
Type = PinType.Place,
//Position = position,
Label = "Test",
Address = "Test..."
};
map.Pins.Add(pin);
//this works but I need current location not hard coded lat/lng
//map.MoveToRegion (MapSpan.FromCenterAndRadius (
//new Position (-26.077740, 28.010100), Distance.FromMiles (1))); // S4C location
//var position = new Position(-26.077740, 28.010100); // Latitude, Longitude
// var pin = new Pin {
// Type = PinType.Place,
// Position = position,
// Label = "Test",
// Address = "Test..."
// };
// map.Pins.Add(pin);
// create buttons
var morePins = new Button { Text = "Show Pins Near Me" };
morePins.Clicked += (sender, e) => {
map.Pins.Add(new Pin {
Position = new Position(-26.074932, 28.012136),
Label = "123"
});
map.Pins.Add(new Pin {
Position = new Position(-26.080752, 28.026094),
Label = "321"
});
map.MoveToRegion (MapSpan.FromCenterAndRadius (
new Position (-26.077740, 28.010100), Distance.FromMiles (2))); // this distance shows dealerships to current position
};
var reLocate = new Button { Text = "Re-center" };
reLocate.Clicked += (sender, e) => {
map.MoveToRegion (MapSpan.FromCenterAndRadius (
new Position (-26.077740, 28.010100), Distance.FromMiles (0.5)));
};
var buttons = new StackLayout {
Orientation = StackOrientation.Horizontal,
Children = {
morePins, reLocate
}
};
// put the page together
Content = new StackLayout {
Spacing = 0,
Children = {
map,
buttons
}
};
}
}
}
编辑:新代码 目前的问题: *它没有显示我当前的位置,并将其与"测试"显示"当前位置"之间距离的位置和他们。
请注意,如果我使用坐标,例如。(022225,555552)而不是" position.Longitude,position.Latitude"然后它工作,但它不用于GPS来获取我当前的位置。请告知我出错的地方以及如何解决。
这是新代码:
using Plugin.Geolocator;
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
namespace WorkingWithMaps
{
public class PinPage : ContentPage
{
Map map;
public PinPage()
{
map = new Map {
IsShowingUser = true,
HeightRequest = 100,
WidthRequest = 960,
VerticalOptions = LayoutOptions.FillAndExpand
}; }
protected async override void OnAppearing()
{
// create buttons
var morePins = new Button { Text = "Show Tests Near Me" };
//current pos
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
var reLocate = new Button { Text = "Re-center" };
reLocate.Clicked += (sender, e) => {
map.MoveToRegion(MapSpan.FromCenterAndRadius(
new Position(position.Longitude, position.Latitude), Distance.FromMiles(0.5)));
};
var buttons = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Children = {
morePins, reLocate
}
};
morePins.Clicked += (sender, e) => {
map.Pins.Add(new Pin {
Position = new Position(-26.074932, 28.012136),
Label = "Test 1"
});
map.Pins.Add(new Pin {
Position = new Position(-26.080752, 28.026094),
Label = "Test 2"
});
map.MoveToRegion(MapSpan.FromCenterAndRadius(
new Position(position.Longitude, position.Latitude), Distance.FromMiles(2))); // this distance shows tests to current position
};
// put the page together
Content = new StackLayout {
Spacing = 0,
Children = {
map,
buttons
}};
}
}
}
固定: 我有lat和长期交换的顺序,这就是为什么它没有找到我的位置。愚蠢的错误。感谢大家的投入!
答案 0 :(得分:2)
您将获得async
method所需的职位{/ 1}}。
await
但是,这种单一更改在您的情况下不起作用,因为您需要将构造函数标记为var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
,这是不可能的。
但您可以覆盖async
并将其设为异步:
OnAppearing
答案 1 :(得分:0)
在将结果传递到locator.GetPositionAsync(timeoutMilliseconds: 10000);
之前,您需要等待map
。像这样的东西 -
Task.Run(async () =>
{
var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
map.MoveToRegion (MapSpan.FromCenterAndRadius (
new Position (position.Longitude, position.Latitude), Distance.FromMiles (1))); // current pos
//var position = new Position(-26.077740, 28.010100); // Latitude, Longitude
var pin = new Pin {
Type = PinType.Place,
//Position = position,
Label = "Test",
Address = "Test..."
};
map.Pins.Add(pin);
});