예제 #1
0
        public IGeoCodeResult GetCoordinates(string search)
        {
            GeoCodeResult gResult = new GeoCodeResult();
            gResult.Library = this.GetType().ToString();
            try
            {
                string url = BuildSearchQuery(search); //1600+Amphitheatre+Parkway,+Mountain+View,+CA
                string json = new WebRequester(url).GetData();
                if (!String.IsNullOrEmpty(json))
                {
                    JObject result = (JObject)JsonConvert.DeserializeObject(json);
                    if (result["found"].Value<int>() > 0)
                    {
                        foreach (var loc in result["features"])
                        {
                            Location Location = new Location();

                            var properties = loc["properties"];
                            if (properties != null)
                            {
                                Location.Name = properties["name"].GetStringValue();

                                Location.Address = Location.Name;
                                Location.Type = properties["place"].GetStringValue();

                            }

                            var centroid = loc["centroid"];
                            if (centroid != null)
                            {
                                var coordinates = centroid["coordinates"];
                                if (coordinates != null && coordinates.Count() > 1)
                                {
                                    Location.Latitude = coordinates[0].GetDoubleValue();
                                    Location.Longitude = coordinates[1].GetDoubleValue();
                                }

                            }

                            gResult.Locations.Add(Location);
                        }

                    }
                    else
                    {
                        gResult.Error = "No results";
                    }
                }

            }
            catch (Exception e)
            {
                gResult.Error = e.Message;
            }

            return gResult;
        }
예제 #2
0
        public IGeoCodeResult GetCoordinates(string search)
        {
            GeoCodeResult gResult = new GeoCodeResult();
            gResult.Library = this.GetType().ToString();
            try
            {
                string url = BuildSearchQuery(search); //1600+Amphitheatre+Parkway,+Mountain+View,+CA
                string json = new WebRequester(url).GetData();
                if (!String.IsNullOrEmpty(json))
                {
                    JObject result = (JObject)JsonConvert.DeserializeObject(json);
                    if (result["status"].GetStringValue() == GoogleResultStatus.OK.ToString() && result["results"].Count()>0)
                    {

                        foreach (var loc in result["results"])
                        {
                            Location Location = new Location();

                            Location.Address = loc["formatted_address"].GetStringValue();

                            var geometry = loc["geometry"];
                            if (geometry != null)
                            {
                                var location = geometry["location"];
                                if (location != null)
                                {
                                    Location.Latitude = location["lat"].GetDoubleValue();
                                    Location.Longitude = location["lng"].GetDoubleValue();
                                }

                                Location.Type = location["location_type"].GetStringValue();

                            }

                            var addresscomponents = loc["address_components"];
                            if (addresscomponents != null && addresscomponents.Count() > 0)
                            {
                                Location.Name = loc["address_components"][0]["long_names"].GetStringValue(); //!String.IsNullOrEmpty(loc["address_components"][0]["long_name"].Value<String>()) ? loc["address_components"][0]["long_name"].Value<String>() : string.Empty;
                            }
                            gResult.Locations.Add(Location);
                        }

                    }
                    else
                    {
                        gResult.Error = result["status"].ToString();
                    }
                }

            }
            catch (Exception e)
            {
                gResult.Error = e.Message;
            }

            return gResult;
        }
예제 #3
0
파일: BingMap.cs 프로젝트: sbosell/geocode
        public IGeoCodeResult GetCoordinates(string search)
        {
            GeoCodeResult gResult = new GeoCodeResult();
            gResult.Library = this.GetType().ToString();
            if (!String.IsNullOrEmpty(Config.Key))
            {
                try
                {
                    string url = BuildSearchQuery(search); //135+pilkington+avenue,+birmingham
                    string json = new WebRequester(url).GetData();
                    if (!String.IsNullOrEmpty(json))
                    {
                        JObject result = (JObject)JsonConvert.DeserializeObject(json);
                        if (result["resourceSets"][0]["resources"].Count() > 0)
                        {
                            foreach (var loc in result["resourceSets"][0]["resources"])
                            {
                                Location l = new Location();
                                String AddressTemp = String.Empty;
                                l.Name = loc["name"].GetStringValue();

                                var point = loc["point"];
                                if (point != null && point.Count()>1)
                                {
                                    l.Latitude = point["coordinates"][0].GetDoubleValue();
                                    l.Longitude = point["coordinates"][1].GetDoubleValue();
                                }

                                l.Accuracy = loc["confidence"].GetStringValue();
                                if (loc["address"].HasValues)
                                {
                                    AddressTemp = String.Join(",", loc["address"].Values().Where(add => !string.IsNullOrEmpty(add.Value<string>())));
                                }
                                l.Address = AddressTemp;
                                gResult.Locations.Add(l);
                            }

                            //gResult.Count = 1; // for now we are only dealing with the first result
                        }
                        else
                        {
                            gResult.Error = "No Results";

                        }

                    }

                }
                catch
                {

                }
            }
            return gResult;
        }
예제 #4
0
        public IGeoCodeResult GetCoordinates(string search)
        {
            GeoCodeResult gResult = new GeoCodeResult();
            gResult.Library = this.GetType().ToString();
            try
            {
                string url = BuildSearchQuery(search); //135+pilkington+avenue,+birmingham
                string json = new WebRequester(url, Config.UserAgent).GetData();  // Open Street Maps wants the agent name set

                if (!String.IsNullOrEmpty(json))
                {
                    JArray result = (JArray)JsonConvert.DeserializeObject(json);
                    if (result.Count() > 0)
                    {
                        foreach (var loc in result)
                        {
                            Location l = new Location();
                            l.Name = loc["display_name"].GetStringValue();
                            l.Latitude = loc["lat"].GetDoubleValue();
                            l.Longitude = loc["lon"].GetDoubleValue();
                            l.Type = loc["type"].GetStringValue();
                            l.Address = l.Name;
                            gResult.Locations.Add(l);
                        }

                        //gResult.Count = 1;
                    }
                }
            }

            catch
            {

            }
            return gResult;
        }
예제 #5
0
        public IGeoCodeResult GetCoordinates(string search)
        {
            GeoCodeResult gResult = new GeoCodeResult();
            gResult.Library = this.GetType().ToString();
            try
            {
                string url = BuildSearchQuery(search); //1600+Amphitheatre+Parkway,+Mountain+View,+CA
                string json = new WebRequester(url).GetData();
                if (!String.IsNullOrEmpty(json))
                {
                    JObject result = (JObject)JsonConvert.DeserializeObject(json);
                    if (result["ResultSet"]["Found"].Value<int>() > 0)
                    {
                        foreach (var loc in result["ResultSet"]["Results"])
                        {

                            Location Location = new Location();
                            try
                            {
                                string[] AddressStrings = new string[] {
                                loc["house"].GetStringValue(),
                                loc["street"].GetStringValue(),
                                loc["xstreet"].GetStringValue(),
                                loc["unit"].GetStringValue(),
                                loc["unittype"].GetStringValue(),
                                loc["postal"].GetStringValue(),
                                loc["neighborhood"].GetStringValue(),
                                loc["house"].GetStringValue(),
                                loc["city"].GetStringValue(),
                                loc["uzip"].GetStringValue(),
                                loc["county"].GetStringValue(),
                                loc["state"].GetStringValue(),
                                loc["country"].GetStringValue()
                                };
                                Location.Address = String.Join(",", AddressStrings.Where(s => !string.IsNullOrEmpty(s)));
                            }
                            catch { }

                            Location.Type = String.Concat(loc["woetype"].GetStringValue());

                            Location.Latitude = loc["latitude"].GetDoubleValue();
                            Location.Longitude = loc["longitude"].GetDoubleValue();
                            Location.Accuracy = loc["quality"].GetStringValue();
                            Location.Name = String.Concat(loc["line2"].GetStringValue(), loc["line4"].GetStringValue());
                            gResult.Locations.Add(Location);

                        }

                    }
                    else
                    {
                        gResult.Error = result["status"].ToString();
                    }
                }

            }
            catch (Exception e)
            {
                gResult.Error = e.Message;
            }

            return gResult;
        }
예제 #6
0
        public IGeoCodeResult GetCoordinates(string search)
        {
            GeoCodeResult gResult = new GeoCodeResult();
            gResult.Library = this.GetType().ToString();
            if (!String.IsNullOrEmpty(Config.Key))
            {
                try
                {
                    string url = BuildSearchQuery(search); //135+pilkington+avenue,+birmingham
                    string json = new WebRequester(url).GetData();
                    if (!String.IsNullOrEmpty(json))
                    {
                        JObject result = (JObject)JsonConvert.DeserializeObject(json);
                        if (result["results"][0]["locations"].Count() > 0)
                        {
                            foreach (var loc in result["results"][0]["locations"])
                            {
                                try
                                {
                                    Location Location = new Location();
                                    var latLng = loc["latLng"];
                                    if (latLng != null)
                                    {
                                        Location.Latitude = latLng["lat"].GetDoubleValue();
                                        Location.Longitude = latLng["lng"].GetDoubleValue();

                                    }

                                    Location.Accuracy = loc["geocodeQuality"].GetStringValue();

                                    String[] AddressTemp = new string[]
                                    {
                                        loc["street"].GetStringValue(),
                                        loc["adminArea5"].GetStringValue(),
                                        loc["adminArea4"].GetStringValue(),
                                        loc["postalCode"].GetStringValue(),
                                        loc["adminArea1"].GetStringValue()

                                    };

                                    Location.Type = loc["type"].GetStringValue();
                                    Location.Address = String.Join(",", AddressTemp.Where(s => !String.IsNullOrEmpty(s)));
                                    Location.Country = loc["adminArea1"].GetStringValue();
                                    Location.Name = Location.Address;

                                    gResult.Locations.Add(Location);
                                }
                                catch
                                {

                                }
                            }

                        }
                        //gResult.Count = 1;

                    }

                }
                catch
                {

                }
            }
            return gResult;
        }