【问题标题】:Google Maps v3 geocoding server-sideGoogle Maps v3 地理编码服务器端
【发布时间】:2011-12-18 00:44:52
【问题描述】:

我正在使用 ASP.NET MVC 3 和 Google Maps v3。我想在一个动作中进行地理编码。那就是将一个有效的地址传递给谷歌并取回纬度和经度。我见过的所有关于地理编码的在线示例都处理了客户端地理编码。您将如何在使用 C# 的操作中执行此操作?

【问题讨论】:

    标签: c# asp.net-mvc-3 google-maps-api-3


    【解决方案1】:

    我不确定我是否理解正确,但我就是这样做的(如果你有兴趣)

    void GoogleGeoCode(string address)
    {
        string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
    
        dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();
        foreach (var result in googleResults.results)
        {
            Console.WriteLine("[" + result.geometry.location.lat + "," + result.geometry.location.lng + "] " + result.formatted_address);
        }
    }
    

    使用扩展方法here & Json.Net

    【讨论】:

    • 很好 - 感谢分享。它工作正常。您的示例使用的是 Google Maps v3 对吗?
    • 不,它不是 javascript api。我使用了来自code.google.com/apis/maps/documentation/geocoding (JSON Output Formats) 的文档
    • 如果我打算将 lat 和 lng 存储到数据库表中,这仍然有效吗?
    【解决方案2】:

    L.B 的解决方案对我有用。但是我遇到了一些运行时绑定问题,必须先转换结果才能使用它们

     public static Dictionary<string, decimal> GoogleGeoCode(string address)
        {
            var latLong = new Dictionary<string, decimal>();
    
            const string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
    
            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();
    
            foreach (var result in googleResults.results)
            {
                //Have to do a specific cast or we'll get a C# runtime binding exception
                var lat = (decimal)result.geometry.location.lat;
                var lng = (decimal) result.geometry.location.lng;
    
                latLong.Add("Lat", lat);
                latLong.Add("Lng", lng);
            }
    
            return latLong;
        }
    

    【讨论】:

      【解决方案3】:

      由于新的 Google API 要求使用有效的 API 密钥,我遇到了问题。为了让事情正常进行,我修改了代码以将密钥附加到地址并将 url 更改为 https:

      public Dictionary<string, decimal> GoogleGeoCode(string address)
      {
          var latLong = new Dictionary<string, decimal>();
          string addressReqeust = address + "&key=your api key here";
          const string url = "https://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
      
          dynamic googleResults = new Uri(url + addressReqeust).GetDynamicJsonObject();
      
          foreach (var result in googleResults.results)
          {
              //Have to do a specific cast or we'll get a C# runtime binding exception
              var lat = (decimal)result.geometry.location.lat;
              var lng = (decimal)result.geometry.location.lng;
              try
              {
                  latLong.Add("Lat", lat);
                  latLong.Add("Lng", lng);
              }
              catch (Exception ex)
              {
      
              }
          }
      
          return latLong;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-04-05
        • 1970-01-01
        • 2013-12-18
        • 2011-09-27
        • 2013-10-05
        • 1970-01-01
        • 1970-01-01
        • 2011-11-23
        • 2012-12-11
        相关资源
        最近更新 更多