Skip to content

Instantly share code, notes, and snippets.

@ca0v
Created January 5, 2016 20:31
Show Gist options
  • Save ca0v/2c88d62453d75b3d0375 to your computer and use it in GitHub Desktop.
Save ca0v/2c88d62453d75b3d0375 to your computer and use it in GitHub Desktop.
AGS route-solving proxy
/**
* http://sampleserver6.arcgisonline.com/arcgis/sdk/rest/index.html#/Network_Layer/02ss0000009p000000/
*/
class Route {
private ajax: Ajax;
constructor(url: string) {
this.ajax = new Ajax(url);
}
/**
* http://sampleserver6.arcgisonline.com/arcgis/sdk/rest/index.html#/Solve_Route/02ss0000001t000000/
*/
solve(data: {
stops: Array<{x: number; y: number;}>;
returnDirections?: boolean;
returnRoutes?: boolean;
}) {
let req = Object.assign({
returnDirections: true,
returnRoutes: true,
preserveFirstStop: true,
preserveLastStop: true,
directionsLanguage: "en",
outputGeometryPrecisionUnits: "esriDecimalDegrees",
directionsOutputType: "esriDOTComplete",
directionsLengthUnits: "esriNAUMiles",
f: "pjson"
}, data);
req.stops = <any>data.stops.map(p => `${p.x},${p.y}`).join(';');
return this.ajax.get(req);
}
}
let usage_example = () => {
let route = new Route("http://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/Route/solve");
route.solve({stops: [{x: -117.141724, y: 32.7122},{x: -117.141724, y: 32.72}]}).then((value: {
error?: {
code: number;
message: string;
details: string[];
}
}) => {
value = JSON.parse(<any>value);
if (value.error) {
console.error(value.error.message);
} else {
console.log("solve", value);
}
return value;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment