jxycms
9/12/2017 - 4:33 AM

convert address to coordination

convert address to coordination

Dictionary<string, object> obj = new Dictionary<string, object>();
double[] numArray = new double[] { 0, 0 };
try
{
    bool flag = false;
    WebClient client = new WebClient();
    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    string address = URLHelper.AddParameterToUrl(URLHelper.AddParameterToUrl("https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyA8I8TGIX-Yv4ZeRKveJjvyYxgTpwCemJI", "address", dataRow["Delivery Address"].ToString()), "sensor", "false");
    string input = client.DownloadString(address);
    client.Dispose();
    Newtonsoft.Json.Linq.JObject result = Newtonsoft.Json.Linq.JObject.Parse(input);
    if (result["status"].ToString() == "OK")
    {
        obj.Add("lat", ValidationHelper.GetDouble(result["results"][0]["geometry"]["location"]["lat"].ToString(), (double)0.0));
        obj.Add("lng", ValidationHelper.GetDouble(result["results"][0]["geometry"]["location"]["lng"].ToString(), (double)0.0));
        // Get State
        string State = "Other";
        foreach (Newtonsoft.Json.Linq.JToken types in result["results"][0]["address_components"].Children())
        {

            if (string.Join(" ", types["types"]).Contains("administrative_area_level_1"))
            {
                State = (string)types["short_name"];
                break;
            }
        }
        obj.Add("state", State);
    }
    else
    {
        obj.Add("lat", 0.0);
        obj.Add("lng", 0.0);
        flag = false;
    }
    EventLogProvider.LogInformation("address", "address", address);
    dataRow["Lat"] = obj["lat"].ToString();
    dataRow["Long"] = obj["lng"].ToString();
}
catch (Exception ex)
{
    EventLogProvider.LogInformation("Coordinate", "Coordinate", ex.Message);
}