2013-12-21 32 views
2

更新:找到Google distancematrix并试图相应地修改我的代码。我在这里得到一个无效的参数错误:用C#中的Google maps API和SSIS包获取驾车距离

return new GeoLocation(dstnc, uri.ToString()); 
      } 
      catch 
      { 
       return new GeoLocation(0.0, "https://"); 
      } 

基本上,我需要从两个纬度/多头知道得到的行驶距离。我正在使用SSIS包,我在网上发现了一个非常棒的教程,它非常接近我需要的结果。

教程:http://www.sqlmusings.com/2011/03/25/geocode-locations-using-google-maps-v3-api-and-ssis/

他们的所作所为是通过已知的街道地址,以谷歌,并从返回的XML读经/纬。

我需要做的不同是传递两个已知纬度/经度并读取返回的距离。

例子:https://maps.googleapis.com/maps/api/distancematrix/xml?origins=32.1576780,%20-82.9070920&destinations=27.6536997,%20-81.5158944&mode=driving&units=imperial&sensor=false

他们使用C#对此我不够好,与知道如何做出修改。我在花刺反正这里是我想出了:

using System; 
using System.Collections.Generic; 
using System.Text; 

//added these 
using System.Data; 
using System.Net; 
using System.Web; 
using System.Xml; 

// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
// PARTICULAR PURPOSE. 

namespace sqlmusings 
{ 

    public interface IGeoLocation 
    { 
     string geocodeurl { get; set; } 
     float distance { get; set; } 
    } 

    public struct GeoLocation : IGeoLocation 
    { 
     private string _geocodeurl; 
     private Double _distance; 

     public GeoLocation(string geocodeurl, Double distance) 
     { 
      _geocodeurl = geocodeurl; 
      _distance = distance; 
     } 

     public string geocodeurl 
     { 
      get { return _geocodeurl; } 
      set { _geocodeurl = value; } 
     } 

     public Double distance 
     { 
      get { return _distance; } 
      set { _distance = value; } 
     } 
    } 

    public class GeoCode 
    { 
     const string _googleUri = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins="; 
     //sample 
     //https://maps.googleapis.com/maps/api/distancematrix/xml?origins=32.1576780,-82.9070920&destinations=27.6536997,-81.5158944&mode=driving&units=imperial&sensor=false 

     private static Uri GetGeoCodeURI(string origins) 
     { 
      origins = HttpUtility.UrlEncode(origins); 
      string uri = String.Format("{0}{1}&sensor=false", _googleUri, origins); 

      return new Uri(uri); 

     public static GeoLocation GetCoordinates(string origins) 
     { 
      WebClient wc = new WebClient(); 
      Uri uri = GetGeoCodeURI(origins); 

      try 
      { 
       string geoCodeInfo = wc.DownloadString(uri); 
       XmlDocument xmlDoc = new XmlDocument(); 
       xmlDoc.LoadXml(geoCodeInfo); 

       string status = xmlDoc.DocumentElement.SelectSingleNode("status").InnerText; 
       double dstnc = 0.0; 
       XmlNodeList nodeCol = xmlDoc.DocumentElement.SelectNodes("result"); 
       foreach (XmlNode node in nodeCol) 
       { 

        dstnc = Convert.ToDouble(node.SelectSingleNode("distance/text").InnerText, System.Globalization.CultureInfo.InvariantCulture); 
       } 
       return new GeoLocation(dstnc, uri.ToString()); 
      } 
      catch 
      { 
       return new GeoLocation(0.0, "https://"); 
      } 
     } 

    } 

} 

我增加了距离和_distance的代码,但林不知道顶部在哪里进行更改,以添加一个字段为我第二个拉特/长。

以防万一,这里是Main.cs(我没有修改它在所有):

/* Microsoft SQL Server Integration Services Script Component 
* Write scripts using Microsoft Visual C# 2008. 
* ScriptMain is the entry point class of the script.*/ 

using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Pipeline.Wrapper; 
using Microsoft.SqlServer.Dts.Runtime.Wrapper; 
using SC_65a998228f6546ed965116b9f8b76953.csproj; 

// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
// PARTICULAR PURPOSE. 

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] 
public class ScriptMain : UserComponent 
{ 

    public override void PreExecute() 
    { 
     base.PreExecute(); 
     /* 
      Add your code here for preprocessing or remove if not needed 
     */ 
    } 

    public override void PostExecute() 
    { 
     base.PostExecute(); 
     /* 
      Add your code here for postprocessing or remove if not needed 
      You can set read/write variables here, for example: 
      Variables.MyIntVar = 100 
     */ 
    } 

    public override void Input0_ProcessInputRow(Input0Buffer Row) 
    { 
     /* 
      Add your code here 
     */ 
     sqlmusings.GeoLocation geolocation = sqlmusings.GeoCode.GetCoordinates(Row.AddressLine1 + "," + Row.City + "," + Row.State + "," + Row.Country); 
     Row.Latitude = Convert.ToDecimal(geolocation.latitude); 
     Row.Longitude = Convert.ToDecimal(geolocation.longitude); 
     Row.GeoCodeURL = geolocation.geocodeurl; 
    } 

} 

在正确的方向就在推的是所有的即时通讯要求。

回答

0

String.Format函数的诀窍是大括号中的数字(例如{0}{1})是由您按照相应顺序提供的参数替换的位。

你通常想要做的是这样的:

double oLat = 32.1576780, oLng = -82.9070920; 
double dLat = 27.6536997, dLng = -81.5158944; 

String url = String.Format("https://maps.googleapis.com/maps/api/distancematrix/xml?origins={0},{1}&destinations={2},{3}&mode=driving&sensor=false", oLat, oLng, dLat, dLng); 

有一点要记住的是,地图API有需要某些东西,以便Terms & Conditions能够使用API​​免费。确保你阅读了他们,因为你的用例可能需要一个商业许可证。

相关问题