制作QQ地图的内嵌坐标拾取器

场景

在我们的应用中如果用到地图,需要让用户输入定位。

然后我们如何让用户可以输入呢?直接让用户输入坐标值是很不友好的。

于是我在QQ地图坐标拾取器的基础上做了修改,让用户可以自己选择地址。

QQ地图示例:

  1. http://lbs.qq.com/tool/getpoint/index.html
  2. http://lbs.qq.com/javascript_v2/case-run.html#sample-event-latlng

废话就少说了,下面是我的代码以及示例。

通过 iframe 嵌入其他网页即可使用,在上级的网页可以访问这个 iframe 文档中的 latlng 对象来获取坐标参数。

示例预览:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <title>腾讯地图 - 坐标拾取器</title>
    <link rel='stylesheet' href='/wp-includes/css/dashicons.min.css?ver=4.2.2' type='text/css' />
    <script src="//libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        body {
            overflow: hidden;
        }
        body, button, input, select, textarea {
            font: 12px/16px Verdana, Helvetica, Arial, sans-serif;
        }
        p{
            width:603px;
            padding-top:3px;
            margin-top:10px;
            overflow:hidden;
        }
        #map_canvas {
            position: fixed; top: 0; left: 0; width: 100%; height: 100%;
        }
        #search_bar {
            position: fixed; bottom: 0; left: 0; width: 100%; height: 30px;
            background: rgba(0, 0, 0, 0.6); color: white;
            box-sizing: border-box; padding: 5px;
        }
        #keyword {
            box-sizing: border-box;
            width: 100%;
        }
        body::after {
            content: ""; display: block;
            position: fixed; left: 50%; top: 50%;
            border-radius: 99px; width: 4px; height: 4px;
            background: red; margin: -2px 0 0 -2px;
            z-index: 1;
        }
        body::before {
            content: ""; display: block;
            position: fixed; left: 50%; top: 50%;
            border-radius: 99px; width: 8px; height: 8px;
            border: 2px solid red; margin: -6px 0 0 -6px;
            z-index: 1;
        }
        #mask {
            position: fixed; top: 0; left: 0; width: 100%; height: 100%;
            background: rgba(0, 0, 0, 0.05);
        }
        #lock {
            position: fixed; right: 6px; top: 6px; width: 32px; height: 32px;
            text-align: center; line-height: 32px; color: white;
            border-radius: 5px; background: rgba(0, 0, 0, 0.5);
            text-shadow: 0 0 10px rgba(0, 0, 0, 0.2); z-index: 101;
        }
    </style>
    <script charset="utf-8" src="http://map.qq.com/api/js?v=1"></script>
    <script>
        var url = location.search; // 获取url中"?"符后的字串
        var objGet = {};
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var segment = str.split("&");
            for(var i = 0; i < segment.length; i ++) {
                objGet[segment[i].split("=")[0]] = decodeURI(segment[i].split("=")[1]);
            }
        }
        var request = {'get': objGet};

        var latlng = (request.get.latlng || '22.95843, 113.08924').split(',');
        var lat = parseFloat(latlng[0]);
        var lng = parseFloat(latlng[1]);

        var searchService,map,markers = [];
        var init = function() {
            var center = new soso.maps.LatLng(lat, lng);
            map = new soso.maps.Map(document.getElementById('map_canvas'),{
                center: center,
                zoomLevel: 15,
                draggable: draggable,
                scrollWheel: scrollWheel,
                zoomInByDblClick: zoomInByDblClick
            });
            searchService = new soso.maps.SearchService();
        };

        // 是否允许移动
        var draggable = !request.get.readonly;
        var scrollWheel = !request.get.readonly;
        var zoomInByDblClick = !request.get.readonly;

        //清除地图上的marker
        function clearOverlays(overlays){
            var overlay;
            while(overlay = overlays.pop()){
                overlay.setMap(null);
            }
        }
        function searchKeyword() {
            var keyword = document.getElementById("keyword").value;
            var region = document.getElementById("region").value;
            clearOverlays(markers);
            var searchRequest = {
                'keyword': keyword,
                'region':region
            };
            var latlngBounds = new soso.maps.LatLngBounds();
            searchService.search(searchRequest, function(results, status) {
                if (status == soso.maps.SearchStatus.OK) {
                    var pois = results.pois;
                    for(var i = 0,l = pois.length;i < l; i++){
                        var poi = pois[i];
                        latlngBounds.extend(poi.latLng);
                        var marker = new soso.maps.Marker({
                            map:map,
                            position: poi.latLng
                        });

                        var decor = new soso.maps.MarkerDecoration({
                            content: i+1,
                            margin: new soso.maps.Size(0, -4),
                            align: soso.maps.ALIGN.CENTER,
                            marker: marker
                        });
                        markers.push(marker);

                        // 点击标记时将标记移动到中心
                        soso.maps.Event.addListener(marker, 'click', function(mk) {
                            map.moveTo(this.getPosition());
                        });

                    }

                    map.fitBounds(latlngBounds);
                } else {
//                    alert("检索没有结果,原因: " + status);
                }
            });
        }

        var keywordTimer = 0;
        function updateKeyword() {
            clearTimeout(keywordTimer);
            setTimeout(searchKeyword, 1000);
        }

        $(function() {
            $('#lock').click(function() {
                $('#mask, #search_bar').toggle();
                $(this).toggleClass('dashicons-lock')
                        .toggleClass('dashicons-admin-network');
            });
        });

    </script>
</head>
<body onload="init()">
<div id="map_canvas"></div>
<div id="search_bar" style="display: none;">
    <input id="keyword" type="text" value="" placeholder="在此输入关键词搜索位置" onkeyup="updateKeyword()">
    <input id="region" type="hidden" value="佛山">
    <!--<input type="button" value="search" onclick="searchKeyword()">-->
</div>
<div id="mask"></div>
<a id="lock" href="javascript:;" class="dashicons dashicons-admin-network"></a>
</body>
</html>

外部嵌入使用方法代码示例:

<iframe id="map_picker" name="map_picker"
        src="<?=get_template_directory_uri()?>/map-picker.html?latlng=<?=$member->member_location?>"></iframe>
<script>
jQuery(function($) {
    $('#map_picker').load(function() {
        setInterval(function() {
            var latlng = frames.map_picker.map.getCenter().toString();
            var $input = $('#member_location');
            if($input.val() != latlng) $input.val(latlng);
        }, 500);
    });
});

【转载请附】愿以此功德,回向 >>

原文链接:https://www.huangwenchao.com.cn/2015/07/qqmap-location-picker.html【制作QQ地图的内嵌坐标拾取器】

《制作QQ地图的内嵌坐标拾取器》有1个想法

发表评论

电子邮件地址不会被公开。 必填项已用*标注