gpt4 book ai didi

c# - 如何将地理编码地址信息存储到数据库中

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:06 32 4
gpt4 key购买 nike

我有一个地理编码请求,它循环遍历我数据库中的多个地址并返回纬度和经度以及其他信息。

foreach(var row in data){
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/geocode/json?address=@HttpUtility.UrlEncode(row.ADDRESS1)+@HttpUtility.UrlEncode(row.CITY)+@row.ST+CA&key=???apikey???"></script>
}

这变成了几百个请求,并为每个请求提供了我的纬度和经度。我需要获取经纬度并将其存储到数据库中。

我真的不在乎它返回的是 json 还是 xml。

我找不到很多关于如何做到这一点的信息,我找到的大部分信息都在质疑你是否真的大声去做我已经调查过的事情。我是一般编程的新手,我对 c# 和使用 goggles api 也是特别新的

最佳答案

我在送货调用中心使用此代码,也许它可以帮助您:

                foreach(var row in data)
{

string strAddr = row.ADDRESS1 + "," + row.CITY; //here you've to write the exact address you want to geo

GoogleMapsDll.GeoResponse myCoordenadas = new GoogleMapsDll.GeoResponse();
myCoordenadas = GoogleMapsDll.GoogleMaps.GetGeoCodedResults(strAddr);

string strLat = myCoordenadas.Results[0].Geometry.Location.Lat.ToString();
string strLong = myCoordenadas.Results[0].Geometry.Location.Lng.ToString();

System.Threading.Thread.Sleep(2000); //THIS IS AN API LIMITs

//this is just an example to update your sql rows, you can use the info where you need
using(SqlConnection myConnection = new SqlConnection(yourConnectionString))
{
myConnection.Open();
string strQueryUpdate = "UPDATE yourAddressTable SET lat = '" + strLat + "' , lon = '" + strLong + "' "
+ "WHERE yourId = '" + row.YourId + "' ";

SqlCommand myCommandUpdate = new SqlCommand(strQueryUpdate, myConnectionUpdate);
myCommandUpdate.ExecuteNonQuery();

}

}

还有对地址进行地理编码的类(我从 StackOverflow 获取的,但没有链接)

using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Web;

public class GoogleMapsDll
{
public class GoogleMaps
{
/// <summary>
///
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static GeoResponse GetGeoCodedResults(string address)
{
string url = string.Format(
"http://maps.google.com/maps/api/geocode/json?address={0}&region=dk&sensor=false",
HttpUtility.UrlEncode(address)
);
var request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeoResponse));
var res = (GeoResponse)serializer.ReadObject(request.GetResponse().GetResponseStream());
return res;
}
}

[DataContract]
public class GeoResponse
{
[DataMember(Name = "status")]
public string Status { get; set; }

[DataMember(Name = "results")]
public CResult[] Results { get; set; }

[DataContract]
public class CResult
{
[DataMember(Name = "geometry")]
public CGeometry Geometry { get; set; }

[DataContract]
public class CGeometry
{
[DataMember(Name = "location")]
public CLocation Location { get; set; }

[DataContract]
public class CLocation
{
[DataMember(Name = "lat")]
public double Lat { get; set; }

[DataMember(Name = "lng")]
public double Lng { get; set; }
}
}
}

public GeoResponse()
{ }
}
}

关于c# - 如何将地理编码地址信息存储到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26142739/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com