Note:
One button, button1, corresponds to a button1_click event, and a textblock, used to display the content. Click this button to get the current GPS coordinate and get the description of the current location based on the coordinate. This application relies on mobile phone GPS hardware support, which cannot be provided by simulators. Call the custom web service method based on the provided GPS longitude and latitude to obtain the location description.
The interface is shown as follows:
The Code is as follows:
Private void button#click (Object sender, routedeventargs e) {// click the key to find my location (depending on the GPS hardware support of the WP mobile phone) geocoordinatewatcher mywatcher = new geocoordinatewatcher (); vaR myposition = mywatcher. position; // Beijing-dimension longitude double latitude = 39.92; double longpolling = 116.46; If (! Myposition. location. isunknown) {latitude = myposition. location. latitude; longpolling = myposition. location. longpolling;} // the text box outputs the longitude and latitude information textblock1.text = "latitude =" + latitude + "\ n" + "longpolling =" + longpolling + "\ n "; // web serive service initialization (usage is different from WM development here, please note) servicereference1.service1soapclient client = new servicereference1.service1soapclient (); client. mylocationcompleted + = new eventhandler <servicereferenc E1.mylocationcompletedeventargs> (client_mylocationcompleted); // call the Web service method client. mylocationasync (latitude, longpolling);} void client_mylocationcompleted (Object sender, servicereference1.mylocationcompletedeventargs e) {// throw new notimplementedexception (); If (E. error! = NULL) {MessageBox. show ("Check the network connection and temporarily cannot access the server. "); return;} else {// receives the returned result textblock1.text + = E. result. tostring ();}}
----------------------------------------------
The Web Service Code is as follows:
Using system; using system. collections; using system. componentmodel; using system. data; using system. LINQ; using system. web; using system. web. services; using system. web. services. protocols; using system. XML. LINQ; namespace lbswebservice {// <summary> // Summary of service1 /// </Summary> [WebService (namespace = "http://tempuri.org/")] [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)] [toolboxitem (F ALSE)] // to allow the use of ASP. Net ajax to call this web service from a script, cancel comments to the downstream. // [System. web. script. services. scriptservice] public class service1: system. web. services. webService {[webmethod] Public String mylocation (Double X, Double Y) {// a Web Service string mylocation = "1 Xizhimen North Street, Haidian District, Beijing "; return mylocation ;}}}
TIPS:
When testing a local web service, you do not need to publish the web service. You only need to use the service1.asmx interface as the startup page to browse in the browser. Vs ide will have a simple service to start the program, copy the URL to add it to the WP client as a web reference.