Search code examples
c#restbing-maps

bingmapsresttoolkit returns wrong longitude and latitude sometimes?


bingmapsresttoolkit sometimes returns wrong latitude and longitude for me, example is post code: CV34 6DD. correct geolocation is

52,275406 & -1,579631

but api returns

56.2158352 & -2.9440218

my code:

            var request = new GeocodeRequest();
            request.BingMapsKey = bingKey;

            request.Query = postCode;

            var result = await request.Execute();
            if (result.StatusCode == 200)
            {
                var toolkitLocation = (result?.ResourceSets?.FirstOrDefault())
                        ?.Resources?.FirstOrDefault()
                        as Location;

                if (toolkitLocation != null)
                {
                    latitude = toolkitLocation.Point.Coordinates[0];
                    longitude = toolkitLocation.Point.Coordinates[1];
                }
            }

any ideas what I`m doing wrong?


Solution

  • The Bing Maps REST Toolkit is just passing through the response from the Bing Maps REST services. So the issue in this case would be with the result from the Bing Maps REST services. Looking into it, it appears that "CV34 6DD" is not recognized by the geocoder.

    What you say is the correct coordinate points to the middle of a field (according to satellite imagery) and about 1km away from where Royal mail says this post code is (again in a field). Doing some research, it looks like this is a newer post code. Note that there is around roughly 20K post code changes a month in the UK. So, in this case, it looks like the issue is that this new post code hasn't made it into Bing maps yet, and as a result it is matching with street address "34 Keilburn, Lundin links, Leven, KY8 6DD, Scotland, United Kingdom" which is far away from the expected result. If you used a structured address query since you know the input is a post code, no result would be returned as this postal code does not exist in the system yet. Usually, the geocoder would move up the hierarchy to the post code area like "CV34" however that doesn't appear to be happening here for some reason.

    Since this is a data issue, you can report it directly to Bing Maps at https://bing.com/maps Search for the post code and it likely will return no results. Click on the "Report a problem" button. (I've also just reported this through there).