Пример #1
0
        /// <summary>
        /// Send the request to the Bing Maps REST API
        /// and deserialize the JSON response.
        /// </summary>
        private Response GetJsonResponse(string requestUrl)
        {
            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new Exception(String.Format(
                                            "Server error (HTTP {0}: {1}).",
                                            response.StatusCode, response.StatusDescription));
                }
                DataContractJsonSerializer jsonSerializer =
                    new DataContractJsonSerializer(typeof(BingMapsRestV1.Response));
                object objResponse =
                    jsonSerializer.ReadObject(response.GetResponseStream());
                BingMapsRestV1.Response jsonResponse =
                    objResponse as BingMapsRestV1.Response;
                return(jsonResponse);
            }
        }
Пример #2
0
        /// <summary>
        /// Calculates the shortest distance between two points based on their zipcodes,
        /// and returns the value.
        /// </summary>
        private double CalculateDistance(string startPoint, string endPoint)
        {
            double distance = 0.0;

            // Required start and end waypoint parameters (location string
            // or a pair of latitude and longitude coordinates).
            string fromAddress = startPoint;
            string toAddress   = endPoint;

            // Optional route calculation parameters:
            // Optimize route for the shortest distance.
            string optimize = "distance";

            // Driving travel mode: driving/walking/transit
            string travelMode = "driving";

            // Measure distance: km/mi
            string distanceUnit = "km";

            // User needs to put here his/her Bing Maps Key
            string BingMapsKey = "Au9tmAwIDZ2XAQVedm85L51EujHtSfgvdFBLpLXBvH-p94kNrokavf00POMu74Xz";

            // Create a REST request for the route's details.
            string requestUrl = "http://dev.virtualearth.net/REST/v1/Routes" +
                                "?output=json" + "&key=" + BingMapsKey + "&wp.0=" + fromAddress +
                                "&wp.1=" + toAddress + "&optimize=" + optimize +
                                "&travelMode=" + travelMode + "&distanceUnit=" + distanceUnit;

            // Send the request and parse the response.
            BingMapsRestV1.Response jsonResponse = GetJsonResponse(requestUrl);

            // Get the shortest distance
            distance = (from Route r in jsonResponse.ResourceSets[0].Resources
                        orderby r.TravelDistance ascending
                        select r.TravelDistance).First();

            return(distance);
        }