【问题标题】:How to retrieve the latitude and longitude of the inputed address using google geo coder?如何使用谷歌地理编码器检索输入地址的纬度和经度?
【发布时间】:2013-05-29 18:25:49
【问题描述】:

当用户输入他的地址为街道、城市、州和邮政编码时,我想在 2 个文本框中显示纬度和经度值。我正在使用谷歌地理编码器。

我在点击按钮时使用了以下代码:

   protected void BtnShow_Click(object sender, EventArgs e)
    {
        GetLatLongFromAddress(TxtStreet.Text, TxtCity.Text, TxtZipcode.Text,   TxtState.Text);
    }

    private void GetLatLongFromAddress(string street, string city, string zipcode, string state)
    {

        string geocoderUri = string.Format(@"http://maps.googleapis.com/maps/api/geocode/xml?address={0},{1},{2},{3}&sensor=false", street, city, zipcode, state);


        XmlDocument geocoderXmlDoc = new XmlDocument();
        geocoderXmlDoc.Load(geocoderUri);

        XmlNamespaceManager nsMgr = new XmlNamespaceManager(geocoderXmlDoc.NameTable);
        nsMgr.AddNamespace("geo", @"http://www.w3.org/2003/01/geo/wgs84_pos#"); 


        string sLong = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"geo:long", nsMgr).InnerText;
        string sLat = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"//geo:lat", nsMgr).InnerText;

        TxtLatitude.Text = sLat;
        TxtLongitude.Text = sLong;
    }

但是它获取 sLong 变量的值为 null 并显示错误

   "Object reference not set to an instance of an object."

我该怎么做?

等待答复...

谢谢..

【问题讨论】:

  • @AakashM - 感谢您的回复。是的,我已经读过了。但我没有任何想法。我该如何使用它以及我应该包含哪些头文件?

标签: c# asp.net visual-studio-2008 google-maps


【解决方案1】:

为此,我使用了出色的 GoogleMap 控件,请在此处查看详细信息:

http://googlemap.codeplex.com/wikipage?title=Google%20Geocoder&referringTitle=Documentation

在这个链接上你有服务器端使用的例子,你也可以从客户端使用它,这个例子是使用 GeoCoder API 根据地名在地图上定位地点。

  function DoMapSearch(placeName) {
    var geocoder = new GClientGeocoder();
    geocoder.getLatLng(placeName, function (point) {
      if (point != null) {
        GoogleMapCnt.loadAddress(addr);
      }
    });

    return false;
  }

【讨论】:

  • 感谢您的回复。我已按照您的建议阅读了 GoogleMap。这是古德。但我想知道如何添加对 Artem.GoogleGeocoding 的引用?
  • googlemap.codeplex.com/releases/view/52051 下载二进制文件并将其作为参考添加到您的项目中
  • 再次感谢您的帮助。我必须下载这两个文件,即发布和构建。 ?我必须在我的项目中添加这两个文件?
  • Yuppi..非常感谢您的帮助。 : ) 我用了你的方法,我得到了解决方案。但我想知道我们如何检查输入的地址是否错误并进行验证以输入正确的地址。 ?
  • 抱歉,请尝试使用 Count 或查看帮助
【解决方案2】:

我使用了你的代码,错误在 xPath 上,我把它改成了这个

string sLong = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"//geometry/location/lat", nsMgr).InnerText;

string sLat = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"//geometry/location/lng", nsMgr).InnerText;

现在一切正常。

【讨论】:

    猜你喜欢
    • 2011-11-21
    • 1970-01-01
    • 2011-09-05
    • 2012-01-17
    • 2014-01-16
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多