协调转换

时间:2011-12-10 05:20:17

标签: vb6

我在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)) 表示运行时错误。由于我是初学者,我无法解决这个问题。请帮帮我。

4 个答案:

答案 0 :(得分:1)

您有两个objDatumSrc个变量。

  • 一个是Form_Load内的私有变量 - 你正在初始化那个。
  • 另一个是模块级别的,你没有初始化那个。

删除Private

中的Form_Load变量声明

答案 1 :(得分:0)

对我来说,看起来你不了解范围,但真正的问题是一个非实例化的变量。表单load表单中的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

答案 2 :(得分: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

答案 3 :(得分:-1)

尝试:

Call objDatumSrc.LoadFromId(4326)

objDatumSrc.LoadFromId  4326
VB使用参数进行方法调用时会有点时髦。如果它不是预期的格式,某些结果可能会有所不同。