核心代码如下:
_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))
}