How to store geocoded address information into the database

后端 未结 1 601
既然无缘
既然无缘 2021-01-07 07:17

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          


        
1条回答
  •  有刺的猬
    2021-01-07 08:19

    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
        {
            /// 
            ///
            /// 
            /// 
            /// 
            public static GeoResponse GetGeoCodedResults(string address)
            {
                string url = string.Format(
                        "http://maps.google.com/maps/api/geocode/json?address={0}®ion=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()
            { }
        }
    }
    

    0 讨论(0)
提交回复
热议问题