-
Notifications
You must be signed in to change notification settings - Fork 160
/
geocoder.html
168 lines (147 loc) · 6.75 KB
/
geocoder.html
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
<!DOCTYPE html>
<head>
<title>Geocoder</title>
<link href="../Build/Cesium/Widgets/widgets.css" rel="stylesheet">
<script type="text/javascript" src="./js/require.min.js" data-main="./js/main"></script>
<script type="text/javascript" src="./js/gps.js"></script>
</head>
<body>
<div id="cesiumContainer"></div>
<script>
function onload(Cesium) {
var url = 'http://{s}.tianditu.com/img_w/wmts?service=WMTS&version=1.0.0&request=GetTile&tilematrix={TileMatrix}&layer=img&style={style}&tilerow={TileRow}&tilecol={TileCol}&tilematrixset={TileMatrixSet}&format=tiles';
var viewer = new Cesium.Viewer('cesiumContainer', {
imageryProvider : new Cesium.WebMapTileServiceImageryProvider({
url :url,
layer : 'img',
style : 'default',
format : 'tiles',
tileMatrixSetID : 'w',
credit : new Cesium.Credit('天地图全球影像服务'),
subdomains : ['t0','t1','t2','t3','t4','t5','t6','t7'],
maximumLevel : 18
}),
baseLayerPicker : false
});
var imageryLayers = viewer.imageryLayers;
var url2 = 'http://{s}.tianditu.com/cia_w/wmts?service=WMTS&version=1.0.0&request=GetTile&tilematrix={TileMatrix}&layer=cia&style={style}&tilerow={TileRow}&tilecol={TileCol}&tilematrixset={TileMatrixSet}&format=tiles';
var labelImagery = new Cesium.WebMapTileServiceImageryProvider({
url : url2,
layer : 'cia',
style : 'default',
format : 'tiles',
tileMatrixSetID : 'w',
credit : new Cesium.Credit('天地图全球影像中文注记服务'),
subdomains : ['t0','t1','t2','t3','t4','t5','t6','t7']
});
imageryLayers.addImageryProvider(labelImagery);
function cancelGeocode(viewModel) {
viewModel._isSearchInProgress = false;
if (Cesium.defined(viewModel._geocodeInProgress)) {
viewModel._geocodeInProgress.cancel = true;
viewModel._geocodeInProgress = undefined;
}
}
function updateCamera(viewModel, destination) {
viewModel._scene.camera.flyTo({
destination : destination,
complete: function() {
viewModel._complete.raiseEvent();
},
duration : viewModel._flightDuration,
endTransform : Cesium.Matrix4.IDENTITY
});
}
function geocode(viewModel) {
var query = viewModel.searchText;
if (/^\s*$/.test(query)) {
//whitespace string
return;
}
// If the user entered (longitude, latitude, [height]) in degrees/meters,
// fly without calling the geocoder.
var splitQuery = query.match(/[^\s,\n]+/g);
if ((splitQuery.length === 2) || (splitQuery.length === 3)) {
var longitude = +splitQuery[0];
var latitude = +splitQuery[1];
var obj = GPS.gcj_decrypt_exact(latitude,longitude);
var height = (splitQuery.length === 3) ? +splitQuery[2] : 300.0;
if (!isNaN(longitude) && !isNaN(latitude) && !isNaN(height)) {
updateCamera(viewModel, Cesium.Cartesian3.fromDegrees(obj.lon,obj.lat, height));
return;
}
}
viewModel._isSearchInProgress = true;
var smPOI = 'http://www.supermapol.com/iserver/services/localsearch/rest/searchdatas/China/poiinfos.jsonp';
var promise = Cesium.loadJsonp(smPOI, {
parameters : {
keywords : query,
city : "北京市",
location : '',
radius : '',
leftLocation : '',
rightLocation : '',
pageSize : 50,
pageNum : 1
},
callbackParameterName : 'callback',
jsonpName : 'callBack'
});
var geocodeInProgress = viewModel._geocodeInProgress = Cesium.when(promise, function(result) {
if (geocodeInProgress.cancel) {
return;
}
viewModel._isSearchInProgress = false;
if (result.length === 0 || result.totalHints === 0) {
viewModel.searchText = viewModel._searchText + ' (not found)';
return;
}
if(Cesium.defined(viewModel.entities)){
for(var i=0;i<viewModel.entities.length;i++)
{
viewer.entities.remove(viewModel.entities[i]);
}
}
viewModel.entities = [];
var obj;
for(var i=0;i<result.poiInfos.length;i++)
{
var resource = result.poiInfos[i];
viewModel._searchText = resource.name;
var location = resource.location;
obj = GPS.gcj_decrypt_exact(location.y,location.x);
var entity = {
id:resource.name + i,
position : Cesium.Cartesian3.fromDegrees(obj.lon,obj.lat),
point : {
show : true, // default
color : Cesium.Color.SKYBLUE, // default: WHITE
pixelSize : 10, // default: 1
outlineColor : Cesium.Color.YELLOW, // default: BLACK
outlineWidth : 3 // default: 0
}
};
entity.description = new Cesium.ConstantProperty(resource.name);
viewModel.entities.push(entity);
viewer.entities.add(entity);
}
updateCamera(viewModel, Cesium.Cartesian3.fromDegrees(obj.lon,obj.lat, height));
}, function() {
if (geocodeInProgress.cancel) {
return;
}
viewModel._isSearchInProgress = false;
viewModel.searchText = viewModel._searchText + ' (error)';
});
}
var geocoder = viewer.geocoder.viewModel;
geocoder._searchCommand = Cesium.createCommand(function() {
if (geocoder.isSearchInProgress) {
cancelGeocode(geocoder);
} else {
geocode(geocoder);
}
});
}
</script>
</body>