Cesium中geocoder地理编码修改(天地图geocoder)
程序员文章站
2022-07-01 22:22:18
...
1.重写geocoder._searchCommand
var geocoder = viewer.geocoder.viewModel;
//重写_searchCommand
geocoder._searchCommand = Cesium.createCommand(function () {
if (geocoder.isSearchInProgress) {
cancelGeocode(geocoder);
} else {
geocode(geocoder);
}
});
2.在geocode请求api,并处理返回数据(其它类型地图编码同理,查询对应管网即可)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<title>检索定位</title>
<link href="../Build/Cesium/Widgets/widgets.css" rel="stylesheet">
<link href="css/pretty.css" rel="stylesheet">
<style>
.cesium-viewer-navigationContainer{
display: none;
}
</style>
<script src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/require.min.js" data-main="js/main"></script>
</head>
<body>
<div id="cesiumContainer"></div>
<div id='loadingbar' class="spinner">
<div class="spinner-container container1">
<div class="circle1"></div>
<div class="circle2"></div>
<div class="circle3"></div>
<div class="circle4"></div>
</div>
<div class="spinner-container container2">
<div class="circle1"></div>
<div class="circle2"></div>
<div class="circle3"></div>
<div class="circle4"></div>
</div>
<div class="spinner-container container3">
<div class="circle1"></div>
<div class="circle2"></div>
<div class="circle3"></div>
<div class="circle4"></div>
</div>
</div>
<script>
function onload(Cesium) {
var viewer = new Cesium.Viewer('cesiumContainer',{
geocoder: true//需开启地理编码
});
var imageryLayers = viewer.imageryLayers;
var key = "0352249e0608cf1709ce368f6017c3ca";
//初始化天地图全球中文注记服务,并添加至影像图层
var CIA_CImagery = new Cesium.TiandituImageryProvider({
mapStyle: Cesium.TiandituMapsStyle.VEC_C//矢量
});
CIA_CImagery._url = CIA_CImagery._url + "?tk=" + key;
imageryLayers.addImageryProvider(CIA_CImagery);
var labelImagery = new Cesium.TiandituImageryProvider({
mapStyle : Cesium.TiandituMapsStyle.CIA_C//天地图全球中文注记服务(经纬度投影)
});
labelImagery._url = labelImagery._url + "?tk=" + key;
imageryLayers.addImageryProvider(labelImagery);
//请开发者自行到supermap online官网(http://www.supermapol.com/)申请key
// viewer.geocoder.viewModel.geoKey = 'fvV2osxwuZWlY0wJb8FEb2i5';
$('#loadingbar').remove();
var geocoder = viewer.geocoder.viewModel;
/**
* 原型方法:通过 prototype 添加方法
*/
var geocode = function (viewModelTiandi) {
//天地图请求: http://api.tianditu.gov.cn/geocoder?ds={"keyWord":"延庆区北京市延庆区延庆镇莲花池村前街50夕阳红养老院"}&tk=您的**
//{"msg":"ok","location":{"level":"地产小区","lon":"117.23608","lat":"31.83107"},"searchVersion":"4.8.0","status":"0"}
var requestString = ' http://api.tianditu.gov.cn/geocoder?ds={keyWord:' + viewModelTiandi._searchText + '}&tk=' + key;
return Cesium.loadJson(requestString) //请求url获取json数据
.then(function (results) {
console.log(viewModelTiandi);
//添加viewModelTiandi.entities
viewModelTiandi.entities = [];
var entity = {
id:results.location.level,
position: Cesium.Cartesian3.fromDegrees(results.location.lon, results.location.lat),
point: {
show: true,
color: Cesium.Color.SKYBLUE,
pixelSize: 10,
outlineColor: Cesium.Color.YELLOW,
outlineWidth: 3
},
description: new Cesium.ConstantProperty(viewModelTiandi._searchText)
};
viewModelTiandi.entities.push(entity);
viewer.entities.add(entity);
});
};
//重写_searchCommand
geocoder._searchCommand = Cesium.createCommand(function () {
if (geocoder.isSearchInProgress) {
cancelGeocode(geocoder);
} else {
geocode(geocoder);
}
});
}
</script>
</body>
</html>
参考文章: