Home > All Tags > XML

[原创]VB.net 通过Google Map API计算路程距离

评分 0.00, 满分 5星 0 票 No comments

利用Google Directions API (Google Map) http://code.google.com/intl/zh-CN/apis/maps/documentation/directions/
和VB.net 输入目的地(支持中文地址、经纬度等)计算出距离。
如:输入 北京|上海|天津
运行程序 会计算从A地出发依次到北京、上海、天津 三地的总距离,可选择是否包含返回的距离(天津到A地的距离)。
如果需要线路优化 可以参照API 介绍中的 optimize:true。开启后会自动安排路线。

已经在VB.net 2003 通过测试。

做的着急有些地方直接复制的,代码没有优化,有些重复,但不影响最终使用。

'需要以下命名空间
Imports System.web
Imports System.Xml
Imports System.IO

'需要声明的变量
Dim gstrPatch As String = "C:\XXX\map.xml"

    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
     '判断输入的目的地是否为空。
        If txtROAD_ROUTE.Text = "" Then Exit Sub

        Try

          If MsgBox("路程距离数据会受到网络、关键字的影响,可能与实际距离相差很大,仅供参考。", MsgBoxStyle.YesNo, "系统提示") = MsgBoxResult.No Then Exit Sub
  
          Dim arrROAD As String()
          Dim strWaypoints As String  
          Dim i As Integer = 0
  
          arrROAD = txtROAD_ROUTE.Text.Split("|")
          Dim arrCalc(arrROAD.Length - 1) As String
      '对搜索的目的地编码
          For t As Integer = 0 To arrROAD.Length - 1
              arrROAD(t) = HttpUtility.UrlEncode(arrROAD(t), System.Text.Encoding.UTF8)  
          Next
         
      '编码重组
          For t As Integer = 0 To arrROAD.Length - 2
              If strWaypoints = "" Then
                  strWaypoints = arrROAD(t)
              Else
                  strWaypoints = strWaypoints & "|" & arrROAD(t)
              End If
          Next
         
      '39.049435,121.782503 为出发地 经纬度,可以替换成路名、地名等,注意中文请注意转码。
          Dim strURL As String
          strURL = "<a href="http://maps.google.com/maps/api/directions/xml?origin=39.049435,121.782503&destination">http://maps.google.com/maps/api/directions/xml?origin=39.049435,121.782503&destination</a>=" & arrROAD(arrROAD.Length - 1) & _
                  "&waypoints=" & strWaypoints & _
                  "&sensor=false"
  
          Dim myWebClient As New System.Net.WebClient
          'myWebClient.DownloadFile("<a href="http://maps.google.com/maps/api/directions/xml?origin=39.049435,121.782503&destination">http://maps.google.com/maps/api/directions/xml?origin=39.049435,121.782503&destination</a>=" & strdifang & "&waypoints=&sensor=false", "C:\msdn.xml")
          myWebClient.DownloadFile(strURL, gstrPatch)
  
          Dim xmlDoc As New XmlDocument
          Dim XmlFileName As String = gstrPatch
  
          If File.Exists(XmlFileName) Then
              xmlDoc.Load(XmlFileName)
              Dim nodeList As XmlNodeList = xmlDoc.SelectSingleNode("DirectionsResponse").ChildNodes
              Dim xn1 As XmlNode
              Dim xn2 As XmlNode
              Dim xn3 As XmlNode
                  For Each xn1 In nodeList
                      For Each xn2 In xn1
                          For Each xn3 In xn2
                              If xn3.Name = "distance" Then
                                  arrCalc(i) = xn3.ChildNodes.Item(1).InnerText
                                  i = i + 1
                              End If
                          Next
                      Next
                  Next
          End If
  
          Dim douKM As Double
          Dim strLast As String
  
          If MsgBox("是否计算往返路程?", MsgBoxStyle.YesNo, "系统提示") = MsgBoxResult.Yes Then
              strURL = "<a href="http://maps.google.com/maps/api/directions/xml?origin">http://maps.google.com/maps/api/directions/xml?origin</a>=" & arrROAD(arrROAD.Length - 1) & "&destination=39.049435,121.782503" & _
                       "&sensor=false"
              myWebClient.DownloadFile(strURL, gstrPatch)
              '*计算
  
              Dim xmlDoc2 As New XmlDocument
              Dim XmlFileName2 As String = gstrPatch
  
              If File.Exists(XmlFileName2) Then
                  xmlDoc2.Load(XmlFileName2)
                  Dim nodeList As XmlNodeList = xmlDoc2.SelectSingleNode("DirectionsResponse").ChildNodes
                  Dim xn12 As XmlNode
                  Dim xn22 As XmlNode
                  Dim xn32 As XmlNode
                      For Each xn12 In nodeList
                          For Each xn22 In xn12
                              For Each xn32 In xn22
                                  If xn32.Name = "distance" Then
                                      strLast = xn32.ChildNodes.Item(1).InnerText
                                  End If
                              Next
                          Next
                      Next
              End If
  
              For r As Integer = 0 To arrCalc.Length - 1
                  douKM = douKM + CType(Replace(arrCalc(r), " km", ""), Double)
              Next
  
              douKM = douKM + CType(Replace(strLast, " km", ""), Double)
          Else
              For r As Integer = 0 To arrCalc.Length - 1
                  douKM = douKM + CType(Replace(arrCalc(r), " km", ""), Double)
              Next
          End If
      '显示距离 KM 单位
          txtDISTANCE.Text = douKM

        Catch ex As Exception
            MsgBox("获取距离数据失败。")
        End Try

    End Sub

[原创]VB.net 使用XML 对 FpSpread 列宽记忆

评分 0.00, 满分 5星 0 票 1 comment
'需要引入的 命名空间
Imports System.Xml
Imports System.IO

#Region "XML 读取 写入 生成"
Private Sub createXML(ByVal xmlFileName As String)
	Try
	
		Dim writer As New Xml.XmlTextWriter(xmlFileName, System.Text.Encoding.GetEncoding("utf-8"))
		'使用自动缩进便于阅读
		writer.Formatting = Xml.Formatting.Indented
		writer.WriteRaw("<!--l version=""1.0"" encoding=""utf-8""-->")
		writer.WriteRaw("<!--   Xing   -->")
		'书写根元素()
		writer.WriteStartElement("xx")
		'关闭根元素
		writer.WriteFullEndElement()
		'将XML写入文件并关闭writer
		writer.Close()
	
	Catch ex As System.Exception
		MsgBox(ex.Message & vbCrLf & ex.StackTrace)
	End Try
End Sub

Private Sub FpSpread_Column_Size_load(ByVal FpSpread As FarPoint.Win.Spread.FpSpread)
	Dim xmlDoc As New XmlDocument
	Dim XmlFileName As String = "C:\XX\INI\FpSpread_Config.xml" '修改为实际XML路径
	Dim intCol As Integer
	
	Try
	
		If File.Exists(XmlFileName) Then
			xmlDoc.Load(XmlFileName)		
			Dim nodeList As XmlNodeList = xmlDoc.SelectSingleNode("xx").ChildNodes '获取xx节点的所有子节点
			Dim xn As XmlNode
			
			For Each xn In nodeList
				Dim xe As XmlElement = CType(xn, XmlElement)				
				If xe.GetAttribute("form") = Me.Name And xe.GetAttribute("Name") = FpSpread.Name Then				
					Dim nls As XmlNodeList = xe.ChildNodes '继续获取xe子节点的所有子节点
					Dim xn1 As XmlNode
					
					For Each xn1 In nls '遍历
						Dim xe2 As XmlElement = CType(xn1, XmlElement) '转换类型
						
						intCol = xe2.Name.Substring(1, xe2.Name.Length - 1)
						FpSpread.ActiveSheet.Columns(Val(intCol)).Width = Integer.Parse(xe2.InnerText)					
					Next xn1
				
				End If
			
			Next
		End If
		
	Catch ex As System.Exception
		File.Delete(XmlFileName)
	End Try
End Sub

Private Sub FpSpread_Column_Size_save(ByVal FpSpread As FarPoint.Win.Spread.FpSpread)
	Dim xmlDoc As New XmlDocument
	Dim XmlFileName As String = "C:\xx\INI\FpSpread_Config.xml" '修改为实际XML路径
	Dim intRecord As Integer
	
	Try
	If File.Exists(XmlFileName) = False Then
		createXML(XmlFileName)
	End If
	
	xmlDoc.Load(XmlFileName)	
	Dim nodeList As XmlNodeList = xmlDoc.SelectSingleNode("xx").ChildNodes '获取xx节点的所有子节点
	Dim xn As XmlNode
	
		For Each xn In nodeList '遍历所有子节点
			Dim xe As XmlElement = CType(xn, XmlElement) '将子节点类型转换为XmlElement类型
			If xe.GetAttribute("form") = Me.Name And xe.GetAttribute("Name") = FpSpread.Name Then '如果form属性值为“当前窗口”Name为 当前spread 那么就 清空下面的数据
			intRecord += 1
			xe.RemoveAll() '*清空数据
			
				If intRecord < 2 Then
				xe.SetAttribute("form", Me.Name)
				xe.SetAttribute("Name", FpSpread.Name)
				
				Dim child_node As XmlElement
					For i As Integer = 0 To FpSpread.ActiveSheet.Columns.Count - 1
						child_node = xmlDoc.CreateElement("C" & i)
						child_node.InnerText = FpSpread.ActiveSheet.Columns(i).Width
						xe.AppendChild(child_node)
					
					Next
				End If
			End If
		Next xn
	
	If intRecord < 1 Then
		Dim root As XmlNode = xmlDoc.SelectSingleNode("xx")
		Dim xe1 As XmlElement = xmlDoc.CreateElement("spread")
		xe1.SetAttribute("form", Me.Name)
		xe1.SetAttribute("Name", FpSpread.Name)
		
		Dim child_node As XmlElement
			For i As Integer = 0 To FpSpread.ActiveSheet.Columns.Count - 1
			child_node = xmlDoc.CreateElement("C" & i)
			child_node.InnerText = FpSpread.ActiveSheet.Columns(i).Width
			xe1.AppendChild(child_node)
	
		Next
	
		root.AppendChild(xe1) '添加到节点中
	
	End If
	
	xmlDoc.Save(XmlFileName) '保存。
	Catch ex As System.Exception
	
	End Try
	
End Sub
#End Region

#Region "Form Load"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	'spread load
	FpSpread_Column_Size_load(Me.spListMain)
End Sub
#End Region

#Region "form 关闭"
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
	FpSpread_Column_Size_save(Me.spListMain)
End Sub
#End Region

生成的XML文件内如如下

<?xml version="1.0" encoding="utf-8"?>
<!--   Xing   -->
<xx>
  <spread form="FORM1" Name="spListMain">
    <C0>60</C0>
    <C1>90</C1>
    <C2>106</C2>
    <C3>60</C3>
    <C4>80</C4>
    <C5>60</C5>
    <C6>178</C6>
    <C7>60</C7>
    <C8>119</C8>
    <C9>82</C9>
    <C10>145</C10>
    <C11>88</C11>
    <C12>123</C12>
    <C13>60</C13>
  </spread>
  <spread form="FORM1" Name="spListList">
    <C0>17</C0>
    <C1>65</C1>
    <C2>65</C2>
    <C3>31</C3>
    <C4>60</C4>
    <C5>65</C5>
    <C6>118</C6>
    <C7>69</C7>
    <C8>62</C8>
    <C9>71</C9>
    <C10>34</C10>
    <C11>52</C11>
    <C12>58</C12>
    <C13>58</C13>
    <C14>59</C14>
    <C15>106</C15>
    <C16>34</C16>
    <C17>34</C17>
  </spread>
  <spread form="FORM1" Name="spList1">
    <C0>60</C0>
    <C1>60</C1>
    <C2>60</C2>
    <C3>32</C3>
    <C4>60</C4>
    <C5>71</C5>
    <C6>60</C6>
    <C7>60</C7>
    <C8>60</C8>
    <C9>60</C9>
    <C10>60</C10>
    <C11>72</C11>
    <C12>68</C12>
    <C13>70</C13>
    <C14>60</C14>
    <C15>60</C15>
    <C16>60</C16>
    <C17>60</C17>
    <C18>60</C18>
    <C19>60</C19>
    <C20>60</C20>
    <C21>60</C21>
    <C22>65</C22>
  </spread>
</xx>