之前做了一个微信定位,搜索同地区的相关商家,直接上源码
前端
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=A1f31379ad5aa6e47617df2dd4e2abfe"></script> <script> wx.config({ debug: false, appId: '<%=appid %>', timestamp: '<%=time %>', nonceStr: '<%=noncestr %>', signature: '<%=resp %>', jsApiList: ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'uploadImage', 'chooseImage', 'getLocation'] }); wx.ready(function () { wx.checkJsApi({ jsApiList: [ 'getNewworkType', 'previewImage', 'getLocation' ], success: function (res) { } }); wx.getLocation({ type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02' success: function (res) { var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90 var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。 var speed = res.speed; // 速度,以米/每秒计 var accuracy = res.accuracy; // 位置精度 getBaiduPosition(latitude, longitude); } }); wx.showOptionMenu(); wx.showMenuItems({ menuList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 要显示的菜单项,所有menu项见附录3 }); wx.onMenuShareTimeline({ title: '<%=string.IsNullOrEmpty(MemberEntity.ShopName)?MemberEntity.Name+"的国际名品店":MemberEntity.ShopName %> <%=!string.IsNullOrEmpty(SName)?"_"+SName:"" %>', // 分享标题 link: '<%=MainUrl%>' + '/page/shop/?shopno=<%=MemberEntity.ShopNo %>&no=<%=SNo%>', // 分享链接 imgUrl: '<%=MemberEntity.HeadUrl %>', success: function () { // 用户确认分享后执行的回调函数 }, cancel: function () { // 用户取消分享后执行的回调函数 } }); wx.onMenuShareAppMessage({ title: '<%=string.IsNullOrEmpty(MemberEntity.ShopName)?MemberEntity.Name+"的国际名品店":MemberEntity.ShopName %> <%=!string.IsNullOrEmpty(SName)?"_"+SName:"" %>', // 分享标题 desc: '<%=MemberEntity.ShopDesc.Replace(" ","").Replace(" ","") %>', // 分享描述 link: '<%=MainUrl%>' + '/page/shop/?shopno=<%=MemberEntity.ShopNo %>&no=<%=SNo%>', // 分享链接 imgUrl: '<%=MemberEntity.HeadUrl %>', type: '', // 分享类型,music、video或link,不填默认为link dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空 success: function () { // 用户确认分享后执行的回调函数 }, cancel: function () { // 用户取消分享后执行的回调函数 } }); }); localStorage["OMGAllisJump"] = "0"; (function () { var bp = document.createElement('script'); bp.src = '//push.zhanzhang.baidu.com/push.js'; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script>
转换微信坐标为百度坐标
public KeyValuePair<string,string> getBaiduPosition() { //转换微信坐标为百度坐标 LogHelper.MyWriteLogTxt("------------开始", 9999); string result = string.Empty; string xx = DNTRequest.GetString("xx"); string yy = DNTRequest.GetString("yy"); string url = "http://api.map.baidu.com/geoconv/v1/?coords=" + yy + "," + xx + "&from=3&to=5&ak=" + CommonHelper.GetAppSettings("akkey") + ""; result = RequestHelper.SendGet(url); LogHelper.MyWriteLogTxt("------------结束"+ result, 9999); MapReturn r = JsonConvert.DeserializeObject<MapReturn>(result); string res = JsonConvert.SerializeObject(r.result); LogHelper.MyWriteLogTxt(res, 9999); List<MapReturnResult> ma = JsonConvert.DeserializeObject<List<MapReturnResult>>(res); string x = ma[0].x; string y = ma[0].y; LogHelper.MyWriteLogTxt(x, 9999); LogHelper.MyWriteLogTxt(y, 9999); return new KeyValuePair<string, string>(x, y); }
百度坐标获取实际地址位置
public string getBaiduAddress(string x, string y) { //百度坐标获取实际地址位置 LogHelper.MyWriteLogTxt("------------获取地理位置开始", 8888); string locaMapUrl = "http://api.map.baidu.com/geocoder/v2/?ak=" + CommonHelper.GetAppSettings("akkey") + "&location=" + y + "," + x + "&output=json&pois=1"; string locaMapres = RequestHelper.SendGet(locaMapUrl); LogHelper.MyWriteLogTxt("------------获取地理位置结束" + locaMapres, 8888); MapReturn r = JsonConvert.DeserializeObject<MapReturn>(locaMapres); string res = JsonConvert.SerializeObject(r.result); LogHelper.MyWriteLogTxt("----------"+res, 8888); AddressResult addressResult = JsonConvert.DeserializeObject<AddressResult>(res); string addresult = JsonConvert.SerializeObject(addressResult.addressComponent); AddressComponent address = JsonConvert.DeserializeObject<AddressComponent>(addresult); string addressRes = JsonConvert.SerializeObject(address); LogHelper.MyWriteLogTxt("返回:" + addressRes, 8888); return addressRes; }