__uniappopenlocation.html 13.3 KB
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8" />
    <script>
        var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'))
        document.write('<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (coverSupport ? ', viewport-fit=cover' : '') + '" />')
    </script>
    <title></title>
    <style>
        html,
        body,
        .container {
            margin: 0;
            padding: 0;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            background: #ffffff;
        }

        #map {
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 80px;
            bottom: calc(80px + constant(safe-area-inset-bottom));
            bottom: calc(80px + env(safe-area-inset-bottom));
        }

        #poi {
            position: absolute;
            left: 0;
            right: 0;
            bottom: 0;
            bottom: constant(safe-area-inset-bottom);
            bottom: env(safe-area-inset-bottom);
            height: 80px;
            background: #FFFFFF;
        }

        .poi-info {
            width: 100%;
            padding: 23px 16px 23px 18px;
            box-sizing: border-box;
            background: #FFFFFF;
        }

        .poi-name {
            font-size: 17px;
            line-height: 17px;
            color: #111111;
            display: block;
            margin-right: 70px;
            word-wrap: break-word;
        }

        .poi-addr {
            font-size: 13px;
            line-height: 13px;
            color: #666666;
            display: block;
            margin-top: 4px;
            margin-right: 70px;
            word-wrap: break-word;
        }

        .poi-nav {
            display: block;
            position: absolute;
            top: 10px;
            right: 16px;
            width: 60px;
            height: 60px;
            border-radius: 60px;
            overflow: hidden;
        }

        .poi-nav i,
        .poi-nav span {
            position: absolute;
            top: 0;
            width: 100%;
            height: 100%;
            display: inline-block;
            border-radius: 60px;
        }

        .poi-nav i {
            background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAMAAAAOusbgAAACnVBMVEUAAAA9avA+avA9avA/bPE8afA+avM9afA9avA9au9Ac/I+bPQ9avA8a/E8au8+avY9avA9avBAgP88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lAWz3vUgwtc3ku9JKZszA0qxa894WEZ4kaAI5WILa1QTsTsz9ggB19UiWeDh6mIeM8F7xZhObhKVIpzIqjhpi0ze6kCqJLbpxHn7Nx5RhJjz6uwdNkBaEefuPyehzIo814+7lccTQ+wvQ4KnJEU04kYG8oAoH6V2gHESA8p/lwq0Q7QXOrjToAY3XsA7tcgYfJjhLkwt7dVTQ8p0oOoe4jk7NNICBAf7gCAG7XoQ37GNMYxhY3fJ4HTdU54ECSumQ8w9SjADHJnjl4mwkS5SQsb0wPllcdgsdRYBPWfBIpr71YBLK7BcoD7LYhYAsGUUALl9NTAYmidJDUuNkGb21+863MAkPq/GeDmCnbxCJZkyF3w9fAXLliChOLZ0LT1N3B+tU6elafWITom9XICTshNhb62saR/TjrhNBIcpxrB+R1pLgCAc5O/X40yP9Sxv1yDQie8HGuc8OPVn3YvWnChqRO8NEk5tFuwtLwKqn75taFyba17Wk1ddcupLqy8RyADiMxa5YQXPcW3AvyY9/i1iv2wHBF7OuABVl6AAI7hFFDifFCKjyWpeeT3PxrXl18/eaeoQXqv/oS/EHHD5QrvlHzp73/+dYJMN5bMUGI7JanJnsZt+Y8MjbNQZqu7du0ELzXIhDiakrjWEwMlVa8naVmU4gQdEWMo5Rg6YsqmFKDoSTKt5EZHrEnUIiPdSBjDUFalA6KZoZBMB4Yn8iqd41UsyKs8kldBKK8SWF5Fv7zKnHkVdvMvZedfvM//uAL/Axr8j6TwP4RDSM9Xc+woP53TQStOR8v0wZw5XvfDdPTjg4VW+figtVDb8cH/AZ+Eg+9jqX0NAAAAAElFTkSuQmCC) no-repeat;
            background-size: 100%;
        }
    </style>
</head>

<body>
    <div id="container" class="container">
        <div id="map"></div>
        <div id="poi">
            <div class="poi-info">
                <span class="poi-name"></span>
                <span class="poi-addr"></span>
                <div class="poi-nav">
                    <i></i>
                    <span></span>
                </div>
            </div>
        </div>
    </div>
    <script>
        var loc;
        var serviceWebview;
        var back = function() {
            var webview = plus.webview.currentWebview()
            if (webview.__uniapp_statusbar_style === 'light') {
                plus.navigator.setStatusBarStyle('light')
            }
            webview.close('auto');
        };

        var isIos = !!navigator.userAgent.match(/iPhone|iPad|iPod/i);

        document.addEventListener('plusready', function() {
            serviceWebview = plus.webview.getWebviewById('__W2A_CONTEXT_') || plus.webview.getLaunchWebview();
            plus.key.addEventListener('backbutton', back);


        })

        /**
         * 调用系统第三方程序进行导航
            */
        function openSysMap(lat, lng, title) {
            /**
             * 网页版地图源
                */
            var mapsSourceWeb = [
                {
                    title: '腾讯地图网页版',
                    getUrl: function() {
                        var url
                        url = 'https://apis.map.qq.com/uri/v1/routeplan?type=drive' + '&to=' +
                            encodeURIComponent(title) + '&tocoord=' + encodeURIComponent(lat + ',' + lng) +
                            '&referer=APP'
                        return url
                    }
                },
                {
                    title: '谷歌地图网页版',
                    getUrl: function() {
                        var url
                        url = 'https://maps.google.cn/?daddr=' + encodeURIComponent(title) + '&sll=' +
                            encodeURIComponent(lat + ',' + lng)
                        return url
                    }
                }
            ]
            /**
             * APP版地图源
                */
            var mapsSource = [
                {
                    title: '高德地图',
                    pname: 'com.autonavi.minimap',
                    action: !isIos ? 'amapuri://' : 'iosamap://',
                    getUrl: function() {
                        var url
                        if (!isIos) {
                            url = 'amapuri://route/plan/'
                        } else {
                            url = 'iosamap://path'
                        }
                        url += '?sourceApplication=APP&dname=' + encodeURIComponent(title) + '&dlat=' + lat +
                            '&dlon=' + lng + '&dev=0'
                        return url
                    }
                },
                {
                    title: '百度地图',
                    pname: 'com.baidu.BaiduMap',
                    action: 'baidumap://',
                    getUrl: function() {
                        var url
                        url = 'baidumap://map/direction?destination=' + encodeURIComponent('latlng:' + lat +
                            ',' + lng + '|name:' + title) + '&mode=driving&src=APP'
                        return url
                    }
                },
                {
                    title: '腾讯地图',
                    pname: 'com.tencent.map',
                    action: 'qqmap://',
                    getUrl: function() {
                        var url
                        url = 'qqmap://map/routeplan?type=drive' + (isIos ? ('&from=' + encodeURIComponent(
                            '我的位置')) : '') + '&to=' + encodeURIComponent(title) + '&tocoord=' +
                            encodeURIComponent(lat + ',' + lng) + '&referer=APP'
                        return url
                    }
                },
                {
                    title: '谷歌地图',
                    pname: 'com.google.android.apps.maps',
                    action: 'comgooglemapsurl://',
                    getUrl: function() {
                        var url
                        if (!isIos) {
                            url = 'https://maps.google.com/'
                        } else {
                            url = 'comgooglemapsurl://maps.google.com/'
                        }
                        url += '?daddr=' + encodeURIComponent(title) + '&sll=' + encodeURIComponent(lat + ',' +
                            lng)
                        return url
                    }
                }
            ]
            var maps = []
            mapsSource.forEach(function(mapsSource) {
                var installed = plus.runtime.isApplicationExist({
                    pname: mapsSource.pname,
                    action: mapsSource.action,
                })
                if (installed) {
                    maps.push(mapsSource)
                }
            })
            if (isIos) {
                maps.unshift({
                    title: 'Apple 地图',
                    getUrl: function() {
                        var url
                        url = 'https://maps.apple.com/?daddr=' + encodeURIComponent(title) + '&sll=' +
                            encodeURIComponent(lat + ',' + lng)
                        return url
                    }
                })
            }
            if (maps.length === 0) {
                maps = maps.concat(mapsSourceWeb)
            }
            plus.nativeUI.actionSheet({
                title: '导航方式',
                cancel: '取消',
                buttons: maps,
            }, function(res) {
                var index = res.index
                var map
                if (index > 0) {
                    map = maps[index - 1]
                    plus.runtime.openURL(map.getUrl(), function() { }, map.pname)
                }
            })
        }

        var ZOOM = 13
        window.__openLocation__ = function(params) {
            var mapElem = document.getElementById('map');
            var poiNameElem = document.querySelector('.poi-name');
            var poiAddrElem = document.querySelector('.poi-addr');
            var poiNavElem = document.querySelector('.poi-nav');
            var latitude = params.latitude;
            var longitude = params.longitude;
            var scale = params.scale;
            var name = params.name;
            var address = params.address;
            var point = new plus.maps.Point(longitude, latitude);
            var map = plus.maps.create('map', {
                center: point,
                zoom: scale || ZOOM,
                top: 0,
                left: 0,
                width: '100%',
                height: mapElem.offsetHeight

            });
            var marker = new plus.maps.Marker(point);
            marker.setIcon('__uniappmarker@3x.png');
            if (name) {
                poiNameElem.innerText = name;
                // marker.setLabel(name);
            }
            if (address) {
                poiAddrElem.innerText = address;
                //                             var bubble = new plus.maps.Bubble(address);
                //                             marker.setBubble(bubble);
            }
            map.addOverlay(marker);
            plus.webview.currentWebview().append(map);
            var userPoint = false
            map.getUserLocation(function(state, point) {
                if (state) {
                    plus.nativeUI.toast('定位失败!');
                } else {
                    userPoint = point;
                }
            })

            poiNavElem.addEventListener('click', function() {
                openSysMap(latitude, longitude, name)
            });
        }
    </script>
</body>

</html>