none
Bing Maps: returned .jpeg doesn't seem to be valid RRS feed

  • Question

  • I'm starting to use the Bing Maps REST API, and for a test I just coded to generate a simple aerial map of an address. The Web Response I get back is a 200 (OK), and indeed I get data returned. However, it doesn't seem to be valid .jpg data. What am I doing wrong? Here's the code:

            static void Main(string[] args)
            {
                string uri = @"
    https://dev.virtualearth.net/REST/v1/Imagery/Map/Aerial?pushpin=-85.871147,39.974295&mapLayer=TrafficFlow&format=jpeg&mapMetadata=0&key=[MY_BING_MAPS_KEY]";

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "GET";

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusDescription != "OK")
                    return;

                Stream stream = response.GetResponseStream();
                string serverResponse = "";
                using (StreamReader reader = new StreamReader(stream))
                {
                    serverResponse = reader.ReadToEnd();
                    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                    byte[] bytes = encoding.GetBytes(serverResponse);

                    File.WriteAllBytes(@"C:\temp\Bing Map.jpg", bytes);
                }
            }

    When I try to open "Bing Map.jpg" from Windows, I get an error. Thanks.

    EDIT: It occurred to me that the string coming back from "reader.ReadToEnd()" is probably not ASCIIEncoding, so I change things a bit to us a memory stream, which gets converted to a byte array, which I write to a file via File.WriteAllBytes. At least doing it this way I don't get an error when trying to read the file. But the only thing I get is a large "icon" instead of a map.

    Supposed to be a .jpg image of a map


    • Edited by dysert Thursday, August 9, 2018 12:15 PM improved description
    Thursday, August 9, 2018 11:34 AM

Answers

All replies

  • Hi dysert,

    Have you looked at location "-85.871147,39.97429" in maps.bing.com?   There doesn't seem to be anything there.  I suggest you retry your code with something like Seattle (47.591346, -122.321777) to make the tile content a bit more interesting.

    Sincerely,

    IoTGirl

    Thursday, August 9, 2018 5:22 PM
    Owner
  • Thanks. Yes, I had noticed that I had swapped the lat and lon values. I should have updated my code when I edited the note. I am now passing the correct lat/lon (in the correct order). I still get the "icon" listed above, but I have tried a different map type and it is now working! My guess is that with the Basic service plan, you don't get a map when you ask for an Aerial map.
    Thursday, August 9, 2018 5:39 PM
  • Hi dysert,

    Aerial isn't available at all locations or at all zoom levels.  Check your location in Maps.bing.com and the zoom level and play with that.

    Sincerely,

    IoTGirl


    Thursday, August 9, 2018 6:12 PM
    Owner
  • You can check for imagery availability via the Get Imagery Metadata API found here:

    https://msdn.microsoft.com/en-us/library/ff701716.aspx

    Using this you can determine availability of tile data for your given tile type and zoom levels.

    Justin

    • Proposed as answer by S1mon_Moderator Sunday, August 12, 2018 3:20 PM
    • Marked as answer by dysert Sunday, August 12, 2018 3:45 PM
    Thursday, August 9, 2018 6:36 PM