2010-09-20 61 views
-3

我有一个CSV文件包含一千个数据(列是地址1地址2城市州邮政编码标记县国家)的一些代理。我想上传并在Google地图中显示此数据。如何在谷歌地图中显示数据

这里是一个参考:

http://code.google.com/apis/maps/documentation/mapsdata/developers_guide_protocol.html#uploading_csv

我想用这个 像Bing地图客户服务,其中用户可以上传自定义数据并显示在地图冰上传这个CSV在C#。

+0

问题是什么? – JohannesH 2010-09-20 06:03:53

+0

我该如何做到这一点在c# – Surajit 2010-09-20 06:06:25

+0

Surajit:请重写,所以有一个具体的问题。 – 2010-09-20 06:13:19

回答

1
POST http://maps.google.com/maps/feeds/maps/default/full 
GData-Version: 2.0 
Authorization: GoogleLogin auth="authorization_token" 
Content-Type: text/csv 
Slug: A new map 

name,latitude,longitude,description 
Hello,-77.066395,-11.968312,Greetings from Lima! 
There,145.34502,-38.51512,Out There Down Under 
How,-88.421001,44.970465,How is Wisconsin? 
Are,13.084501,63.399164,Sorry about that 
You,140.637898,42.842568,I love you Hokkaido 


    public static class ClientLogin 
    { 
     /// <summary> 
     /// Client login url where we'll post login data to. 
     /// </summary> 
     private static string clientLoginUrl = 
      @"https://www.google.com/accounts/ClientLogin"; 

     /// <summary> 
     /// Data to be sent with the post request. 
     /// </summary> 
     private static string postData = 
      @"service={0}&continue=http://www.google.com/&Email={1}&Passwd={2}&source={3}"; 

     /// <summary> 
     /// Get the Auth token you get after a successful login. 
     /// You'll need to reuse this token in the header of each new request you make. 
     /// </summary> 
     /// <param name="service"></param> 
     /// <param name="username"></param> 
     /// <param name="password"></param> 
     /// <param name="source"></param> 
     /// <returns></returns> 
     public static string GetAuthToken(
      string service, string username, string password, string source) 
     { 
      // Get the response that needs to be parsed. 
      string response = PostRequest(service, username, password, source); 

      // Get auth token. 
      string auth = ParseAuthToken(response); 
      return auth; 
     } 

     /// <summary> 
     /// Parse the Auth token from the response. 
     /// </summary> 
     /// <param name="response"></param> 
     /// <returns></returns> 
     private static string ParseAuthToken(string response) 
     {    
      // Get the auth token. 
      string auth = ""; 
      try 
      { 
       auth = new Regex(@"Auth=(?<auth>\S+)").Match(response).Result("${auth}"); 
      } 
      catch (Exception ex) 
      { 
       throw new AuthTokenException(ex.Message); 
      } 

      // Validate token. 
      if (string.IsNullOrEmpty(auth)) 
      { 
       throw new AuthTokenException("Missing or invalid 'Auth' token."); 
      } 

      // Use this token in the header of each new request. 
      return auth; 
     } 

     /// <summary> 
     /// Create a post request with all the login data. This will return something like: 
     /// 
     /// SID=AQAAAH1234 
     /// LSID=AQAAAH8AA5678 
     /// Auth=AQAAAIAAAAB910 
     /// 
     /// And we need the Auth token for each subsequent request. 
     /// </summary> 
     /// <param name="service"></param> 
     /// <param name="email"></param> 
     /// <param name="password"></param> 
     /// <param name="source"></param> 
     /// <returns></returns> 
     private static string PostRequest(
      string service, string email, string password, string source) 
     { 
      // Get the current post data and encode. 
      ASCIIEncoding ascii = new ASCIIEncoding(); 
      byte[] encodedPostData = ascii.GetBytes(
       String.Format(postData, service, email, password, source)); 

      // Prepare request. 
      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(clientLoginUrl); 
      request.Method = "POST"; 
      request.ContentType = "application/x-www-form-urlencoded"; 
      request.ContentLength = encodedPostData.Length; 

      // Write login info to the request. 
      using (Stream newStream = request.GetRequestStream()) 
       newStream.Write(encodedPostData, 0, encodedPostData.Length); 

      // Get the response that will contain the Auth token. 
      HttpWebResponse response = null; 
      try 
      { 
       response = (HttpWebResponse)request.GetResponse(); 
      } 
      catch (WebException ex) 
      { 
       HttpWebResponse faultResponse = ex.Response as HttpWebResponse; 
       if (faultResponse != null && faultResponse.StatusCode == HttpStatusCode.Forbidden) 
        throw new IncorrectUsernameOrPasswordException(
         faultResponse.StatusCode, faultResponse.StatusDescription); 
       else 
        throw; 
      } 

      // Check for login failed. 
      if (response.StatusCode != HttpStatusCode.OK) 
       throw new LoginFailedException(
        response.StatusCode, response.StatusDescription); 

      // Read. 
      using (StreamReader reader = new StreamReader(response.GetResponseStream())) 
       return reader.ReadToEnd(); 
     } 
    } 

string auth = ClientLogin.GetAuthToken("local", "username", "password", ""); 

UploadCSV(auth) 

public string UploadCSV(string auth) 
     { 
      string URI = "http://maps.google.com/maps/feeds/maps/default/full"; 

      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(URI); 
      //Add these, as we're doing a POST 

      req.KeepAlive = false; 
      req.Method = "POST"; 
      req.Headers.Add("GData-Version", "2.0"); 
      req.Headers.Add("Slug", "A new map"); 

      req.Headers.Add("Authorization", "GoogleLogin auth=" + auth); 
      FileStream fs = new FileStream("E:\\Surajit\\MapPoint\\1.csv", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
      req.ContentLength = fs.Length; 
      req.ContentType = "text/csv"; 
      Stream outputStream = req.GetRequestStream(); 

      WriteInputStreamToRequest(fs, outputStream); 

      HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
      outputStream.Close(); 
      fs.Close(); 
      if (resp == null) return null; 
      StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()); 
      return sr.ReadToEnd().Trim(); 
     } 




protected void WriteInputStreamToRequest(Stream input, Stream output) 
     { 
      BinaryWriter w = new BinaryWriter(output); 
      const int size = 4096; 
      byte[] bytes = new byte[4096]; 
      int numBytes; 

      while ((numBytes = input.Read(bytes, 0, size)) > 0) 
      { 
       w.Write(bytes, 0, numBytes); 
      } 
      w.Flush(); 
     } 
+0

嗯,GetResponse()让我503错误:“服务不可用”。怎么来的? – Norbert 2010-11-11 11:55:41

+0

你的问题解决了吗? – Surajit 2010-11-15 09:00:49

+0

不,难以置信! :( – Norbert 2010-11-19 08:07:40

2

要使用webforms上传csv文件,请参阅Microsoft's tutorial,但如果您不打算实际保存它,我会直接使用上传控件中的流来避免将其保存到磁盘。

解析csv可以使用任何csv parser

然后,您可以使用java脚本在网络上使用google maps api examples中的任意一个在地图上显示数据。

编辑:尽管您有更新,但我仍设法误解了您的问题。如果您打算直接从c#拨打其余服务,this articlethis可能会有所帮助。

EDIT2: 为了您的CSV数据发送到谷歌的API,你必须:

  1. 验证和获取 认证令牌(google documentation; c# implementation
  2. 使POST请求包含您的数据和身份验证 您在一个中获得的令牌(example of how to send data via HttpPost
+0

老板你做了一个很大的混乱....数据将显示在地图......不是1或2 ...其数千..所以我想通过CSV数据上传数据使用谷歌数据api – Surajit 2010-09-20 10:25:40

+0

请阅读http://code.google.com/apis/maps/documentation/mapsdata/developers_guide_protocol.html#uploading_csv – Surajit 2010-09-20 10:26:42

+0

请参阅我的编辑。您应该能够使用我提供的链接中提供的http请求来调用上传restful api来上传您的位置。 – PHeiberg 2010-09-20 10:32:04