2011-12-10 82 views
0

我正在用eye4software製作一個簡單的座標轉換器。以下鏈接爲轉換器提供了所需的Visual Basic 6代碼。座標轉換

http://www.eye4software.com/products/gpstoolkit/source/vb/datumtransformation/

我按照根據鏈接所提供的資料說的過程。

Private Sub Form1_Load() 

    Private objProjection As GpsProjection 

    Private objDatumSrc As GpsDatumParameters 

    Private objDatumDst As GpsDatumParameters 

    Set objProjection = CreateObject("Eye4Software.GpsProjection") 

    Set objDatumSrc = CreateObject("Eye4Software.GpsDatumParameters") 

    Set objDatumDst = CreateObject("Eye4Software.GpsDatumParameters") 

End Sub 

Option Explicit 


Private objProjection As GpsProjection 

Private objDatumSrc As GpsDatumParameters 

Private objDatumDst As GpsDatumParameters 


Private Sub CommandTranslate_Click() 

    ' Set Source Datum (WGS84) 
    ' The ID for WGS84 is 4326, see 'http://www.eye4software.com/resources/datums' for a full list of supported datums 
    ' To convert from another datum, just change the code below (EPSG code) 
    objDatumSrc.LoadFromId (4326) 

    ' Set Destination Datum (NAD27) 
    ' The ID for NAD27 is 4267, see 'http://www.eye4software.com/resources/datums' for a full list of supported datums 
    ' To convert to another datum, just change the code below (EPSG code) 
    objDatumDst.LoadFromId (4267) 

    ' Set Source coordinates 
    objProjection.Latitude = CDbl(Textlat1.Text) 
    objProjection.Longitude = CDbl(Textlon1.Text) 

    ' Perform the datum transformation 
    objProjection.TransformDatum objDatumSrc, objDatumDst 

    ' Display the result 
    Textlat2.Text = objProjection.Latitude 
    Textlon2.Text = objProjection.Longitude 
End Sub 

但我得到一個運行時錯誤此代碼(objDatumSrc.LoadFromId(4326))說所需的對象。由於我是初學者,我無法解決這個問題。請幫幫我。

+0

小問題,但[不要在程序參數中放置括號](http://hashvb.earlsoft.co.uk/Brackets_around_procedure_parameters)。這可能不是問題的原因,但它會在某些時候咬你(同樣的錯誤) – Deanna

回答

-1

嘗試之一:

Call objDatumSrc.LoadFromId(4326) 

objDatumSrc.LoadFromId 4326 

VB變得有點古怪做方法與參數調用。如果它不符合預期格式,則某些結果可能會有所不同。

+0

- 謝謝你的答案但仍然有相同的錯誤。 – user1047784

1

你有兩個objDatumSrc變量。

  • 其中一個是中的私有變量 - 您正在初始化該變量。
  • 另一個是模塊級別,你不是初始化那一個。

刪除Private變量聲明中

+0

謝謝,但錯誤保持不變。 – user1047784

0

對我來說,它看起來像你不理解的範圍,但真正的問題是一個非實例變量。您在表單加載事件中的objDatumSrc聲明將無法在表單的其餘部分中看到,因爲您在方法外聲明的變量未被實例化。

這個位置替換當前的代碼......

Option Explicit 

Private objProjection As New GpsProjection 
Private objDatumSrc As New GpsDatumParameters 
Private objDatumDst As New GpsDatumParameters 

Private Sub CommandTranslate_Click() 
    ' Set Source Datum (WGS84) 
    ' The ID for WGS84 is 4326, see 'http://www.eye4software.com/resources/datums' for a full list of supported datums 
    ' To convert from another datum, just change the code below (EPSG code) 
    objDatumSrc.LoadFromId (4326) 

    ' Set Destination Datum (NAD27) 
    ' The ID for NAD27 is 4267, see 'http://www.eye4software.com/resources/datums' for a full list of supported datums 
    ' To convert to another datum, just change the code below (EPSG code) 
    objDatumDst.LoadFromId (4267) 

    ' Set Source coordinates 
    objProjection.Latitude = CDbl(Textlat1.Text) 
    objProjection.Longitude = CDbl(Textlon1.Text) 

    ' Perform the datum transformation 
    objProjection.TransformDatum objDatumSrc, objDatumDst 

    ' Display the result 
    Textlat2.Text = objProjection.Latitude 
    Textlon2.Text = objProjection.Longitude 
End Sub 
+0

我也嘗試過這個.error也是一樣。謝謝。 – user1047784

+0

我已經更新我的答案後進一步審查,只是發佈你應該使用的代碼。 – UnhandledExcepSean

0

的代碼,這樣顯然不應該編譯,很明顯,你是不是顯示你真正的代碼。例如,你的錯誤處理是什麼?如果你已經完成了類似On Error Resume Next的操作,那麼如果以下行發生錯誤,則不會報告錯誤。

Set objProjection = CreateObject("Eye4Software.GpsProjection") 
Set objDatumSrc = CreateObject("Eye4Software.GpsDatumParameters") 
Set objDatumDst = CreateObject("Eye4Software.GpsDatumParameters") 

因爲它們將被設置爲Nothing,如果你試圖在objProjection,objDatumSrc和objDatumDst執行的方法和屬性,它們會引發錯誤「所需的對象」。

由於這可能不是您嘗試運行的代碼,您是否可以驗證所有程序ID,例如「Eye4Software.GpsProject」是否正確?事實上 - 你有沒有註冊這些組件?爲什麼你不能用稍微簡潔的符號實例化這些對象,例如

Set objProjection = New Eye4Software.GpsProjection 

+0

你會得到「對象或未設置塊」,而不是「對象需要」。 – Deanna