Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Phone_81_Development_for_Absolute_Beginners

.pdf
Скачиваний:
34
Добавлен:
10.02.2015
Размер:
18.77 Mб
Скачать

A few things I want to point out. Notice that we’re setting the DesiredAccuracyInMeters to 50. Due to technological features of the phone and the GPS system, it is not always possible to get an exact location of the phone. It will try different techniques to get as close as possible. For example, it will not only use the GPS satellite, but it may also use wifi signals and cell tower signals to attempt to triangulat the location. Depending on how accurate you need this to be and what the resources are near the device at the given time that you're attempting to retrieve the position, it could take longer because it has to account for all of these possibilities, wifi signals that are nearby, cell towers, GPS in order to find the location. Honestly, I don't understand how all that works.

However, at some point, we're going to obtain a Geoposition object providing us with a Coordinate. A Coordinate is made up of positioning information. The most important for our purposes is a Point. A Point is simply just a combination of the latitude and longitude and we'll use that information to try and set the view for the map

.

Whenever you see that key word “try” in a method nae, that means it's going to return back to you a true or false: true if the operation was successful, false if it wasn’t. For our purposes in this pithy example, we’ll ignore the return value. In other words, if TrySetViewAsync() fails, then it just fails and it will be quiet about it.

In the TrySetViewAsync() we're passing a Point which is a Geocoordinate position which is basically what we get from position.Coordinate. We also pass in the zoom level. A setting of zero would zoom to see all of North and South America, and a zoom level of 20 would be where we can see in our own back yards. It is a double data type so I add the D next to it just is a way of making the value 18 into a double literal.

Windows Phone 8.1 Development for Absolute Beginners – Page 310

Before we run the app, we’ll need to add the Location capability to our app. Ope the package.appxmanifest, go to the (1) Capabilities tab, and (2) select the Location capability:

Now we should be able to run the application and then see our map control some where near this Redmond Town Center in Redmond, Washington. Unfortunately, I don’t know the streets of Redmond, Washington well enough to verify how far the location we’re given in the MapControl is to the location we set in the Emulator’s Location tab, but I’ll assume it is close.

Windows Phone 8.1 Development for Absolute Beginners – Page 311

Next, I’ll implement two Button controls to set and retrieve the current position of the ma. I’ll add two more row definitions and setting the height of each to 50:

Windows Phone 8.1 Development for Absolute Beginners – Page 312

As a means of further exercising the Geolocation and Map Control, we'll write out the current position of the Map Control to a TextBlock and I’ll programmatically set the position of the Map Control. Two Button controls will trigger these actions. So, I’ll add the following XAML inside the Grid:

<StackPanel Orientation="Horizontal" Grid.Row="1"> <Button Name="getPositionButton"

Content="Get Position" Click="getPositionButton_Click" Margin="0,0,20,0" />

<Button Name="setPositionButton" Content="Set Position" Click="setPositionButton_Click" Margin="0,0,20,0" />

</StackPanel>

<TextBlock Name="positionTextBlock" Grid.Row="2"

FontSize="22" />

I’ll create method stubs for the getPositionButton_Click and setPositionButton_Click by putting my mouse cursor on their names in the XAML and pressing F12.

In each of those two methods, I’ll add the following code:

private void getPositionButton_Click(object sender, RoutedEventArgs e)

{

positionTextBlock.Text = String.Format("{0}, {1}", MyMap.Center.Position.Latitude, MyMap.Center.Position.Longitude);

}

private async void setPositionButton_Click(object sender, RoutedEventArgs e)

{

var myPosition = new Windows.Devices.Geolocation.BasicGeoposition(); myPosition.Latitude = 41.7446;

myPosition.Longitude = -087.7915;

Windows Phone 8.1 Development for Absolute Beginners – Page 313

var myPoint = new Windows.Devices.Geolocation.Geopoint(myPosition); if (await MyMap.TrySetViewAsync(myPoint, 10D))

{

// Haven't really thought that through!

}

}

Hopefully all of this looks familiar, or at least, straight forward.

In the getPositionButton_Click I’m merely retrieving the Map’s Center position, grabbing the latitude and logitude and printing to a TextBlock. This demonstrates how to retrieve all or part of the current location OF THE MAP CONTROL which can be useful when building Mapcentric apps.

In the setPositionButton_Click, I’m hard-coding the position of the Map. Again, I’m using TrySetViewAsync() to do the heavy lifting. However, this time I’m creating a

BasicGeoposition object to contain the latitude and longitude and then creating a Geopoint using the BasicGeoposition as a constructor argument.

When I run the app, I can retrieving the current position into a TextBlock by clicking the

“Get Position” button …

… and can set the position of the Map to a specific location near where I grew up by clicking the “Set Position” button:

Windows Phone 8.1 Development for Absolute Beginners – Page 314

Next, I’ll add a Slider control to perform zooming. I’ll add the following XAML to the

Grid:

<Slider Name="mySlider" Maximum="20"

Minimum="10" ValueChanged="Slider_ValueChanged" Grid.Row="3"

/>

Windows Phone 8.1 Development for Absolute Beginners – Page 315

And will use the F12 shortcut to create the Slider_ValueChanged event handler method, to which I add the following code:

private void Slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)

{

if (MyMap != null) MyMap.ZoomLevel = e.NewValue;

}

Simply put, the slider will allow me to move from 10 to 20 (extreme close up).

Finally, I’ll initiate the Slider’s value at start up by setting its value to the Map Control’s current ZoomLevel in the OnNavigatedTo event handler method:

Now, when I run the app, I can adjust the zoom level:

Windows Phone 8.1 Development for Absolute Beginners – Page 316

I can reset the position using the “Set Position” button, and can zoom in dramatically:

Windows Phone 8.1 Development for Absolute Beginners – Page 317

Finally, if you want to build apps that include the Map Control, you’ll need to provide a MapServiceToken. You’ve see the warning message each during run time. You can obtain a

MapServiceToken from the Windows Phone Dev Center after you register your app (which we do not cover in this series). You would set it like so:

Windows Phone 8.1 Development for Absolute Beginners – Page 318

Windows Phone 8.1 Development for Absolute Beginners – Page 319

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]