Solved! Go to Solution.
I need it in javascript as all my codes are in javascript and html. I am not using C#.
That why I said you'd have to port it, i.e. from C# to JavaScript.
Have you looked at the code? It's not that complicated.
Ya I have seen the codes inside the file. They have something called the Path inside. What do I change the Path to? Polyline?
Maybe....you could do a little research, find out what a "Path" (in that context) is and then relate it to the classes available in the JS API.
Here's somewhere to start:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/002m/002m0000045n000000.htm
Which might lead you to:
http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/polyline.htm#addPath
function check(){
alert("stage 1");
//alert("\nCompressed geometry with no Ms, no Zs:");
//PrintPath(CreatePathFromCompressedGeometry("+1m91-6fkfr+202tp+k+f+7+3+34+2d"));
//alert("\nCompressed geometry with both Ms, Zs:");
//PrintPath(CreatePathFromCompressedGeometry("+0+1+3+1+emjd+3j07m+3+0+0+1-3-1|+9og+0+lv4+0+lv4|+5rg+uq+r9+au+168"));
PrintPath(CreatePathFromCompressedGeometry("+1+hrt+139j+0+0"));
}
function PrintPath(pth){
alert("stage 5");
//alert(pth);
for (i = 0; i < pth.length; i++){
var p = pth;
alert(p);
//PointN p = pth.PointArray as PointN;
//alert(p.x + ",\t" + p.y);
}
}
function CreatePathFromCompressedGeometry(compresedGeometry){
alert("stage 2");
var points = [];
var nIndex = 0;
var dMultBy = parseFloat(ExtractInt(compresedGeometry, nIndex)); // exception
var nLastDiffX = 0;
var nLastDiffY = 0;
var nLength = compresedGeometry.length; // reduce call stack
while (nIndex != nLength) {
// extract number
var nDiffX = ExtractInt(compresedGeometry, nIndex); // exception
var nDiffY = ExtractInt(compresedGeometry, nIndex); // exception
// decompress
var nX = nDiffX + nLastDiffX;
var nY = nDiffY + nLastDiffY;
var dX = parseFloat(nX / dMultBy);
var dY = parseFloat(nY / dMultBy);
// add result item
var point = new esri.geometry.Point(dX, dY, new esri.SpatialReference({ wkid: 3414 }));
//point.x = dX;
//point.y = dY;
points.push(point); // memory exception
// prepare for next calculation
nLastDiffX = nX;
nLastDiffY = nY;
}
return points;
}
function ExtractInt(src, nStartPos) {
alert("stage 3");
// Read one integer from compressed geometry string by using passed position
// Returns extracted integer, and re-writes nStartPos for the next integer
var bStop = false;
var result = "";
var nCurrentPos = nStartPos;
while (!bStop) {
var cCurrent = src[nCurrentPos];
if (cCurrent == '+' || cCurrent == '-' || cCurrent == '|') {
if (nCurrentPos != nStartPos) {
bStop = true;
continue;
}
}
result += cCurrent;
nCurrentPos++;
if (nCurrentPos == src.length) // check overflow
bStop = true;
}
var nResult = 0;
if (result.length != 0) {
nResult = FromStringRadix32(result.toString());
nStartPos = nCurrentPos;
}
return nResult;
}
function FromStringRadix32(s) {
alert("stage 4");
// Sample input and output: +1lmo -> 55000
var result = 0;
for (i = 1; i < s.length; i++) {
var cur = s;
//Assert.IsTrue((cur >= '0' && cur <= '9') || (cur >= 'a' && cur <= 'v'), "Cannot parse CompressedGeometry");
if (cur >= '0' && cur <= '9')
result = (result << 5) + parseInt(cur) - parseInt('0');
else if (cur >= 'a' && cur <= 'v')
result = (result << 5) + parseInt(cur) - parseInt('a') + 10;
//else Assert.Fail("Cannot parse CompressedGeometry");
}
if (s[0] == '-')
result = -result;
return result;
}
Primitive arguments in JavaScript are passed by value so in your code nIndex will always be zero, hence your infinite loop.
Are you getting this from a RouteTask? You know that this is decompressed for you in the RouteResult etc?
Do you want to share your javascript port? Maybe on gist.github.com?