The Google Static Maps API returns an image (either GIF, PNG or JPEG) in response to a HTTP request via a URL. For each request, you can specify the location of the map, the size of the image, the zoom level, the type of map, and the placement of optional markers at locations on the map.
        public void Markers_ShouldNotUseExtraZeros_BecauseUrlLengthIsLimited()
        {
            StaticMapRequest map = new StaticMapRequest();

            map.Markers.Add(new LatLng(40.0, -60.0));
            map.Markers.Add(new LatLng(41.1, -61.1));
            map.Markers.Add(new LatLng(42.22, -62.22));
            map.Markers.Add(new LatLng(44.444, -64.444));
            map.Markers.Add(new LatLng(45.5555, -65.5555));
            map.Markers.Add(new LatLng(46.66666, -66.66666));
            map.Markers.Add(new LatLng(47.777777, -67.777777));
            map.Markers.Add(new LatLng(48.8888888, -68.8888888));
            // based on this http://gis.stackexchange.com/a/8674/15274,
            // I'm not too concerned about more than 7 decimals of precision.

            string actual = map.ToUri().Query;

            StringAssert.Contains("markers=40,-60&", actual);
            StringAssert.Contains("markers=41.1,-61.1&", actual);
            StringAssert.Contains("markers=42.22,-62.22&", actual);
            StringAssert.Contains("markers=44.444,-64.444&", actual);
            StringAssert.Contains("markers=45.5555,-65.5555&", actual);
            StringAssert.Contains("markers=46.66666,-66.66666&", actual);
            StringAssert.Contains("markers=47.777777,-67.777777&", actual);
            StringAssert.Contains("markers=48.8888888,-68.8888888", actual);
        }
Exemple #2
0
        /// <summary>
        /// Retrieves the map with the given request and writes the image bytes to the given target stream.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="targetStream"></param>
        /// <returns>number of bytes written to the target stream</returns>
        public int GetMapToStream(StaticMapRequest mapOptions, System.IO.Stream outputStream)
        {
            Uri          requestUri      = new Uri(BaseUri, mapOptions.ToUri());
            GoogleSigned signingInstance = GoogleSigned.SigningInstance;

            if (signingInstance != null)
            {
                requestUri = new Uri(signingInstance.GetSignedUri(requestUri));
            }

            int totalBytes = 0;

            WebRequest request = WebRequest.Create(requestUri);

            using (WebResponse response = request.GetResponse())
            {
                Stream inputStream = response.GetResponseStream();

                int       bytesRead          = 0;
                const int BYTE_BUFFER_LENGTH = 4096;
                byte[]    buffer             = new byte[BYTE_BUFFER_LENGTH];

                do
                {
                    bytesRead = inputStream.Read(buffer, 0, BYTE_BUFFER_LENGTH);
                    outputStream.Write(buffer, 0, bytesRead);
                    totalBytes += bytesRead;
                }while(bytesRead > 0);
            }

            return(totalBytes);
        }
		public void Sensor_not_set_throws_invalidoperationexception_when_touri_called()
		{
			StaticMapRequest sm = new StaticMapRequest();
			sm.ToUri();

			Assert.Fail("InvalidOPerationException was expected");
		}
		/// <summary>
		/// Retrieves the map with the given request and writes the image bytes to the given target stream.
		/// </summary>
		/// <param name="request"></param>
		/// <param name="targetStream"></param>
		/// <returns>number of bytes written to the target stream</returns>
		public int GetMapToStream(StaticMapRequest mapOptions, System.IO.Stream outputStream)
		{
			Uri requestUri = new Uri(BaseUri, mapOptions.ToUri());
			GoogleSigned signingInstance = GoogleSigned.SigningInstance;
			if (signingInstance != null)
			{
				requestUri = new Uri(signingInstance.GetSignedUri(requestUri));
			}

			int totalBytes = 0;

			WebRequest request = WebRequest.Create(requestUri);

			using (WebResponse response = request.GetResponse())
			{
				Stream inputStream = response.GetResponseStream();

				int bytesRead = 0; 
				const int BYTE_BUFFER_LENGTH = 4096;
				byte[] buffer = new byte[BYTE_BUFFER_LENGTH];

				do
				{
					bytesRead = inputStream.Read(buffer, 0, BYTE_BUFFER_LENGTH);
					outputStream.Write(buffer, 0, bytesRead);
					totalBytes += bytesRead;
				}
				while (bytesRead > 0);
			}

			return totalBytes;
		}
Exemple #5
0
        /// <summary>
        /// Retrieves a map and returns back the image bytes
        /// </summary>
        /// <param name="request"></param>
        /// <returns>byte array of the image bytes</returns>
        public byte[] GetImageBytes(StaticMapRequest mapOptions)
        {
            MemoryStream outputStream = new MemoryStream();

            GetMapToStream(mapOptions, outputStream);

            return(outputStream.ToArray());
        }
        /// <summary>
        /// Retrieves a map and returns back the image bytes
        /// </summary>
        /// <param name="request"></param>
        /// <returns>byte array of the image bytes</returns>
        public byte[] GetImageBytes(StaticMapRequest mapOptions)
        {
            MemoryStream outputStream = new MemoryStream();

            GetMapToStream(mapOptions, outputStream);

            return outputStream.ToArray();
        }
        public void Scale_argumentoutofrange()
        {
            StaticMapRequest sm = new StaticMapRequest()
            {
                Scale = 3
            };

            Assert.Fail("Expected an ArgumentOutOfRange exception.");
        }
        public void Scale_validvalue_4()
        {
            StaticMapRequest sm = new StaticMapRequest()
            {
                Scale = 4
            };

            Assert.AreEqual(4, sm.Scale);
        }
        public void Scale_validvalue_4()
        {
            StaticMapRequest sm = new StaticMapRequest()
            {
                Scale = 4
            };

            Assert.AreEqual(4, sm.Scale);
        }
 public void Zoom_argumentoutofrange_bottom()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() =>
     {
         StaticMapRequest sm = new StaticMapRequest()
         {
             Zoom = -1
         };
     });
 }
        public void Invalid_size_propert_set()
        {
            StaticMapRequest sm = new StaticMapRequest()
            {
                Sensor = false,
                Size = new System.Drawing.Size(-1, -1)
            };

            Assert.Fail("Invalid size was set to property but no exception happened.");
        }
		public void Zoom_argumentoutofrange_bottom()
		{
			StaticMapRequest sm = new StaticMapRequest()
			{
				Sensor = false,
				Zoom = -1
			};

			Assert.Fail("Zoom was set to invalid value.");
		}
 public void Scale_argumentoutofrange()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() =>
     {
         StaticMapRequest sm = new StaticMapRequest()
         {
             Scale = 3
         };
     });
 }
 public void Invalid_size_max()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() =>
     {
         StaticMapRequest sm = new StaticMapRequest()
         {
             Size = new MapSize(4097, 4097)
         };
     });
 }
		public void Zoom_setbacktonull()
		{
			StaticMapRequest sm = new StaticMapRequest()
			{
				Zoom = 1
			};
			sm.Zoom = null;

			Assert.Pass();
		}
 public void Invalid_size_propert_set()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() =>
     {
         StaticMapRequest sm = new StaticMapRequest()
         {
             Size = new MapSize(-1, -1)
         };
     });
 }
Exemple #17
0
        /// <summary>
        /// Retrieves a static map image at a default size of 512x512 with the given parameters.
        /// </summary>
        /// <param name="center"></param>
        /// <param name="zoom"></param>
        /// <param name="sensor"></param>
        /// <returns></returns>
        public byte[] GetMap(Location center, int zoom, bool sensor)
        {
            StaticMapRequest request = new StaticMapRequest()
            {
                Center = center,
                Zoom   = zoom,
                Sensor = sensor
            };

            return(GetImageBytes(request));
        }
Exemple #18
0
        public void Implicit_Address_set_from_string()
        {
            var map = new StaticMapRequest();

            map.Center = "New York, NY";

            string expected = "New York, NY";
            string actual   = map.Center.ToString();

            Assert.AreEqual(expected, actual);
        }
        public void Zoom_setbacktonull()
        {
            StaticMapRequest sm = new StaticMapRequest()
            {
                Zoom = 1
            };

            sm.Zoom = null;

            Assert.Pass();
        }
Exemple #20
0
        public void Path_NonstandardColor_EncodedProperly()
        {
            var map = new StaticMapRequest();

            map.Paths.Add(new Path(new LatLng(30.0, -60.0))
            {
                Color = MapColor.FromArgb(0x80, 0xA0, 0xC0)
            });
            string color = ExtractColorFromUri(map.ToUri());

            Assert.AreEqual("0X80A0C0FF", color.ToUpper());
        }
		public void StaticMapRequest_Example()
		{
			var map = new StaticMapRequest();
			map.Center = new Location("1600 Amphitheatre Parkway Mountain View, CA 94043");
			map.Size = new System.Drawing.Size(400, 400);
			map.Zoom = 14;
			map.Sensor = false;

			var imgTagSrc = map.ToUri();

			Assert.Pass();
		}
Exemple #22
0
        /// <summary>
        /// Retrieves a static map image at a default size of 512x512 and using the specified image format.
        /// </summary>
        /// <param name="center">A location to center the map on</param>
        /// <param name="zoom">Zoom level to use</param>
        /// <param name="sensor">Pass true if the location was provided via a sensor</param>
        /// <param name="imageFormat">The format of the image</param>
        /// <returns></returns>
        public byte[] GetMap(Location center, int zoom, Size size, GMapsImageFormats imageFormat, bool sensor)
        {
            StaticMapRequest request = new StaticMapRequest()
            {
                Format = imageFormat,
                Size   = size,
                Center = center,
                Zoom   = zoom,
                Sensor = sensor
            };

            return(GetImageBytes(request));
        }
Exemple #23
0
        public byte[] GetMapWithCenterMarked(Location center, int zoom, bool sensor)
        {
            StaticMapRequest request = new StaticMapRequest()
            {
                Center = center,
                Zoom   = zoom,
                Sensor = sensor
            };

            request.Markers.Add(request.Center);

            return(GetImageBytes(request));
        }
Exemple #24
0
        public void Points_One()
        {
            var request = new StaticMapRequest();

            LatLng first = new LatLng(30.1, -60.2);

            request.Path = new Path(first);

            string expected = "https://maps.google.com/maps/api/staticmap?size=512x512&path=30.1,-60.2";
            var    actual   = request.ToUri();

            Assert.AreEqual(expected, actual.ToString());
        }
        public void BasicUri()
        {
            var expected = Helpers.ParseQueryString("/maps/api/staticmap?center=30.1,-60.2&size=512x512");

            StaticMapRequest sm = new StaticMapRequest()
            {
                Center = new LatLng(30.1, -60.2)
            };

            Uri actualUri = sm.ToUri();
            var actual    = Helpers.ParseQueryString(actualUri.PathAndQuery);

            actual.ShouldBeEquivalentTo(expected);
        }
Exemple #26
0
        public void BasicUri()
        {
            string expected = "/maps/api/staticmap?center=30.1,-60.2&size=512x512";

            StaticMapRequest sm = new StaticMapRequest()
            {
                Center = new LatLng(30.1, -60.2)
            };

            Uri    actualUri = sm.ToUri();
            string actual    = actualUri.PathAndQuery;

            Assert.AreEqual(expected, actual);
        }
Exemple #27
0
        public void TwoPaths()
        {
            var map = new StaticMapRequest();

            map.Paths.Add(GreenTriangleInAdaMN());
            map.Paths.Add(RedTriangleNearAdaMN());

            string expectedPath1 = "&path=color:green|47.3017,-96.5299|47.2949,-96.4999|47.2868,-96.5003|47.3017,-96.5299".Replace("|", "%7C");
            string expectedPath2 = "&path=color:red|47.3105,-96.5326|47.3103,-96.5219|47.3045,-96.5219|47.3105,-96.5326".Replace("|", "%7C");
            string actual        = map.ToUri().Query;

            StringAssert.Contains(expectedPath1, actual);
            StringAssert.Contains(expectedPath2, actual);
        }
		public void BasicUri()
		{
			string expected = "/maps/api/staticmap?center=30.1,-60.2&size=512x512&sensor=false";

			StaticMapRequest sm = new StaticMapRequest()
			{
				Sensor = false,
				Center = new LatLng(30.1, -60.2)
			};

			Uri actualUri = sm.ToUri();
			string actual = actualUri.PathAndQuery;

			Assert.AreEqual(expected, actual);
		}
Exemple #29
0
        public void Encode_set_but_not_all_LatLng_positions()
        {
            Assert.Throws <InvalidOperationException>(() =>
            {
                var request = new StaticMapRequest();

                LatLng first    = new LatLng(30.0, -60.0);
                Location second = new Location("New York");
                request.Path    = new Path(first, second)
                {
                    Encode = true
                };

                var actual = request.ToUri();
            });
        }
Exemple #30
0
        public void Encoded_SinglePoint()
        {
            var request = new StaticMapRequest();

            LatLng zero = new LatLng(30.0, -60.0);

            request.Path = new Path(zero)
            {
                Encode = true
            };

            string expected = "https://maps.google.com/maps/api/staticmap?size=512x512&path=enc:_kbvD~vemJ";
            var    actual   = request.ToUri();

            Assert.AreEqual(expected, actual.ToString());
        }
		private void refreshMap()
		{
			if (resultsTreeView.SelectedItem == null) return;

			var location = ((LatLng)((TreeViewItem)resultsTreeView.SelectedItem).Tag);
			var map = new StaticMapRequest();
			map.Center = location;
			map.Zoom = Convert.ToInt32(zoomSlider.Value);
			map.Size = new System.Drawing.Size(332, 332);
			map.Markers.Add(map.Center);
			map.MapType = (MapTypes)Enum.Parse(typeof(MapTypes), ((ComboBoxItem)mapTypeComboBox.SelectedItem).Content.ToString(),true);
			map.Sensor = false;

			var image = new BitmapImage();
			image.BeginInit();
			image.CacheOption = BitmapCacheOption.OnDemand;
			image.UriSource = map.ToUri();
			image.DownloadFailed += new EventHandler<ExceptionEventArgs>(image_DownloadFailed);
			image.EndInit();
			image1.Source = image;
		}
        public void Markers_ShouldNotUseExtraZeros_BecauseUrlLengthIsLimited()
        {
            StaticMapRequest map = new StaticMapRequest { Sensor = false };
            map.Markers.Add(new LatLng(40.0, -60.0));
            map.Markers.Add(new LatLng(41.1, -61.1));
            map.Markers.Add(new LatLng(42.22, -62.22));
            map.Markers.Add(new LatLng(44.444, -64.444));
            map.Markers.Add(new LatLng(45.5555, -65.5555));
            map.Markers.Add(new LatLng(46.66666, -66.66666));
            map.Markers.Add(new LatLng(47.777777, -67.777777));
            map.Markers.Add(new LatLng(48.8888888, -68.8888888));
            // based on this http://gis.stackexchange.com/a/8674/15274,
            // I'm not too concerned about more than 7 decimals of precision.

            string actual = map.ToUri().Query;
            StringAssert.Contains("markers=40,-60&", actual);
            StringAssert.Contains("markers=41.1,-61.1&", actual);
            StringAssert.Contains("markers=42.22,-62.22&", actual);
            StringAssert.Contains("markers=44.444,-64.444&", actual);
            StringAssert.Contains("markers=45.5555,-65.5555&", actual);
            StringAssert.Contains("markers=46.66666,-66.66666&", actual);
            StringAssert.Contains("markers=47.777777,-67.777777&", actual);
            StringAssert.Contains("markers=48.8888888,-68.8888888&", actual);
        }
        /// <summary>
        /// Retrieves a static map image at a default size of 512x512 and using the specified image format.
        /// </summary>
        /// <param name="center">A location to center the map on</param>
        /// <param name="zoom">Zoom level to use</param>
        /// <param name="sensor">Pass true if the location was provided via a sensor</param>
        /// <param name="imageFormat">The format of the image</param>
        /// <returns></returns>
        public byte[] GetMap(Location center, int zoom, System.Drawing.Size size, GMapsImageFormats imageFormat, bool sensor)
        {
            StaticMapRequest request = new StaticMapRequest()
            {
                Format = imageFormat,
                Size = size,
                Center = center,
                Zoom = zoom,
                Sensor = sensor
            };

            return GetImageBytes(request);
        }
        public void TwoPaths()
        {
            var map = new StaticMapRequest
            {
                Sensor = false
            };
            map.Paths.Add(GreenTriangleInAdaMN());
            map.Paths.Add(RedTriangleNearAdaMN());

            string expectedPath1 = "&path=color:green|47.3017,-96.5299|47.2949,-96.4999|47.2868,-96.5003|47.3017,-96.5299".Replace("|", "%7C");
            string expectedPath2 = "&path=color:red|47.3105,-96.5326|47.3103,-96.5219|47.3045,-96.5219|47.3105,-96.5326".Replace("|", "%7C");
            string actual = map.ToUri().Query;
            StringAssert.Contains(expectedPath1, actual);
            StringAssert.Contains(expectedPath2, actual);
        }
        /// <summary>
        /// Retrieves a static map image at a default size of 512x512 with the given parameters.
        /// </summary>
        /// <param name="center"></param>
        /// <param name="zoom"></param>
        /// <param name="sensor"></param>
        /// <returns></returns>
        public byte[] GetMap(Location center, int zoom, bool sensor)
        {
            StaticMapRequest request = new StaticMapRequest()
            {
                Center = center,
                Zoom = zoom,
                Sensor = sensor
            };

            return GetImageBytes(request);
        }
        public void Implicit_Address_set_from_string()
        {
            var map = new StaticMapRequest();
            map.Center = "New York, NY";

            string expected = "New York, NY";
            string actual = map.Center.ToString();

            Assert.AreEqual(expected, actual);
        }
 public void Path_NonstandardColor_EncodedProperly()
 {
     var map = new StaticMapRequest
     {
         Sensor = false
     };
     map.Paths.Add(new Path(new LatLng(30.0, -60.0))
     {
         Color = System.Drawing.Color.FromArgb(0x80, 0xA0, 0xC0)
     });
     string color = ExtractColorFromUri(map.ToUri());
     Assert.AreEqual("0X80A0C0FF", color.ToUpper());
 }
        public byte[] GetImage(StaticMapRequest request)
        {
            var stream = GetStream(request);

            return(StreamToArray(stream));
        }
        public async Task <byte[]> GetImageAsync(StaticMapRequest request)
        {
            var stream = await GetStreamAsync(request);

            return(StreamToArray(stream));
        }
        public Stream GetStream(StaticMapRequest request)
        {
            var uri = new Uri(baseUri, request.ToUri());

            return(http.GetStream(uri));
        }
        public Task <Stream> GetStreamAsync(StaticMapRequest request)
        {
            var uri = new Uri(baseUri, request.ToUri());

            return(http.GetStreamAsync(uri));
        }
        public byte[] GetMapWithCenterMarked(Location center, int zoom, bool sensor)
        {
            StaticMapRequest request = new StaticMapRequest()
            {
                Center = center,
                Zoom = zoom,
                Sensor = sensor
            };
            request.Markers.Add(request.Center);

            return GetImageBytes(request);
        }