txmap1.html 5.4 KB
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <title>点击地图事件</title>
    <style type="text/css">
        *{
            margin:0px;
            padding:0px;
        }
        body, button, input, select, textarea {
            font: 12px/16px Verdana, Helvetica, Arial, sans-serif;
        }
        p{
            width:603px;
            padding-top:3px;
            overflow:hidden;
        }
        #container {
            min-width:603px;
            min-height:767px;
        }
    </style>

</head>
<body onload="">
<div class="LogoCon clear" style="font-size:18px;">

    <div style="display:block">
        <span>当前坐标:</span>
        <input type="text" readonly id="pointInput" class="pointInput"
               style="display: inline-block; width: 160px; height: 20px; line-height: 20px; font-size: 14px; font-weight: 700;" />
        <span> <input type="button" value="确定选择" onclick="confirmSelect()" style="margin-left: 8px;font-size:14px;width:80px;" /></span>
    </div>
</div>
<div id="container"></div>
<!--<div id="searchPart">-->
    <!--<input type="text" id="searchInput" placeholder="请输入">-->
    <!--<button id="searchBtn">搜索</button>-->
<!--</div>-->
</body>

<script src="https://map.qq.com/api/gljs?v=1.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>
<!--<script charset="utf-8" src="http://map.qq.com/api/js?v=2.exp"></script>-->
<script>
    // var markersArray=[];
    // var init = function() {
    //     var map = new qq.maps.Map(document.getElementById("container"),{
    //         center: new qq.maps.LatLng(39.916527,116.397128),
    //         zoom: 13,
    //     });
    //     //添加监听事件
    //     qq.maps.event.addListener(map, 'click', function(data) {
    //
    //         for (i in markersArray) {
    //             markersArray[i].setMap(null);
    //         }
    //
    //         var anchor = new qq.maps.Point(6, 6),
    //             size = new qq.maps.Size(24, 24),
    //             origin = new qq.maps.Point(0, 0),
    //            marker = new qq.maps.Marker({
    //             map: map,
    //             position:data.latLng});
    //         markersArray.push(marker);
    //         console.log(data);
    //         document.getElementById('pointInput').value=data.latLng;
    //         setTimeout(function(){
    //          //   alert(data.latLng);
    //         },200);
    //
    //
    //     });
    // }

    var map, marker, infoWindow;
    // 创建信息窗口





    var center = new TMap.LatLng(39.984104, 116.307503);//设置中心点坐标
    //初始化地图
    var map = new TMap.Map("container", {
        center: center
    });

    //初始化marker图层
    var markerLayer = new TMap.MultiMarker({
        id: 'marker-layer',
        map: map
    });

    //监听点击事件添加marker
    map.on("click", (evt) => {


        console.log(evt.latLng);
        document.getElementById('pointInput').value=evt.latLng

    });

    function searchClick() {
        let searchInput = document.getElementById('searchInput').value; // 获取搜索内容
        let url = [
            'https://apis.map.qq.com/ws/place/v1/search',
            '?boundary=nearby(39.984104,116.307503,1000,0)',
            `&keyword=${searchInput}`,
            '&page_size=10&page_index=1&orderby=_distance',
            '&output=jsonp&callback=cb',
            '&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77'
        ].join('');
        jsonp_request(url);
        infoWindow.close(); // 关闭信息窗口
    }
    // 添加搜索按钮点击事件
    const searchBtn = document.getElementById('searchBtn');
    searchBtn.addEventListener('click', searchClick, false);

    function jsonp_request(url) {
        let script = document.createElement('script');
        script.src = url;
        document.body.appendChild(script);
    }

    function confirmSelect(){
        var selectLocation = {};
        var gpsAddr = document.getElementById('currentAddr');
        var location = document.getElementById('pointInput');
        /*   selectLocation.gpsAddr = gpsAddr.innerHTML;*/
        selectLocation.location = location.value;
        console.log(selectLocation.location);
        window.opener.SelectLocation.selectCallback(selectLocation);
        window.close();
    }


    function cb(ret) {
        let newBounds = new TMap.LatLngBounds();
        let markerArr = [];
        if(ret && ret.status===0 && ret.data.length>0) {
            // 将搜索结果保存进数组中
            ret.data.forEach( (item, index) => {
                let position = new TMap.LatLng(item.location.lat, item.location.lng);
                markerArr.push({
                    position: position,
                    properties: {
                        title: item.title,
                        address: item.address,
                        tel: item.tel!==' ' ? item.tel : '暂无'
                    }
                });
                // 寻找搜索结果的边界
                newBounds.extend(position);
            });
            // 更新marker层,显示标记
            marker.setGeometries(markerArr);
            // 地图自适应边界
            map.fitBounds(newBounds, {
                padding: 100  // 边界与内容之间留的间距
            });
        }
    }

</script>
</html>