duheng
2025-04-03 82f8a7ead37ff50dec3d0d40e572d4331f14a8b2
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!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="https://webapi.amap.com/maps?v=2.0&key=3627ed9deaac2622e26a7169f0c36b1b&plugin=AMap.Geocoder,AMap.Autocomplete,AMap.PlaceSearch"></script>
    <script src="../js/jquery-2.1.1.min.js"></script>
    <script src="../js/public.js"></script>
    <script src="../js/tools.js"></script>
</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;//地图对象 高德jsapi用
        let _map;//地图对象
        let _marker;//点
        let _marker_overlay;//点标记
        let _callBackObj;//交互对象
 
        $(document).ready(function () {
 
            try {
                map = _map = new AMap.Map('container', {
                    viewMode: '2D', //默认使用 2D 模式
                    resizeEnable: true,
                    expandZoomRange: true,
                    zoom: 15,
                    zooms: [10, 22],
                    center: [121.507845, 31.20351], //地图中心点
                });
 
                //地图点击时关闭搜索面板
                _map.on("click", function () {
                    if ($("#search_text").css("display") == 'block') {
                        $("#search_text").hide();
                    }
                });
 
                //地图点击
                _map.on("click", onMapDrawClick);
 
                _map.on("zoomend", onMapZoomend);
 
                //地图加载完成事件
                _map.on("complete", function () {
                    _callBackObj = window.chrome.webview.hostObjects.callBackObj;
                    _callBackObj.LoadCompleted();
                });
 
            }
            catch (e) {
                _callBackObj = window.chrome.webview.hostObjects.callBackObj
                _callBackObj.LoadFailed();
            }
        })
 
        //加载点标记
        function loadMarker(marker) {
            if (isEmpty(marker)) {
                return false;
            }
            _map.clearMap();
            _marker = marker;
            let lnglat = [_marker.Point.X, _marker.Point.Y];
            _marker_overlay = new AMap.Marker({
                position: lnglat,
                icon: new AMap.Icon({
                    size: new AMap.Size(32, 32),  //图标大小
                    image: "../img/current.png",
                    imageOffset: new AMap.Pixel(0, 0)
                }),
                offset: new AMap.Pixel(-15, -21)
            });
            _marker_overlay.setMap(_map);
            _map.setFitView();
 
            if (isEmpty(_marker.Address)) {
                let geocoder = new AMap.Geocoder({});
                geocoder.getAddress(lnglat, function (status, result) {
                    if (status === 'complete' && result.info === 'OK') {
                        let address = result.regeocode.formattedAddress;
                        _marker.Address = address;
                        _callBackObj.SetMarker(JSON.stringify(_marker));
                    }
                });
            }
            return true;
        }
 
         //加载地图矩形范围
         function LoadMapBounds(southWestObj,northEastObj) {
            if (isEmpty(southWestObj)) {
                return false;
            }
            if (isEmpty(northEastObj)) {
                return false;
            } 
            
            let southWest  = new AMap.LngLat( southWestObj.X, southWestObj.Y);
            let northEast  = new AMap.LngLat( northEastObj.X, northEastObj.Y);
            let mapBounds =new AMap.Bounds(southWest,northEast)
 
            _map.setBounds( mapBounds,true); 
           
            return true;
         }
 
 
        //地图缩放事件
        function onMapZoomend() {
            var bounds = map.getBounds();
            let northEast = bounds.northEast;
            let southWest = bounds.southWest;
 
            let southWestObj={X:southWest.lng ,Y:southWest.lat }
            let northEastObj={X:northEast.lng ,Y:northEast.lat }
 
            _callBackObj.SetMapBounds(JSON.stringify(southWestObj), JSON.stringify(northEastObj)); 
        }
 
        //地图绘制点标记点击事件
        function onMapDrawClick(e) {
            _map.clearMap();
            let lnglat = [e.lnglat.getLng(), e.lnglat.getLat()];
            _marker_overlay = new AMap.Marker({
                position: lnglat,
                icon: new AMap.Icon({
                    size: new AMap.Size(32, 32),  //图标大小
                    image: "../img/current.png",
                    imageOffset: new AMap.Pixel(0, 0)
                }),
                offset: new AMap.Pixel(-15, -21)
            });
            _marker_overlay.setMap(_map);
            _marker = { Point: { X: lnglat[0], Y: lnglat[1] }, Address: "" };
            let geocoder = new AMap.Geocoder({});
            geocoder.getAddress(lnglat, function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    let address = result.regeocode.formattedAddress;
                    _marker.Address = address;
                    _callBackObj.SetMarker(JSON.stringify(_marker));
                }
            });
        }
 
 
 
        //根据详细地址查看地图
        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>