【微信开发】 前端

【微信开发】 前端

 1. jssdk -     <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

 2. 验证 js 是否能用

 wx.config({
            debug: false,
            appId: '',
            timestamp: '',
            nonceStr: '',
            signature: '',
            jsApiList: [需要验证的 js 函数]
        });

        wx.ready(function () {
           // 验证通过后调用
        });

        wx.error(function (res) {
       // 验证失败后调用
       alert(JSON.stringify(res));         
        });

3. 选择图片

 function chooseImage() {
            wx.chooseImage({
                count: 9,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],
                success: function (res) {
                    uploadImage(res.localIds, 0);
                }
            });
        }

4. 上传图片,多张图片得传完一张后再传下一张,上传成功后会返回图片id,用于将图片下载到服务器

  function uploadImage(localIds, index) {
            if (localIds.length == 0 || index >= localIds.length) {
                return;
            }
            var id = localIds[index];
            wx.uploadImage({
                localId: id.replace("wxlocalresource", "wxLocalResource"),  // ios得做转换
                success: function (res) {
                    index++;
                    uploadImage(localIds, index)
                },
                fail: function (res) {
                    alert(JSON.stringify(res));
                }
            });
        }

5. 获取位置(微信接口)

 var positionPoint;
 function getPosition() {
            wx.getLocation({
                type: 'gcj02', 
                success: function (res) {
                    positionPoint.latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
                    positionPoint.longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
                    positionPoint.accuracy = res.accuracy; // 位置精度
                },
                fail: function (res) {
                    alert("无法获取当前位置,请打开“定位服务”来允许“微信”确定您的位置");
                }
            });
        }

6. 打开地图(微信接口)

    function openLocation() {
            wx.openLocation({
                latitude: positionPoint.latitude, // 纬度,浮点数,范围为90 ~ -90
                longitude: positionPoint.longitude, // 经度,浮点数,范围为180 ~ -180。
                name: '', // 位置名
                address: '', // 地址详情说明
                scale: 14, // 地图缩放级别,整形值,范围从1~28。默认为最大
                infoUrl: '', // 在查看位置界面底部显示的超链接,可点击跳转
                fail: function (res) {
                    alert(JSON.stringify(res))
                }
            });
        }

7. 获取位置(html5接口)

function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition,showError);
            }
            else { alert("无法获取当前位置"); }
        }

8. 打开地图(腾讯地图)

       function showPosition(position) {
            if (typeof (position) == "undefined" || typeof (position.coords) == "undefined") {
                alert("无法获取当前位置");
                return;
            }
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;
            qq.maps.convertor.translate(new qq.maps.LatLng(lat, lng), 1, function (res) {
                latlng = res[0];
                var map = new qq.maps.Map(document.getElementById("allmap"), {
                    center: latlng,
                    zoom: 13
                });
                var marker = new qq.maps.Marker({
                    map: map,
                    position: latlng
                });
            });
        }

  

  

原文地址:https://www.cnblogs.com/fzz2727551894/p/5291189.html