Windows Phone开发之GPS、Web Service服务使用简介

说明:
一个按钮button1,对应一个button1_click事件,一个TextBlock,用来显示内容。点击该按钮可以得到当前GPS坐标,并根据坐标得出当前所在的位置描述。该应用依赖于手机GPS的硬件支持,模拟器提供不了。根据提供的GPS的经纬度调用自定义的Web Service方法来得到位置描述。

界面图如下:

代码如下:

 private void button1_Click(object sender, RoutedEventArgs e)
        {
            //点击按键 找到我的位置(依赖于WP手机的GPS硬件支持)
            GeoCoordinateWatcher myWatcher = new GeoCoordinateWatcher();
            var myPosition = myWatcher.Position;
            
			//北京维度 经度
            double latitude = 39.92;
            double longitude = 116.46;
            if (!myPosition.Location.IsUnknown) {
                latitude = myPosition.Location.Latitude;
                longitude = myPosition.Location.Longitude;
            }
			//文本框输出经纬度信息
            textBlock1.Text = "latitude==" + latitude + "\n" + "longitude==" + longitude+"\n";

            //Web serive服务初始化(此处用法与WM开发有不同,需注意)
            ServiceReference1.Service1SoapClient client = new ServiceReference1.Service1SoapClient();
            client.myLocationCompleted +=new EventHandler<ServiceReference1.myLocationCompletedEventArgs>(client_myLocationCompleted);
            //调用Web Service方法
            client.myLocationAsync(latitude, longitude);
         }

        void client_myLocationCompleted(object sender, ServiceReference1.myLocationCompletedEventArgs e)
        {
            //throw new NotImplementedException();
			if(e.Error!=null){
				MessageBox.Show("请检查网络连接,暂时访问不到服务端.");
                return;
			}else{
            //接收返回结果
            textBlock1.Text += e.Result.ToString();
			}
        }

----------------------------------------------

Web Service代码如下:

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>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService {

        [WebMethod]
        public string myLocation(double x,double y) {
            //自己随便写的一个Web Service服务
            string myLocation="北京海淀区西直门北大街1号";
            return myLocation;
        }
    }
}

小技巧:
本地Web Service测试时,没必要将该Web Service服务发布,只需要将Service1.asmx界面作为启动页在浏览器浏览。VS IDE会有简易服务启动该程序,将该地址URL复制即可添加到WP客户端作为web引用。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值