Esempio n. 1
0
        public static List <Geospatial> GetLocationGeography(String address)
        {
            String urlString = MAPS_URI + "geocode/json?address=" + GXUtil.UrlEncode(address) + "&sensor=false";
            String ApiKey    = "";

            if (Config.GetValueOf("GoogleApiKey", out ApiKey))
            {
                urlString += "&key=" + ApiKey;
            }

            String response = GXGeolocation.GetContentFromURL(urlString);

            List <Geospatial> result = new List <Geospatial>();

            try
            {
                if (!string.IsNullOrEmpty(response))
                {
                    StringReader   sr   = new StringReader(response);
                    JsonTextReader tr   = new JsonTextReader(sr);
                    JObject        json = (JObject)(tr.DeserializeNext());
                    if (json.Contains("results"))
                    {
                        JArray results = (JArray)json["results"];
                        for (int i = 0; i < results.Length; i++)
                        {
                            JObject jo = (JObject)results[i];
                            if (jo.Contains("geometry"))
                            {
                                JObject geometry = (JObject)jo["geometry"];
                                if (geometry.Contains("location"))
                                {
                                    JObject location = (JObject)geometry["location"];
                                    if (location != null && (location.Contains("lat")) && (location.Contains("lng")))
                                    {
                                        Geospatial point = new Geospatial(Convert.ToDecimal(location["lat"]), Convert.ToDecimal(location["lng"]));
                                        result.Add(point);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (JsonException ex)
            {
                GXLogging.Error(log, "getLocation error json:" + response, ex);
            }

            return(result);
        }
Esempio n. 2
0
        public static List <string> GetAddress(String location)
        {
            String urlString = MAPS_URI + "geocode/json?latlng=" + GXUtil.UrlEncode(location) + "&sensor=false";
            String ApiKey    = "";

            if (Config.GetValueOf("GoogleApiKey", out ApiKey))
            {
                urlString += "&key=" + ApiKey;
            }
            String response = GXGeolocation.GetContentFromURL(urlString);

            List <string> result = new List <string>();

            try
            {
                if (!string.IsNullOrEmpty(response))
                {
                    StringReader   sr   = new StringReader(response);
                    JsonTextReader tr   = new JsonTextReader(sr);
                    JObject        json = (JObject)(tr.DeserializeNext());
                    if (json.Contains("results"))
                    {
                        JArray results = (JArray)json["results"];
                        for (int i = 0; i < results.Length; i++)
                        {
                            JObject jo = (JObject)results[i];
                            if (jo.Contains("formatted_address"))
                            {
                                result.Add((string)jo["formatted_address"]);
                            }
                        }
                    }
                }
            }
            catch (JsonException ex)
            {
                GXLogging.Error(log, "getAddress error json:" + response, ex);
            }

            return(result);
        }