![](/img/trans.png)
[英]How to change location of a windows form element in Visual Studios 2012 C# Windows Phone
[英]using maps for visual studios 2012 for windows phone
我第一次嘗試在Windows Phone上運行應用程序。 對於我的第一個實際項目,我雖然會在手機上顯示地圖,但我的中心是我自己的gps坐標。 不幸的是,我遇到了所有可以想象的問題,谷歌首次對我有0幫助。 現在,每當我在手機上打開應用程序時,它就會顯示空白屏幕。 這是我的xaml代碼。
<phone:PhoneApplicationPage
x:Class="GPSTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:m="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<m:Map x:Name="MainMap"
Grid.Row="0"
Height="748"
Margin="10,10,10,0"
VerticalAlignment="Top"
ZoomLevel="14" Grid.RowSpan="2"/>
</Grid>
</phone:PhoneApplicationPage
這是我在后面代碼中的代碼:xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GPSTest.Resources;
using Microsoft.Phone.Maps.Controls;
using System.Device.Location;
using System.Threading;
using Windows.Devices.Geolocation;
namespace GPSTest
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
OneShotLocation();
}
private async void OneShotLocation()
{
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracyInMeters = 50;
try
{
Geoposition geoposition = await geolocator.GetGeopositionAsync(
maximumAge: TimeSpan.FromMinutes(5),
timeout: TimeSpan.FromSeconds(10)
);
Double DLat = Convert.ToDouble(geoposition.Coordinate.Latitude.ToString("0.00"));
Double DLong = Convert.ToDouble(geoposition.Coordinate.Longitude.ToString("0.00"));
MainMap.Center.Latitude = DLat;
MainMap.Center.Longitude = DLong;
}
我聽說我需要添加對bing映射,Bing Maps for C#,C ++或Visual Basic的引用,但是無法添加它,因為它沒有顯示在我的引用列表中。 還有一些有關獲取鑰匙的事情。 我去了一個,但是不能將它放在CredentialsProvider =中,因為它說不需要。
他們似乎有很多過時的代碼,這可能就是問題所在。 在此先感謝您,我真的對此感到困惑。
如果您使用的是Windows Phone 8,則可以在此處實施諾基亞地圖,因此不需要任何密鑰
所以現在為您的currnet位置使用此代碼
Geolocator geoLocator = new Geolocator();
Geoposition geoPosition = await geoLocator.GetGeopositionAsync();
Geocoordinate myGeocoordinate = geoPosition.Coordinate;
GeoCoordinate myGeoCoordinate = CoordinateConverter.ConvertGeoCoOrdinate(myGeocoordinate);
// mapWithMyLocation.Center = myGeoCoordinate;
mapWithMyLocation.SetView(myGeoCoordinate , 10, MapAnimationKind.Parabolic);
由於有了這個新地圖,Geocoordinate和GeoCoordinate有所不同。 所以您必須對此進行轉換,以便使用此轉換器方法
public static GeoCoordinate ConvertGeoCoOrdinate(Geocoordinate geoCooridinate)
{
return new GeoCoordinate(
geoCooridinate.Latitude,
geoCooridinate.Longitude,
geoCooridinate.Altitude ?? double.NaN,
geoCooridinate.Accuracy,
geoCooridinate.AltitudeAccuracy ?? double.NaN,
geoCooridinate.Speed ?? double.NaN,
geoCooridinate.Heading ?? double.NaN);
}
這很容易,不是嗎? :-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.