Пример #1
0
        private string AddressFromLatLng(string pLatitude, string pLongitude)
        {
            GeocodeService.ReverseGeocodeRequest reverseGeocodeRequest = new GeocodeService.ReverseGeocodeRequest();

            // Set the credentials using a valid Bing Maps key
            reverseGeocodeRequest.Credentials = new Credentials();
            reverseGeocodeRequest.Credentials.ApplicationId = m_BingKey;

            // Set the point to use to find a matching address
            GeocodeService.Location point = new GeocodeService.Location();
            point.Latitude = Convert.ToDouble(pLatitude);
            point.Longitude = Convert.ToDouble(pLongitude);

            reverseGeocodeRequest.Location = point;

            // Make the reverse geocode request
            GeocodeService.GeocodeServiceClient geocodeService =
            new GeocodeService.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
            GeocodeService.GeocodeResponse geocodeResponse = geocodeService.ReverseGeocode(reverseGeocodeRequest);

            vPosition = geocodeResponse.Results[0].DisplayName;

            if (vPosition != null)
                StartSearch(vPosition);

            return vPosition;
        }
        /// <summary>
        /// Converts GPS coordinates into address.
        /// </summary>
        /// <param name="coordinates">"latitue, longitude"</param>
        /// <returns>address + ; + country + ; + city</returns>
        public string MakeReverseGeocodeRequest(string coordinates)
        {
            try
            {
                CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
                // Set a Bing Maps key before making a request

                GeocodeService.ReverseGeocodeRequest reverseGeocodeRequest = new GeocodeService.ReverseGeocodeRequest();

                // Set the credentials using a valid Bing Maps key
                reverseGeocodeRequest.Credentials = new GeocodeService.Credentials();
                reverseGeocodeRequest.Credentials.ApplicationId = key;

                string[] digits = coordinates.Split(',');
                double lat = double.Parse(digits[0].Trim(), culture);
                double lon = double.Parse(digits[1].Trim(), culture);

                // Set the point to use to find a matching address
                GeocodeService.Location point = new GeocodeService.Location();
                point.Latitude = lat;
                point.Longitude = lon;

                reverseGeocodeRequest.Location = point;

                // Make the reverse geocode request
                GeocodeService.GeocodeServiceClient geocodeService =
                new GeocodeService.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
                GeocodeService.GeocodeResponse geocodeResponse = geocodeService.ReverseGeocode(reverseGeocodeRequest);

                if (geocodeResponse.Results.Count() == 0)
                {
                    return "No location found.";
                }
                return geocodeResponse.Results[0].Address.AddressLine + ";" + geocodeResponse.Results[0].Address.CountryRegion + ";" + geocodeResponse.Results[0].Address.Locality;

            }
            catch (Exception)
            {
                return "An exception occurred.";

            }
        }