核心代码如下:
_doFindRoute: function (fromStop, toStop) {
this.routeGraphicLayer.clear()
this.stopSymbol.setOffset(0, 20)
this.routeGraphicLayer.add(new Graphic(fromStop, this.stopSymbol))
routeTask = new RouteTask(this.routeTaskLayer)
routeParams = new RouteParameters()
routeParams.stops = new FeatureSet()
routeParams.outSpatialReference = this._map.spatialReference
var stop1 = new Graphic({
geometry: fromStop,
symbol: this.stopSymbol
})
var stop2 = new Graphic({
geometry: toStop,
symbol: this.stopSymbol
})
routeParams.stops.features.push(stop1)
routeParams.stops.features.push(stop2)
routeParams.returnRoutes = false
routeParams.returnDirections = true
routeParams.directionsLengthUnits = Units.METERS
var routeSymbol = this.routeSymbol
var graphicLayer = this._graphicLayer
var map = this._map
var _this = this
routeTask.solve(routeParams,
function (results) {
results.routeResults.forEach(function (aResult) {
aResult.directions.features.forEach(function (aRoute) {
var aRouteSymbol = new Graphic(aRoute.geometry, routeSymbol)
_this.routeGraphicLayer.add(aRouteSymbol)
if (aRoute.attributes.length > 0) {
map.setExtent(aRoute.geometry.getExtent().expand(2))
}
})
})
}.bind(this),
function (errors) {
console.log(errors.details[0])
tips('未查询到路线!')
}.bind(this))
}
本文介绍了一个基于特定地理信息系统(GIS)的路线查找算法实现过程。该算法通过定义起点与终点,利用路线任务(RouteTask)和服务参数(RouteParameters)来计算两点间路径,并在地图上展示查询结果。如果查询失败,还会给出相应的错误提示。
710

被折叠的 条评论
为什么被折叠?



