lixiaojun
2025-03-01 56226fdae54578cd98a49d7f4848161a1e35bb63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!DOCTYPE html>
<html>
 
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>消防演练创建</title>
    <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css" />
    <link href="../css/public.css" rel="stylesheet" />
    <script src="http://cache.amap.com/lbs/static/es5.min.js"></script>
    <script src="http://webapi.amap.com/maps?v=1.4.0&key=3627ed9deaac2622e26a7169f0c36b1b&plugin=AMap.Geocoder"></script>
    <script src="../js/jquery-2.1.1.min.js"></script>
    <script src="../js/public.js"></script>
    <script src="../js/tools.js"></script>
    <style>
    </style>
</head>
 
<body>
    <div id="container"></div>
    <div class="search-box" id="search_box" style="display: block;">
        <input class="search-text" id="search_text" type="text" placeholder="请输入详细地址" onkeypress="keySearchAddress()" />
        <input class="search-button" id="search_button" type="button" onclick="searchAddress()" />
    </div>
 
    <script>
        let map;//地图对象 使用 ToolBar时,地图对象需要定义位map 否则会报错
        let _map;//地图对象
        
 
        $(document).ready(function () {
 
            try {
                map = _map = new AMap.Map('container', {
                    resizeEnable: true,
                    expandZoomRange: true,
                    zoom: 10,
                    zooms: [3, 20]
                });
 
                _map.on("complete", function () {//地图加载完成
                    callbackObj.loadCompleted();
                });
 
                _map.on("click", function () {
                    if ($("#search_text").css("display") == 'block') {
                        $("#search_text").hide();
                    }
                });
            }
            catch (e) {
                callbackObj.loadFailed();
            }
        })
 
        //获取地图显示层级和中心点
        function getCenter() {
            let level = _map.getZoom();
            let cp = _map.getCenter();
            let jsonObj = { Level: level, CenterPoint: { X: cp.getLng(), Y: cp.getLat() } };
            let jsonStr = JSON.stringify(jsonObj);
            return jsonStr;
        }
 
        //根据详细地址查看地图
        function locateByAddress(address) {
            let geocoder = new AMap.Geocoder({});
            //地理编码,返回地理编码结果
            geocoder.getLocation(address, function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    let geocode = result.geocodes;
                    _map.setCenter([geocode[0].location.getLng(), geocode[0].location.getLat()]);
                    let marker = new AMap.Marker({ position: [geocode[0].location.getLng(), geocode[0].location.getLat()] });
                    _map.setFitView(marker);
                }
                else {
                    let error = { ErrorType: error_type.locate_address, Message: "根据详细地址定位地图失败" };
                    callbackObj.handingError(JSON.stringify(error));
                }
            });
        }
 
        //搜索地址
        function searchAddress() {
            if ($("#search_text").css("display") == 'block') {
                let address = $("#search_text").val();
                if (!isEmpty(address)) {
                    locateByAddress(address);
                }
                $("#search_text").hide();
            }
            else {
                $("#search_text").show();
            }
        }
 
        //enter搜索地址
        function keySearchAddress() {
            if (event.keyCode == 13) {
                searchAddress();
            }
        }
 
 
    </script>
</body>
 
</html>