2012-02-25 83 views
0

你好我正在開發WinForms中運行日誌的過程。我想知道任何人是否有一些用於Google地圖集成的良好資源。最終目標是讓用戶雙擊「距離」列中的單元格,它將打開一個新表單。新表格上會有一個Google Map,用戶可以使用Google的距離測量工具來追蹤他們的路線。接受該路線將關閉窗口,並將追蹤路線的距離(英里)輸入到單元格中。谷歌地圖集成,獲得測量距離

我對Google地圖集成完全陌生,如果它實際上可以用C#應用程序。請指向正確的方向,我可以找到一些示例代碼來實現我的目標。

在此先感謝。

回答

2

您將需要使用google maps API。 謝天謝地,這很簡單,並使用JSON,這是很好的支持。

看一看: http://code.google.com/apis/maps/documentation/distancematrix/

不幸的是谷歌會選擇你進入兩點之間的最佳路線,這是沒有多大用處,如果你沒有運行最快的路線。

爲了解決這個問題,看看這裏: http://code.google.com/apis/maps/documentation/directions/

這允許您添加航點,因此也許更有用,但你可能會需要用戶創建這些航點自己。

最後要記住的是,如果您希望獲得大量請求 - 谷歌現在向開發者收取使用這些服務的費用。作爲非商業用戶,您每天限制2,500個請求,每個請求最多有8個航點。

4

使用這個類在你的項目中獲得兩兩地之間的距離 您可以從https://www.nuget.org/packages/newtonsoft.json/

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Newtonsoft.Json.Linq; 

namespace Management 
{ 
    class DeliveryMapInfo 
    { 
    string origin = @""; //Write the address of the origin here 
    public DeliveryMapInfo() { } 
    public int getDistanceTo(string destination) 
    { 
     System.Threading.Thread.Sleep(1000); 
     int distance = -1; 
     string url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false"; 
     string requesturl = url;string content = fileGetJSON(requesturl); 
     JObject _Jobj = JObject.Parse(content); 
     try 
     { 
      distance = (int)_Jobj.SelectToken("routes[0].legs[0].distance.value"); 
      return distance/1000;// Distance in KMS 
     } 
     catch 
     { 
      return distance/1000; 
     } 
    } 
    protected string fileGetJSON(string fileName) 
    { 
     string _sData = string.Empty; 
     string me = string.Empty; 
     try 
     { 
      if (fileName.ToLower().IndexOf("http:") > -1) 
      { 
       System.Net.WebClient wc = new System.Net.WebClient(); 
       byte[] response = wc.DownloadData(fileName); 
       _sData = System.Text.Encoding.ASCII.GetString(response); 

      } 
      else 
      { 
       System.IO.StreamReader sr = new System.IO.StreamReader(fileName); 
       _sData = sr.ReadToEnd(); 
       sr.Close(); 
      } 
     } 
     catch { _sData = "unable to connect to server "; } 
     return _sData; 
    } 
} 

得到Newtonsoft.Json.Linq}

很多自己的想法取決於你想要什麼API使用,即。距離矩陣,方向,地理編碼等。 注意:「/ directions /」 因爲它可以進行地理編碼,找到距離,查找持續時間等等,所以我找到了最適合我使用的api指令。等等。 但是,方向JSON文件大於其他JSON文件。 使用你的判斷。

string url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false"; 

distance = (int)_Jobj.SelectToken("routes[0].legs[0].distance.value"); 

爲如:

內路線JSON文件的結構是:

-Routes 

    - Bound 
     - NorthEast 
     - SouthWest 
    - Copyrights 
    - Legs 
     - Steps 
      - duration 
      - distance 
      - endlocation 
      - maneuver 
      - htmlinstructions 
      - polyline 
      - startlocation 
      - travelmode 
     - Distance 
      - text 
      - value 
     - Duration 
      - text 
      - value 
     - StartAddress 
     - EndAddress 
     - EndLocation 
      - lat 
      - lng 
     - StartLocation 
      - lat 
      - lng 
    - Overview Polyline 
    - Summary 
    - Warnings 
    - Waypoint Order 

- Status