元生康SCRM系统博客
元生康SCRM系统包含了:CRM、OA、微信V3支付、考试、学院百科、财务、推广、CMS、代理加盟、抽奖、积分、现金券、在线充值活动、及时语音转化技术子系统等

ASP微信服务号(公众号)获取客户精准地址(正/逆地理编码),思路有2个:

第一个方案是通过navigator.geolocation获取到经纬度之后,再通过经纬度进行计算精确的详细地址,但是这个方法有一个弊端不能通用。假如你的H5不是https协议则在苹果ISO系统之下的浏览器无效,所以一般不建议采用此法纳入到项目内;

第二个方案是通过微信开放平台提供的定位功能获取经纬度再去计算详细地址,这样比较稳定安全,毕竟微信公司官方公布出来,针对各种可能的系统BUG都是有考虑到。

下面直接上代码吧,本处需要用到sha1.asp,大家可以直接参考前篇《ASP调用SDK微信分享好友、朋友圈》获取代码

主要代码:

页面:pos.asp

  1 <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
  2 <!--#include file="sha1.asp"-->
  3 <!DOCTYPE html>
  4 <html>
  5 <head>
  6   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7   <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
  8   <title>获取我的详细地址</title>
  9   <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
 10   <script type="text/javascript">
 11     var cls = {
 12        trim: function(str){ return (!str) ? '' : str.toString().replace(/^\s+|\s+$/g, ''); },
 13        parseJSON: function(str){ if(typeof(str)==='object'){ return str; }else{ if(window.JSON){ return JSON.parse(str); } else { return eval('(' + str + ')'); } }  },
 14        getHttp: function(){
 15          return window.XMLHttpRequest?(new XMLHttpRequest()):((new ActiveXObject("Microsoft.XMLHTTP"))?new ActiveXObject("Microsoft.XMLHTTP"):new ActiveXObject("Msxml2.XMLHTTP"));
 16        },
 17        onSubmit: function(varJson){
 18            var xmlHttp = cls.getHttp();
 19            if(xmlHttp==null) return;
 20            var Json    = cls.parseJSON(varJson);
 21            var posturl = Json.url;
 22            var senddata = Json.postdata;
 23            var callback = Json.success;
 24            xmlHttp.open("POST",posturl,true);
 25            xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
 26            xmlHttp.send(senddata);
 27            xmlHttp.onreadystatechange = function(){
 28              if(xmlHttp.readyState==4 && xmlHttp.status==200){
 29                  if(typeof callback=="function"){
 30                    callback(xmlHttp.responseText);
 31                  }
 32              }
 33            }
 34        }
 35     };
 36     var $ = function(id){
 37        var _$ = {};/*var id = arguments[0]; //如果function内不设参数则可以用此处代替*/
 38        function getid(_id){ return document.querySelector(_id); }
 39        _$ = getid(id);
 40        _$.html = function(str){ ifexit(id); getid(id).innerHTML = str; };
 41        return _$;
 42     };
 43   </script>
 44 </head>
 45 <body>
 46 <%
 47  On Error Resume Next
 48  Dim WxObj
 49  Set WxObj = Server.CreateObject("WeixinDLL.WeixinClass")
 50  WxObj.SetAppID      = "AppID"
 51  WxObj.SetAppSecret  = "AppSecret"
 52  
 53  Dim token,JsapiTicket,timestamp,nonceStr,signature,sha1Url
 54  token = WxObj.Get_Token() ' 当前用户的access_token
 55  If Trim(token) = "" Or IsNull(token) Then Response.Write "没有获取到有效的access_token,导致模板消息发送失败!" : Response.End()
 56  sha1Url = "https://www.domain.com/position/pos.asp"
 57  timestamp = WxObj.ToUnixTime(now)
 58  nonceStr  = WxObj.MakeRanNumChar(12)
 59  JsapiTicket = WxObj.Get_Ticket(token)
 60  signature = sha1("jsapi_ticket=" & JsapiTicket & "&noncestr=" & nonceStr & "&timestamp=" & timestamp & "&url=" & sha1Url & "")
 61  Set WxObj = Nothing
 62  If Err Then Response.Write Err.Description
 63 %>
 64 <div style="display:block; width:350px; height:300px; border:1px solid #ddd; background:#f2f2f2; margin:20px auto;">
 65   <div id="showid"></div>
 66   <div id="posid"></div>
 67 </div>
 68 <input type="button" value="获取定位" id="localid" />
 69 <script type="text/javascript">
 70 window.onload = function(){
 71    wx.config({
 72         debug: false,// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
 73         appId: "<%=AppID%>",
 74         timestamp: "<%=timestamp%>",
 75         nonceStr: "<%=nonceStr%>",
 76         signature: "<%=signature%>",
 77         jsApiList: ['checkJsApi','getLocation'/* 所有要调用的 API 都要加到这个列表中*/
 78    });
 79    wx.checkJsApi({
 80       jsApiList: ['getLocation'],
 81       success:function(res){
 82          if(res.checkResult.getLocation==false) {
 83             alert('你的微信版本太低,不支持微信JS接口,请升级到最新的微信版本!');
 84             return;
 85          }
 86       },
 87       fail:function (res) {
 88          alert('checkJsApi fail=' + JSON.stringify(res));
 89       }
 90    });
 91    wx.ready(function () {
 92       wx.getLocation({
 93           type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
 94           success: function (res) {
 95             var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
 96             var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
 97             var speed = res.speed; // 速度,以米/每秒计
 98             var accuracy = res.accuracy; // 位置精度
 99             cls.onSubmit({
100                url:"/position/position.asp",
101                postdata:"latitude=" + latitude + "&longitude=" + longitude + "&speed=" + speed + "&accuracy=" + accuracy,
102                success:function(res){
103                    var json = cls.parseJSON(res);
104                    $("#posid").html("status="+json.status+";<br>address="+json.address);
105                }
106             });
107             
108           }
109       });
110       
111    });
112 }
113 </script>
114 </body>
115 </html>

页面:position.asp

 1 <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
 2 <%
 3  Response.cachecontrol="no-cache"
 4  Response.addHeader "pragma","no-cache"
 5  Response.expires=-1
 6  Response.expiresAbsolute=now-1
 7  Response.CharSet="utf-8"
 8  
 9  Dim QS,ToUrl,Param,Html,Json,latitude,longitude,speed,accuracy,location
10  latitude = Request.Form("latitude")
11  longitude = Request.Form("longitude")
12  speed = Request.Form("speed")
13  accuracy = Request.Form("accuracy")
14  location = latitude & "," & longitude
15  
16  Dim WxObj
17  Set WxObj = Server.CreateObject("WeixinDLL.WeixinClass")
18  WxObj.SetAppID      = "AppID"
19  WxObj.SetAppSecret  = "AppSecret"
20  
21  ToUrl = "http://api.map.baidu.com/reverse_geocoding/v3/"
22  Param = "ak=百度AK&output=json&coordtype=wgs84ll&location=" & location
23  Html = WxObj.file_get_contents(ToUrl, "GET", Param)
24  Set Json = WxObj.parseJSON(Html)
25  'Response.Write Html & "<hr>"
26  'Response.Write "status=" & Json.status & "<hr>"
27  'Response.Write "formatted_address=" & Json.result.formatted_address & "<hr>"
28  'Response.Write "business=" & Json.result.addressComponent.country & "<hr>"
29  If Json.status & "" = "0" Then
30     Response.Write "{""status"":""success"",""address"":""" & Json.result.formatted_address & """}"
31  Else
32     Response.Write "{""status"":""fail"",""address"":""详细地址获取失败!""}"
33  End If
34  Set Json = Nothing
35  Set WxObj = Nothing
36 %>

此处用到的WeixinDLL组件,如有需要可以加微信 z18670092211,联系获取!

posted on 2021-05-04 10:23  元森美  阅读(306)  评论(0)    收藏  举报