Пример #1
0
        /// <summary>
        /// Converts GoogleImageFormat enum value to parameter string
        /// </summary>
        /// <param name="format">The image format enum value.</param>
        /// <returns>Parameter string for the given format</returns>
        public static string GetGoogleFormatString(GoogleImageFormat format)
        {
            switch (format)
            {
            case (GoogleImageFormat.PNG):
            {
                return("png");
            }

            case (GoogleImageFormat.PNG32):
            {
                return("png32");
            }

            case (GoogleImageFormat.GIF):
            {
                return("gif");
            }

            case (GoogleImageFormat.JPG):
            {
                return("jpg");
            }

            case (GoogleImageFormat.JPGBaseline):
            {
                return("jpg-baseline");
            }

            default:
            {
                return("jpg-baseline");
            }
            }
        }
        public string GetMapUri(float latitude, float longitude, int zoom, int scale, int width, int height, GoogleMapType mapType, GoogleImageFormat format, IList <TrackCoordinate> trackCoordinates)
        {
            var requestUri = string.Empty;

            for (int i = _resolutionModifierStep; i <= _maxResolutionModifierStep; i++)
            {
                if (i > _resolutionModifierStep)
                {
                    _logger.LogTrace($"Attempting to build Track Map requestUri at resolution of {i}");
                }
                string pathParameter = BuildMapPath(trackCoordinates, i);

                requestUri = string.Format(
                    "https://maps.googleapis.com/maps/api/staticmap?center={0}&zoom={1}&size={2}&maptype={3}&format={4}{5}&key={6}",
                    $"{latitude},{longitude}",
                    zoom,
                    $"{width}x{height}",
                    GoogleMapHelper.GetGoogleMapTypeString(mapType),
                    GoogleMapHelper.GetGoogleFormatString(format),
                    pathParameter,
                    GoogleMapHelper.ApiKey);

                if (requestUri.Length < 8192)
                {
                    break;
                }
                else if (requestUri.Length > 8192 && i >= _maxResolutionModifierStep)
                {
                    throw new InvalidOperationException($"requestUri too long: {requestUri.Length}");
                }
            }

            return(requestUri);
        }
 public string GetMapUri(float latitude, float longitude, int zoom, int scale, int width, int height, GoogleMapType mapType, GoogleImageFormat format)
 {
     return(GetMapUri(latitude, longitude, zoom, scale, width, height, mapType, format, null));
 }
        public async Task <byte[]> GetMapAsync(float latitude, float longitude, int zoom, int scale, int width, int height, GoogleMapType mapType, GoogleImageFormat format, IList <TrackCoordinate> trackCoordinates)
        {
            byte[] bitmap = null;

            try
            {
                var requestUri = string.Empty;

                for (int i = _resolutionModifierStep; i <= _maxResolutionModifierStep; i++)
                {
                    if (i > _resolutionModifierStep)
                    {
                        _logger.LogTrace($"Attempting to build Track Map requestUri at resolution of {i}");
                    }
                    string pathParameter = BuildMapPath(trackCoordinates, i);

                    requestUri = string.Format(
                        "https://maps.googleapis.com/maps/api/staticmap?&scale=1center={0}&zoom={1}&size={2}&maptype={3}&format={4}&key={5}{6}",
                        $"{latitude},{longitude}",
                        zoom,
                        $"{width}x{height}",
                        GoogleMapHelper.GetGoogleMapTypeString(mapType),
                        GoogleMapHelper.GetGoogleFormatString(format),
                        GoogleMapHelper.ApiKey,
                        pathParameter);

                    if (requestUri.Length < 8192)
                    {
                        break;
                    }
                    else if (requestUri.Length > 8192 && i >= _maxResolutionModifierStep)
                    {
                        throw new InvalidOperationException($"requestUri too long: {requestUri.Length}");
                    }
                }
                _logger.LogTrace($"Google Maps requestUri: {requestUri}");

                using (WebClient wc = new WebClient())
                {
                    bitmap = await wc.DownloadDataTaskAsync(new Uri(requestUri));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Exception in GetMapAsync");
                throw;
            }

            return(bitmap);
        }