0

I am trying to receive map image for specific location.

So first, I translated the name into the longitude and latitude. This is working and is done by code below:

                string singleLocation = streamReader.ReadLine();
                Console.WriteLine(singleLocation);

                var requestUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false", Uri.EscapeDataString(singleLocation));

                var request = WebRequest.Create(requestUri);
                var response = request.GetResponse();
                var xdoc = XDocument.Load(response.GetResponseStream());

                var result = xdoc.Element("GeocodeResponse").Element("result");
                var locationElement = result.Element("geometry").Element("location");
                var lat = locationElement.Element("lat");
                var lng = locationElement.Element("lng");

                var lat2 = lat.Value;
                lat2 = lat2.Replace('.', ',');
                double latitude;
                double.TryParse(lat2, out latitude);

                var lng2 = lng.Value;
                lng2 = lng2.Replace('.', ',');
                double longitude;
                double.TryParse(lng2, out longitude);

                Console.WriteLine(latitude + " " + longitude);

And then I am trying to receive an image of map for specific location. And this part is not working properly. I do not know how to handle response.

                var requestUri2 = string.Format(
                    "https://maps.googleapis.com/maps/api/staticmap?center={0}&zoom={1}&size={2}x{3}&maptype=roadmap",
                    Uri.EscapeDataString(singleLocation),
                    Uri.EscapeDataString(5.ToString()),
                    Uri.EscapeDataString(640.ToString()),
                    Uri.EscapeDataString(640.ToString()));

                var request2 = WebRequest.Create(requestUri2);
                var response2 = request2.GetResponse();

                var map = response2.GetResponseStream();

The application must to just do the work it doesnt need to be pretty or do complex process, neither use good wrapers if it is not required. I would like to just save this map into file like jpg or png.

EDIT: Solution is here: How to use httpwebrequest to pull image from website to local file

Community
  • 1
  • 1
jan kowalski
  • 189
  • 3
  • 21

1 Answers1

2

I use this code. The "mapa" byte[] you can save or show it where you want. en latitud and longitud you've to use your values.

byte[] mapa                
string url = @"http://maps.googleapis.com/maps/api/staticmap?center=" + latitud +"," + longitud + "&zoom=15&size=504x400&maptype=roadmap&markers=color:red%7Clabel:%7C" + latitud + "," + longitud + "&sensor=false";

                using (WebClient wc = new WebClient())
                {
                    mapa = wc.DownloadData(url);
                }

After that you can check http://stackoverflow.com/questions/8946846/converting-a-byte-array-to-png-jpg

paio
  • 167
  • 4