2014-09-05 3 views
2

XML 파일에서 GPS 좌표를 가져 와서 Google Static Maps API로 전송하는 도구를 작성 중입니다. Google에서 폴리 라인을 얻으려고합니다.C# Google Static Maps 인코딩 된 경로 계산

내가 발견 한 documentation 지금까지 나는 폴리 라인의 인코딩 값을 double 값을 변환하려면이 서면으로 작성했습니다 :

private String convertDouble(Double _input) 
{ 
    String result = String.Empty; 
    //value * 1e5 
    Int32 multiplication = Convert.ToInt32(Math.Floor(_input * 1e5)); 

    //value to binary string 
    String binaryString = Convert.ToString(multiplication, 2); 

    //binary to hex 
    binaryString = BinaryStringToHexString(binaryString); 

    //value to hex + 1 
    Int32 hexConvert = Convert.ToInt32(binaryString, 16) + Convert.ToInt32("01", 16); 

    //value to binary string 
    binaryString = Convert.ToString(hexConvert, 2); 

    //binary string zu int[] for further calculations 
    Int32[] bitValues = new Int32[binaryString.Length]; 
    for (Int32 i = 0; i < bitValues.Length; i++) 
    { 
     if (binaryString[i] == '0') 
     { 
     bitValues[i] = 0; 
     } 
     else 
     { 
      bitValues[i] = 1; 
     } 
    } 

    //shift binary to left 
    Int32[] bitValues_2 = new Int32[bitValues.Length]; 
    for (Int32 i = 0; i < bitValues.Length - 1; i++) 
    { 
     bitValues_2[i] = bitValues[i + 1]; 
    } 
    bitValues_2[bitValues_2.Length - 1] = 0; 

    //if input value is negative invert binary 
    if (_input < 0) 
    { 
     for (Int32 i = 0; i < bitValues.Length; i++) 
     { 
      if (bitValues_2[i] == 0) 
      { 
      bitValues_2[i] = 1; 
       } 
      else 
      { 
       bitValues_2[i] = 0; 
      } 
     } 
    } 

    //make blocks of 5 
    Int32 lengthDifference = bitValues_2.Length % 5; 

    Int32[] bitValues_3 = new Int32[bitValues_2.Length - lengthDifference]; 

    for (Int32 i = bitValues_2.Length - 1; i > (bitValues_2.Length - bitValues_3.Length); i--) 
    { 
     bitValues_3[i - (bitValues_2.Length - bitValues_3.Length)] = bitValues_2[i]; 
    } 

    //twist blocks 
    Int32[] bitValues_4 = new Int32[bitValues_3.Length]; 

    Int32 numberOfBlocks = bitValues_3.Length/5; 
    Int32 counter = 0; 

    String[] stringValues = new String[numberOfBlocks]; 

    for (Int32 i = numberOfBlocks - 1; i >= 0; i--) 
    { 
     for (Int32 j = i * 5; j < (i * 5) + 5; j++) 
     { 
      bitValues_4[counter] = bitValues_3[j]; 
      counter++; 
     } 
    } 

    counter = 0; 

    //write int[] into strings for final conversion 
    for (Int32 i = 0; i < bitValues_4.Length; i++) 
    { 
     if (i > 0 && i % 5 == 0) 
     { 
      counter++; 
     } 

     stringValues[counter] += bitValues_4[i].ToString(); 
    } 

    // blocks^0x20 (32) and convert to char 
    Int32 value = 0; 
    Int32[] intValues = new Int32[stringValues.Length]; 
    Char[] charValues = new Char[stringValues.Length]; 
    for (Int32 i = 0; i < stringValues.Length; i++) 
    { 
     value = Convert.ToInt32(stringValues[i], 2); 
     if (i < stringValues.Length - 1) 
     { 
      intValues[i] = value^32; 
     } 
     else 
     { 
      intValues[i] = value; 
     } 
      intValues[i] = intValues[i] + 63; 
      charValues[i] = (Char)intValues[i]; 
      result += charValues[i]; 
     } 

    return result; 
} 

나는 documentation

의 값을 사용하는 경우

-179.4

나는 결과를 얻을

`~ 이아 (Oia) @ 괜찮지 만, 나는 내 값 중 하나를 사용하는 경우 예

:

LAT : 8.7587061 LONG : 48.6331662

LAT : 8.8905152 LONG : 48.6226701를

잘못된 값이 표시됩니다. 최종 폴리선은 독일 남부에 있어야합니다. 그러나 폴리 라인은 비용 계산에 가까운 곳에 있습니다. 어쩌면 인코딩 된 좌표를 제공하는 완성 된 클래스가 있고 아직 찾지 못했을 것입니다.

예, 폴리 라인을 인코딩해야합니다. XML에 여러 좌표가 있기 때문에 (GPS 추적기는 몇 시간 동안 실행되고 매 10 초마다 위치를 캡처합니다.) 폴리 라인을 인코딩해야합니다.

+0

'double'대신 'decimal'을 사용해보십시오. – Zer0

+0

@ Zer0 그게 뭐야? –

+0

@ L.B 기본 2 부동 소수점 문제를 제거하십시오. – Zer0

답변

1

글쎄, 검색 및 테스트의 또 다른 밤 후에 나는 해결책을 찾았습니다. Brian Pedersen 그의 블로그에 해결책이 있습니다. 그리고 그것은 꼭해야만하는 것처럼 작동합니다. 여기에 코드를 복사하여 붙여 넣기를 원하지 않지만 링크에 포함되어 있습니다.