예제 #1
0
        /// <inheritdoc />
        public MapBoundingBox CalculateBoundingBox(List<MapPoint> pushpins)
        {
            if (pushpins == null || !pushpins.Any())
            {
                throw new ArgumentNullException("pushpins");
            }

            // find the bounding box.
            // South Latitude, lowerst latitude
            // West Longitude, lowerst longitude
            // North Latitude, largest latitide
            // East Longitude. largest longitude
            var firstPin = pushpins[0];
            var minLat = firstPin.Latitude;
            var minLon = firstPin.Longitude;
            var maxLat = firstPin.Latitude;
            var maxLon = firstPin.Longitude;

            foreach (var pushPin in pushpins)
            {
                minLat = minLat > pushPin.Latitude ? pushPin.Latitude : minLat;
                minLon = minLon > pushPin.Longitude ? pushPin.Longitude : minLon;
                maxLat = maxLat < pushPin.Latitude ? pushPin.Latitude : maxLat;
                maxLon = maxLon < pushPin.Longitude ? pushPin.Longitude : maxLon;
            }

            var southWest = new MapPoint(minLat, minLon);
            var northEast = new MapPoint(maxLat, maxLon);

            var boundingBox = new MapBoundingBox(northEast, southWest);

            return boundingBox;
        }
        public async Task<Image> GetStaticMapImage(MapBoundingBox mapAreaBoundingBox, MapRequest mapRequest)
        {
            var mapDescription = mapRequest.MapDescription;

            var mapPathParams = new SortedList<int, string> { { 1, mapDescription.ImagerySet.ToString() } };

            var mapImageQueryArgs = new List<KeyValuePair<string, string>>();
            
            AddMapSizeQueryArguments(mapDescription, mapImageQueryArgs);
            
            AddMapAreaQueryArguments(mapAreaBoundingBox, mapImageQueryArgs);

            AddDisplayPushpins(mapRequest, mapImageQueryArgs);

            AddMapKeyQueryArguments(this.BingAPIKey, mapImageQueryArgs);

            var mapImage = await this.restClient.DownloadImage(BingRestImageryRoadmapsEndpoint, mapPathParams, mapImageQueryArgs);

            return mapImage.ReSize(mapDescription.MapImageScaleFactor);
        }
 private static void AddMapAreaQueryArguments(
     MapBoundingBox boundingBox,
     List<KeyValuePair<string, string>> queryArguments)
 {            
     // Bing needs: South Latitude, West Longitude, North Latitude, East Longitude
     // ?mapArea=37.317227,-122.318439,37.939081,-122.194565&
     var mapArea = string.Format(
         "{0},{1},{2},{3}",
         boundingBox.SouthWest.Latitude,
         boundingBox.SouthWest.Longitude,
         boundingBox.NorthEast.Latitude,
         boundingBox.NorthEast.Longitude);
     queryArguments.Add(new KeyValuePair<string, string>("ma", mapArea));
 }
        public async Task<IMapMetadata> GetStaticMapMetaData(
            MapBoundingBox mapAreaBoundingBox,            
            MapRequest mapRequest)
        {
            var mapDescription = mapRequest.MapDescription;
            IEnumerable<MapPoint> mapPushPinCoordinates = mapRequest.Pushpins.Where(pp=>pp.CustomPushpinIcon != null).Select(p=>p.Coordinate);

            if (mapAreaBoundingBox == null)
            {
                throw new ArgumentNullException("mapAreaBoundingBox");
            }

            var pushPinCoordinates = mapPushPinCoordinates as IList<MapPoint> ?? mapPushPinCoordinates.ToList();
            if (mapPushPinCoordinates == null || !pushPinCoordinates.Any())
            {
                throw new ArgumentNullException("mapPushPinCoordinates");
            }

            var mapPathParams = new SortedList<int, string> { { 1, mapDescription.ImagerySet.ToString() } };

            var metaDataQueryArgs = new List<KeyValuePair<string, string>>
                                        {
                                            new KeyValuePair<string, string>(
                                                "o",
                                                "json"),
                                            new KeyValuePair<string, string>(
                                                "mapmetadata",
                                                "1"),
                                        };

            AddMapSizeQueryArguments(mapDescription, metaDataQueryArgs);

            AddMapAreaQueryArguments(mapAreaBoundingBox, metaDataQueryArgs);            

            AddMapKeyQueryArguments(this.BingAPIKey, metaDataQueryArgs);

            var bingMetaData = await this.GetBingMapMetadata(pushPinCoordinates, mapPathParams, metaDataQueryArgs);

            if (bingMetaData == null)
            {
                return null;
            }

            // NOTE: do this in try catch incase ResourceSets or MetadataResources returned from BING are null.
            try
            {
                return bingMetaData.ResourceSets.FirstOrDefault().MetadataResources.FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw new InvalidDataException("invalid MetaData from Bing REST api", ex);
            }
        }