Implement GPS, location API and call Web Services in Windows Phone 7

Source: Internet
Author: User
View code

Mainpage. XAML:

<Phone: phoneapplicationpage
X: class = " Geopositionwebservice. mainpage "
Xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/presentation "
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 " D: designwidth = " 480 " D: designheight = " 768 "
Fontfamily = " {Staticresource phonefontfamilynormal} "
Fontsize = " {Staticresource phonefontsizenormal} "
Foreground = " {Staticresource phoneforegroundbrush} "
Supportedorientations = " Portrait " Orientation = " Portrait "
Shell: systemtray. isvisible = " True " >

<! -- Layoutroot Is The root Grid Where All page content Is Placed -->
<Grid X: Name = " Layoutroot " Background = " Transparent " >
<Grid. rowdefinitions>
<Rowdefinition Height =" Auto " />
<Rowdefinition Height = " * " />
</Grid. rowdefinitions>

<! -- Titlepanel contains the name of the application and page title -->
<Stackpanel X: Name = " Titlepanel " Grid. Row = " 0 " Margin = " " >
<Textblock X: Name = " Applicationtitle " TEXT = " My application " Style = " {Staticresource phonetextnormalstyle} " />
<Textblock X: Name =" Pagetitle " TEXT = " Page name " Margin = " 9,-7, 0, 0 " Style = " {Staticresource phonetexttitle1style} " />
</Stackpanel>

<! -- Contentpanel-place additional content here -->
<Grid X: Name = " Contentpanel " Grid. Row = " 1 " Margin = " 12, 0, 12, 0 " >
<Textblock Height = " 39 " Horizontalalignment = " Left " Margin = " 12, 6, 0, 0 " Name = " Textblock1 " TEXT = "" Verticalalignment = " Top " Width = " 438 " />
<Button content =" Find me " Height = " 72 " Horizontalalignment = " Left " Margin = " 272,51, 0, 0 " Name = " Button1 " Verticalalignment = " Top " Width = " 160 " Click = " Button#click " />
</GRID>
</GRID>

<! -- Sample Code showing usage of ApplicationBar -->
<! -- <Phone: phoneapplicationpage. ApplicationBar>
<Shell: ApplicationBar isvisible = " True " Ismenuenabled = " True " >
<Shell: applicationbariconbutton iconuri = " /Images/appbar_button1.png " TEXT = " Button 1 " />
<Shell: applicationbariconbutton iconuri = " /Images/appbar_button2.png " TEXT = " Button 2 " />
<Shell: ApplicationBar. menuitems>
<Shell: applicationbarmenuitem text = " Menuitem 1 " />
<Shell: applicationbarmenuitem text = " Menuitem 2 " />
</Shell: ApplicationBar. menuitems>
</Shell: ApplicationBar>
</Phone: phoneapplicationpage. ApplicationBar> -->

</Phone: phoneapplicationpage>
View code

Mainpage. XAML. CS

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. net;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. documents;
Using System. Windows. input;
Using System. Windows. Media;
Using System. Windows. Media. animation;
Using System. Windows. shapes;
Using Microsoft. Phone. controls;
Using System. device. location;

Namespace Geopositionwebservice
{
Public Partial Class Mainpage: phoneapplicationpage
{
// Constructor
Public Mainpage ()
{
Initializecomponent ();
}

Private Void Button#click ( Object Sender, routedeventargs E)
{
Geocoordinatewatcher mywatcher = New Geocoordinatewatcher ();
VaR Myposition = mywatcher. position;
// Hello, a friend from Wuhan. Your current latitude is 30.5833, And the longitude is 114.2667.
// In this case, since we're not working with a device, I'll just
// Set a default value. If we cannot get the current location,
// Then we'll default to Wuhan.
// WebService URL: Http://msrmaps.com/TerraService2.asmx
Double Latitude = 30.5833 ;
Double Longpolling = 114.2667 ;
If (! Myposition. Location. isunknown)
{
Latitude = myposition. Location. Latitude;
Longpolling = myposition. Location. longpolling;
}
Myterraservice. terraservicesoapclient client = New Myterraservice. terraservicesoapclient ();
Client. convertlonlatpttonearestplacecompleted + = New Eventhandler <myterraservice. convertlonlatpttonearestplacecompletedeventargs> (client_convertlonlatpttonearestplacecompleted );
Client. convertlonlatpttonearestplaceasync ( New Myterraservice. lonlatpt {lat = latitude, Lon = longpolling });
}

Void Client_convertlonlatpttonearestplacecompleted ( Object Sender, myterraservice. convertlonlatpttonearestplacecompletedeventargs E)
{
// Throw new notimplementedexception ();
Textblock1.text = E. result;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.