none
Trying to get NAVTEQNA Spatial Data Results RRS feed

  • Question

  • I am trying to query the spatial data services for NAVTEQNA and I keep getting an error.

    "An exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll but was not handled in user code

    Additional information: There was an error deserializing the object of type BingMapsRESTService.Common.JSON.Response. Encountered unexpected character 'P'."

    Here is my code:

      //Call the Bing Rest Service
            private async Task<Response> GetResponse(Uri uri)
            {
                HttpClient client = new HttpClient();
                var response = await client.GetAsync(uri);
    
                using (var stream = await response.Content.ReadAsStreamAsync())
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
                    return ser.ReadObject(stream) as Response;
                }
            }
    
            private async void Search_Btn(object sender, RoutedEventArgs e)
            {
    
                //Create the request URL for the Geocoding service
                Uri geocodeRequest = new Uri(string.Format("http://spatial.virtualearth.net/REST/v1/data/f22876ec257b474b82fe2ffcb8393150/NavteqNA/NavteqPOIs?spatialFilter=nearby(32.46648,-81.75399,50)&$format=json&jsonp=POICallback&$filter=EntityTypeID Eq '4581'&key={0}", myMap.Credentials));
    
                //Make a request and get the response
                Response r = await GetResponse(geocodeRequest);
            }

    Any help would be good. As well, Do I need to request access in order to use the NAVTEQNA Spatial Data?

    Thanks

    Tex


    • Edited by Texeco Tuesday, November 26, 2013 9:49 PM
    Tuesday, November 26, 2013 9:47 PM

Answers

  • The BingMapsRESTService.Common.JSON.Response class is for the Bing Maps REST services. The Navteq data is in the Bing Spatial Data Services which is a completely different service that has a different structure. Thus the error you are receiving. I have the classes you need in one of my code samples here: http://code.msdn.microsoft.com/Augmented-Reality-with-bcb17045/sourcecode?fileId=85735&pathId=55633198

    You will need the Response, ResultSet and Result classes from that code sample.


    http://rbrundritt.wordpress.com

    Wednesday, November 27, 2013 9:53 AM
  • It's marked as an answered as the solution points to a working code sample that does exactly what you are trying to do. Note there are three files, not 2. Double check your namespaces to ensure you have the correct Response class. Also, these classes are for JSON only. There is no need to use XML, it only makes the response larger which means more download time and turns no more data than if you use JSON. All of these classes are designed to use JSON. Also, your request URL is also set to return JSON as well.

    Also, you should be parsing the response like this:

    private async Task<Response> GetResponse(Uri uri) 
    { 
    	try 
    	{ 
    		System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); 
    		var response = await client.GetAsync(uri); 
    
    		using (var stream = await response.Content.ReadAsStreamAsync()) 
    		{ 
    			DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response)); 
    
    			return ser.ReadObject(stream) as Response; 
    		} 
    	} 
    	catch (Exception) 
    	{ 
    		return null; 
    	} 
    } 


    http://rbrundritt.wordpress.com

    • Proposed as answer by Ricky_Brundritt Tuesday, December 3, 2013 3:52 PM
    • Marked as answer by Texeco Tuesday, December 3, 2013 6:10 PM
    Tuesday, December 3, 2013 3:52 PM

All replies

  • The BingMapsRESTService.Common.JSON.Response class is for the Bing Maps REST services. The Navteq data is in the Bing Spatial Data Services which is a completely different service that has a different structure. Thus the error you are receiving. I have the classes you need in one of my code samples here: http://code.msdn.microsoft.com/Augmented-Reality-with-bcb17045/sourcecode?fileId=85735&pathId=55633198

    You will need the Response, ResultSet and Result classes from that code sample.


    http://rbrundritt.wordpress.com

    Wednesday, November 27, 2013 9:53 AM
  • I don't know why this is marked as an answer. This solution did not work for me. I tried to use the two files that you suggested, but couldn't get it to work either.

    I ended up being able to output the XML response, now I am just having issues trying to get the elements within the XML.

    Here's my code:

    public async void FindPOIItems()
            {
                // Find all previously uploaded MyShops entities located within
                // a certain radius around a point.
                // Custom name of spatial data source created during upload
                string dataSourceName = "NavteqNA";
                // Name of entities in the data source
                string dataEntityName = "NavteqPOIs";
                // Unique access ID assigned to your data source by Bing Maps
                // e.g. f8986xxxxxxxc844b
                string accessId = "f22876ec257b474b82fe2ffcb8393150";
                // Your Bing Maps Spatial Data Services query key.
                string bingMapsKey = myMap.Credentials;
                // Coordinates of the point to search from.
                double SearchLatitude = 47.63674;
                double SearchLongitude = -122.30413;
                // Search radius
                double Radius = 10; // km
                //string entityTypeID = "7011";//hotels
                //string entityTypeID = "9996";//Coffee Shops
                string entityTypeID = "5540";//Petrol/Gasoline Station
                // Setup REST request to query our uploaded customer data
                string requestUrl = string.Format("http://spatial.virtualearth.net/REST/v1/data/{0}/{1}/{2}" +
                  "?spatialFilter=nearby({3},{4},{5})" + "&$filter=EntityTypeID%20EQ%20" + entityTypeID + "&$top=100" + "&key={6}", accessId, dataSourceName,
                  dataEntityName, SearchLatitude, SearchLongitude, Radius, bingMapsKey);
                XDocument response = await GetXmlResponse(requestUrl);
                ProcessEntityElements(response);
            }
    
            public async static Task<XDocument> GetXmlResponse(string requestUrl)
            {
                try
                {
                    WebRequest request = WebRequest.Create(requestUrl);
                    WebResponse response = await request.GetResponseAsync();
    
                    XDocument xmlDoc = XDocument.Load(response.GetResponseStream());
                    return (xmlDoc);
                }
                catch 
                {
                    return null;
                }
            }
    
            private void ProcessEntityElements(XDocument response)
            {
                var q = response.Elements();
                foreach (XElement ele in q)
                {               
                    ResultsList.Text += ele.ToString();
                }
             }

    So this will output the full XML file, but I am trying to get a single element for example the Address of each entry. Any help would be great

    Monday, December 2, 2013 5:10 PM
  • It's marked as an answered as the solution points to a working code sample that does exactly what you are trying to do. Note there are three files, not 2. Double check your namespaces to ensure you have the correct Response class. Also, these classes are for JSON only. There is no need to use XML, it only makes the response larger which means more download time and turns no more data than if you use JSON. All of these classes are designed to use JSON. Also, your request URL is also set to return JSON as well.

    Also, you should be parsing the response like this:

    private async Task<Response> GetResponse(Uri uri) 
    { 
    	try 
    	{ 
    		System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); 
    		var response = await client.GetAsync(uri); 
    
    		using (var stream = await response.Content.ReadAsStreamAsync()) 
    		{ 
    			DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response)); 
    
    			return ser.ReadObject(stream) as Response; 
    		} 
    	} 
    	catch (Exception) 
    	{ 
    		return null; 
    	} 
    } 


    http://rbrundritt.wordpress.com

    • Proposed as answer by Ricky_Brundritt Tuesday, December 3, 2013 3:52 PM
    • Marked as answer by Texeco Tuesday, December 3, 2013 6:10 PM
    Tuesday, December 3, 2013 3:52 PM
  • So I double checked all of my namespace. Made sure that the three files are included and updated my GetResponse method. And it is still returning no results. Here is the code that I have

    private async Task<Response> GetResponse(Uri uri)
            {
                try
                {
                    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
                    var response = await client.GetAsync(uri);
    
                    using (var stream = await response.Content.ReadAsStreamAsync())
                    {
                        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
    
                        return ser.ReadObject(stream) as Response;
                    }
                }
                catch (Exception)
                {
                    return null;
                } 
    
            }
    
            private async void Search_Btn(object sender, RoutedEventArgs e)
            {
    
     string dataSourceName = "NavteqNA";
            // Name of entities in the data source
            string dataEntityName = "NavteqPOIs";
            // Unique access ID assigned to your data source by Bing Maps
            // e.g. f8986xxxxxxxc844b
            string accessId = "f22876ec257b474b82fe2ffcb8393150";
            
            // Coordinates of the point to search from.
            double SearchLatitude = 47.63674;
            double SearchLongitude = -122.30413;
            // Search radius
            double Radius = 10; // km
            //string entityTypeID = "7011";//hotels
            string entityTypeID = "9996";//Coffee Shops
            //string entityTypeID = "5540";//Petrol/Gasoline Station
                // Your Bing Maps Spatial Data Services query key.
                string bingMapsKey = myMap.Credentials;
                
                //Create the request URL for the Geocoding service
                string geocodeRequest = string.Format("http://spatial.virtualearth.net/REST/v1/data/{0}/{1}/{2}" +
                  "?spatialFilter=nearby({3},{4},{5})" + "&$filter=EntityTypeID%20EQ%20" + entityTypeID + "&$format=json&$top=100" + "&key={6}", accessId, dataSourceName,
                  dataEntityName, SearchLatitude, SearchLongitude, Radius, bingMapsKey);
    
                //Make a request and get the response
                Response response = await GetResponse(new Uri(geocodeRequest));
    
                if (response != null &&
                       response.ResultSet != null &&
                       response.ResultSet.Results != null &&
                       response.ResultSet.Results.Length > 0)
                {
                    //Loop through the results and create PoiItems that can be added to the ARDisplay 
                    foreach (var r in response.ResultSet.Results)
                    {
                        Location loc = new Location(r.Latitude, r.Longitude);
    
                        PoiItem item = new PoiItem()
                        {
                            Name = r.DisplayName,
                            AddressLine = r.AddressLine,
                            //Some locations have postal code information in the locality property. 
                            Locality = (r.PostalCode.StartsWith(r.Locality)) ? r.AdminDistrict : r.Locality,
                            PostalCode = r.PostalCode,
                            EntityTypeID = r.EntityTypeID
    
                        };
                        ResultsList.Items.Add(item);
                    }
                }
            }

    I use this query and remove the "&$format=json" and I can get the xml with the results. Why wouldn't this work with the json?

    Tuesday, December 3, 2013 5:58 PM
  • So I don't know why, but I was having issues with VS and closed it. Restarted my application and it started working.

    The solution that you provided does work.

    Tuesday, December 3, 2013 6:10 PM