总的界面布局如下:
其xaml代码如下:
<Grid> <Grid.RowDefinitions> <RowDefinition Height="450"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Maps:MapControl Grid.Row="0" Name="myMap"/> <StackPanel Grid.Row="1"> <TextBlock Name="sliderTextblock" HorizontalAlignment="Center" Style="{ThemeResource ControlHeaderTextBlockStyle}" /> <Slider Name="mapSlider" ValueChanged="mapSlider_ValueChanged" Maximum="18" Minimum="0"/> <StackPanel Orientation="Horizontal"> <Button Name="GetPositionButton" Content="Get" Click="GetPositionButton_Click"/> <Button Name="SetPositonButton" Content="Set" Click="SetPositonButton_Click"/> </StackPanel> <TextBlock Name="showMapTextblock" /> </StackPanel> </Grid>
对应的主要的C#代码如下:
protected async override void OnNavigatedTo(NavigationEventArgs e) { var locator = new Geolocator();//开启位置 locator.DesiredAccuracyInMeters = 50;//设置位置服务的准确度 50米 var position = await locator.GetGeopositionAsync(); //得到当前的位置 await myMap.TrySetViewAsync(position.Coordinate.Point,18D);// MapControl控件显示当前位置(point是纬度和经度;18D为当前精度 mapSlider.Value = myMap.ZoomLevel; } private void GetPositionButton_Click(object sender, RoutedEventArgs e) { showMapTextblock.Text = string.Format("{0},{1}", myMap.Center.Position.Longitude, myMap.Center.Position.Latitude); } private async void SetPositonButton_Click(object sender, RoutedEventArgs e) { var setposition = new Windows.Devices.Geolocation.BasicGeoposition(); setposition.Latitude=47; setposition.Longitude=-122; var mypoint=new Windows.Devices.Geolocation.Geopoint(setposition); if (await myMap.TrySetViewAsync(mypoint, 18D)) { // } } private void mapSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { if (myMap!=null) { myMap.ZoomLevel = e.NewValue; double tt; tt = e.NewValue / 18 * 100; sliderTextblock.Text = string.Format("{0}%", (int)tt); } } }
同时还需注意在Package文件中把位置功能打开,相应步骤如下: