ピンを緯度と経度を使って地図上に表示する方法を知っていますが、現在の位置ピンが表示されているという問題があります。現在の位置のピンを地図上に表示する
私はチュートリアル続いている: https://www.youtube.com/watch?v=cx5O9GfOnS4
をしかし、私は「position.Longitude」または「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
}
};
}
}
}
EDIT:新しいコード 最新号: *それは私の現在位置を表示し、「現在の場所」とそれらの間の距離を示すために、「テスト」の場所にそれを比較しません。
"position.Longitude、position.Latitude"の代わりに。(022225,555552)などの座標を使用すると動作しますが、現在の場所を取得するためにGPSでは使用されませんのでご注意ください。私がどこに間違っていたか、それを修正する方法について助言してください。それは私の位置を見つけることはなかったなぜそれがだ、私は緯度の順序を持っていたし、長いスワップ :FIXED
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
}};
}
}
}
:
はここに新しいコードです。愚かな間違い。皆さん、ありがとうございました!
こんにちは、私はこの方法で試しましたが、私の現在の位置を取得せず、 "テスト"座標と比較します。上記の投稿を更新し、新しいコードを「編集」の下に追加しました。見て、私が現在のポジションを修正する方法を教えてください。 –
元の質問を見て、それを修正したようです! :-) – Krumelur
うん、私は持っている。すべての入力を感謝します! –