如何将经过地理编码的地址信息存储到数据库中 [英] How to store geocoded address information into the database

查看:122
本文介绍了如何将经过地理编码的地址信息存储到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这会变成几百个请求,并为每个请求提供纬度和经度。我需要把纬度和经度存储到数据库中。



我不在乎它是否返回json或xml。



我找不到很多关于如何做到这一点的信息,我发现的大部分信息都是质疑你是否真的大声说出了我已经看过的内容。我是一般的编程新手,我对c#特别陌生,并且正在使用goggles api's

解决方案

使用这个代码在一个传递呼叫中心,也许它可以帮助你:

$ $ $ $ $ $ $ $> foreach(var row in data)
{

字符串strAddr = row.ADDRESS1 +,+ row.CITY; //在这里你必须写出你想要的地址

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); //这是一个API限制

//这只是一个更新你的sql行的例子,你可以使用你需要
的信息(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,但没有链接)

 使用System.Net; 
使用System.Runtime.Serialization;
使用System.Runtime.Serialization.Json;
使用System.Web;

public class GoogleMapsDll
{
public class GoogleMaps
{
///< summary>
///
///< / summary>
///< param name =address>< / param>
///<返回>< /返回>
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;


$ b $ [DataContract]
public class GeoResponse
{
[DataMember(Name =status)]
公共字符串状态{get;组; }

[DataMember(Name =results)]
public CResult []结果{get;组; }
$ b [DataContract]
public class CResult
{
[DataMember(Name =geometry)]
public CGeometry Geometry {get;组; }
$ b $ [DataContract]
public class CGeometry
{
[DataMember(Name =location)]
public CLocation Location {get;组; }

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

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

public GeoResponse()
{}
}
}
code>


I have a geocode request that loops through multiple addresses in my data base and returns latitude and longitude as well as other information.

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>
    }

Which turns into a few hundred requests and gives my latitudes and longitudes for each. I need to take the latitude and longitude and store it into the database.

I don't really care if it returns json, or xml.

I can't find a lot of information on how to do this, most of the information I find is questioning whether or not you actually are aloud to do it which I have already looked into. I'm new to programming in general and i'm as specially new to c# and working with goggles api's

解决方案

Im using this code in a delivery call center, maybe it can help you:

                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();

                    }

                 }

And the class to geo code your address (I took it from StackOverflow, but dont have the link)

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()
        { }
    }
}

这篇关于如何将经过地理编码的地址信息存储到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆