none
Bing maps reverse geocoding RRS feed

  • Question

  • Hello all,


    I am using the below code to get the lat lon from specifing an address in the text box as per some instructions on microsoft http://msdn.microsoft.com/en-us/library/dd221354.aspx

    On completing the above example from microsoft, the code contained several erroprs which led me potential fixes here:http://social.msdn.microsoft.com/Forums/en-US/vemapcontroldev/thread/aa3b172b-a5f2-4843-b586-489725c617a3/

    However in my below code  I am still having some problems getting the code to work.


           private String GeocodeAddress(string address)
            {
                string results = "";
                string key = "BING_MAPS_KEY";
                GeocodeRequest geocodeRequest = new GeocodeRequest();
    
                // Set the credentials using a valid Bing Maps key
                geocodeRequest.Credentials = new geocodeservice.Credentials();
                geocodeRequest.Credentials.ApplicationId = key;
    
                // Set the full address query
                geocodeRequest.Query = address;
    
                // Set the options to only return high confidence results 
                ConfidenceFilter[] filters = new ConfidenceFilter[1];
                filters[0] = new ConfidenceFilter();
                filters[0].MinimumConfidence = geocodeservice.Confidence.High;
    
                // Add the filters to the options
                GeocodeOptions geocodeOptions = new GeocodeOptions();
                geocodeOptions.Filters = filters;
                geocodeRequest.Options = geocodeOptions;
    
                // Make the geocode request
    
                GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
    
    
                GeocodeRequest request = new GeocodeRequest()
                {
                    Credentials = new geocodeservice.Credentials() { ApplicationId = "key" },
                    Address = new geocodeservice.Address() { Locality = "TOWN", AddressLine = address, CountryRegion = "COUNTRY", PostalCode = "ZIP" },
                    Options = new GeocodeOptions() { Filters = new ObservableCollection<FilterBase>() }
                };
                request.Options.Filters.Add(new ConfidenceFilter() { MinimumConfidence = geocodeservice.Confidence.Medium });
    
                geocodeService.GeocodeAsync(request);
                geocodeService.GeocodeCompleted += geocodeService_GeocodeCompleted;
    
                GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);
    
    
                if (results.Length > 0)
                    results = String.Format("Latitude: {0}\nLongitude: {1}",
                      geocodeResponse.Results[0].Locations[0].Latitude,
                      geocodeResponse.Results[0].Locations[0].Longitude);
                else
                    results = "No Results Found";
    
                return results;
            }
    
            private void geocodeService_GeocodeCompleted(object sender, geocodeservice.GeocodeCompletedEventArgs e)
            {
                geocodeservice.GeocodeResult result = e.Result.Results[0];
            }

    The first error ObservableCollection as follows:


    Error    32    Cannot implicitly convert type 'System.Collections.ObjectModel.ObservableCollection<SilverLightDatabaseExample.geocodeservice.FilterBase>' to 'SilverLightDatabaseExample.geocodeservice.FilterBase[]'    D:\SilverLightDatabaseExample

    The second error

    Error    33    'System.Array' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)    D:\SilverLightDatabaseExample  

    'SilverLightDatabaseExample.geocodeservice.GeocodeServiceClient' does not contain a definition for 'Geocode'

    Any help would be appreciated in on this, thanks everyone for your help in advance.


    Microsoft SharePoint 2007, Microsoft SQL 2008


    • Moved by JOshiro Friday, October 5, 2012 6:23 PM Bing maps question (From:Live Connect)
    • Edited by Ricky_Brundritt Monday, October 22, 2012 8:43 AM Removing Bing Maps Key
    Friday, October 5, 2012 1:57 PM

Answers

  • Your issue is that you have configured your service to return a ObservableCollection but your code expects an array. Right click on your service reference and your can change this setting.

    http://rbrundritt.wordpress.com

    Saturday, October 6, 2012 12:53 PM

All replies

  • Your issue is that you have configured your service to return a ObservableCollection but your code expects an array. Right click on your service reference and your can change this setting.

    http://rbrundritt.wordpress.com

    Saturday, October 6, 2012 12:53 PM
  • Hi and many thanks for your swift answer.

    I followed as per your instructions and it helped, however another appeared as followed:

                // Add the filters to the options
                GeocodeOptions geocodeOptions = new GeocodeOptions();
                geocodeOptions.Filters = filters;
                geocodeRequest.Options = geocodeOptions;

    The above underline has the containing error:

    Error    1    Cannot implicitly convert type 'SilverLightDatabaseExample.geocodeservice.ConfidenceFilter[]' to 'System.Collections.ObjectModel.ObservableCollection<SilverLightDatabaseExample.geocodeservice.FilterBase>

    I cannot work passed this one at the moment, please help and much thanks in advance.


    Microsoft SharePoint 2007, Microsoft SQL 2008

    Monday, October 8, 2012 12:31 PM
  • Any ideas anyone??

    thanks in advance


    Microsoft SharePoint 2007, Microsoft SQL 2008

    Wednesday, October 10, 2012 8:22 PM
  • Make sure you update the service reference otherwise the local classes will still expect an ObservableCollection instead of an array.

    http://rbrundritt.wordpress.com

    Thursday, October 11, 2012 7:14 PM
  • Hello All,

    Did this as recommended and still the below error.

    Error    8    Cannot implicitly convert type 'SilverLightDatabaseExample.geocodeservice.ConfidenceFilter[]' to 'System.Collections.ObjectModel.ObservableCollection<SilverLightDatabaseExample.geocodeservice.FilterBase>'  

    Does any one out there have an working example of reverse geocoding with a simple text box to specify the address to retreive the lat long in silverlight visual studio 2010 please?

    Many thanks in advance.


    Microsoft SharePoint 2007, Microsoft SQL 2008

    Friday, October 12, 2012 10:01 AM
  • Can you email your code to me at ricky_brundritt at Hotmail dot com and I can have a look.

    http://rbrundritt.wordpress.com

    Friday, October 12, 2012 4:20 PM
  • Good Morning,

    Any further thoughts or ideas?

    Randine


    Microsoft SharePoint 2007, Microsoft SQL 2008

    Friday, October 19, 2012 10:32 AM
  • Hello All,

    I am really trying to get a silverlight app up and runing which does a simple address lookup, cannot seem to get this working from any example from Microsoft websites, my latest I am using is from

    http://www.microsoft.com/maps/isdk/silverlight/#MapControlInteractiveSdk.Tutorials.Services.Geocode

    But in the source code in Visual Studio 2010 I receive a lot of the following errors.


    Error    9    The type or namespace name 'PlatformServices' could not be found (are you missing a using directive or an assembly reference?)    D:\SilverLight DB\SilverLightDatabaseExample\SilverLightDatabaseExample\GeocodeLayer.cs    61    35    SilverLightDatabaseExample

    Error    22    The name 'Output' does not exist in the current context    D:\SilverLight DB\SilverLightDatabaseExample\SilverLightDatabaseExample\Geocode.xaml.cs    94    13    SilverLightDatabaseExample

    Error    25    Cannot convert lambda expression to delegate type 'System.Action<Microsoft.Maps.MapControl.Credentials>' because the parameter types do not match the delegate parameter types    D:\SilverLight DB\SilverLightDatabaseExample\SilverLightDatabaseExample\Geocode.xaml.cs    98    17    SilverLightDatabaseExample

    I have looked everywhere to fix this error and since the earlier examples cannot be resolved, can anyone help with this example, how do I reference PlatformServices, why does none of the Microsoft Silverlight bing example work with silverlight 5?

           public Geocode()
            {
                InitializeComponent();
            }
    
    
            private int geocodesInProgress;
            private PlatformServices.GeocodeServiceClient geocodeClient;
            private PlatformServices.GeocodeServiceClient GeocodeClient
            {
                get
                {
                    if (null == geocodeClient)
                    {
                        //Handle http/https; OutOfBrowser is currently supported on the MapControl only for http pages
                        bool httpsUriScheme = !Application.Current.IsRunningOutOfBrowser && HtmlPage.Document.DocumentUri.Scheme.Equals(Uri.UriSchemeHttps);
                        BasicHttpBinding binding = httpsUriScheme ? new BasicHttpBinding(BasicHttpSecurityMode.Transport) : new BasicHttpBinding(BasicHttpSecurityMode.None);
                        UriBuilder serviceUri = new UriBuilder("http://dev.virtualearth.net/webservices/v1/GeocodeService/GeocodeService.svc");
                        if (httpsUriScheme)
                        {
                            //For https, change the UriSceheme to https and change it to use the default https port.
                            serviceUri.Scheme = Uri.UriSchemeHttps;
                            serviceUri.Port = -1;
                        }
    
                        //Create the Service Client
                        geocodeClient = new PlatformServices.GeocodeServiceClient(binding, new EndpointAddress(serviceUri.Uri));
                        geocodeClient.GeocodeCompleted += new EventHandler<PlatformServices.GeocodeCompletedEventArgs>(client_GeocodeCompleted);
                    }
                    return geocodeClient;
                }
            }
    
            private GeocodeLayer geocodeLayer;
            private GeocodeLayer GeocodeLayer
            {
                get
                {
                    if (null == geocodeLayer)
                    {
                        geocodeLayer = new GeocodeLayer(MyMap);
                    }
                    return geocodeLayer;
                }
            }
    
            private void GeocodeAddress(string address)
            {
                PlatformServices.GeocodeRequest request = new PlatformServices.GeocodeRequest();
                request.Culture = MyMap.Culture;
                request.Query = address;
                // Don't raise exceptions.
                request.ExecutionOptions = new PlatformServices.ExecutionOptions();
                request.ExecutionOptions.SuppressFaults = true;
    
                // Only accept results with high confidence.
                request.Options = new PlatformServices.GeocodeOptions();
                // Using ObservableCollection since this is the default for Silverlight proxy generation.
                request.Options.Filters = new ObservableCollection<PlatformServices.FilterBase>();
                PlatformServices.ConfidenceFilter filter = new PlatformServices.ConfidenceFilter();
                filter.MinimumConfidence = PlatformServices.Confidence.High;
                request.Options.Filters.Add(filter);
    
                Output.Text = "< geocoding " + address + " >";
                geocodesInProgress++;
    
                MyMap.CredentialsProvider.GetCredentials(
                    (Credentials credentials) =>
                    {
                        //Pass in credentials for web services call.
                        //Replace with your own Credentials.
                        request.Credentials = credentials;
    
                        // Make asynchronous call to fetch the data ... pass state object.
                        GeocodeClient.GeocodeAsync(request, address);
                    });
            }
    
            private void client_GeocodeCompleted(object sender, PlatformServices.GeocodeCompletedEventArgs e)
            {
                // Callback when the geocode is finished.
                string outString;
                geocodesInProgress--;
                try
                {
                    if (e.Result.ResponseSummary.StatusCode != PlatformServices.ResponseStatusCode.Success)
                    {
                        outString = "error geocoding ... status <" + e.Result.ResponseSummary.StatusCode.ToString() + ">";
                    }
                    else if (0 == e.Result.Results.Count)
                    {
                        outString = "No results";
                    }
                    else
                    {
                        // Only report on first result.
                        outString = e.Result.Results[0].DisplayName;
                        Location loc = GeocodeLayer.AddResult(e.Result.Results[0]);
                        // Zoom the map to the location of the item.
                        MyMap.SetView(loc, 18);
                    }
                }
                catch
                {
                    outString = "Exception raised";
                }
    
                Output.Text = outString;
            }
    
    
            private void GeocodeInput()
            {
                // Geocode whatever is in the textbox.
                string address = Input.Text;
                if (address.Length > 0)
                {
                    GeocodeAddress(address);
                }
            }




    Microsoft SharePoint 2007, Microsoft SQL 2008

    Monday, October 22, 2012 10:20 AM
  • I'm assuming you called your service reference PlatformService, if not that's why you are getting the first error. To be honest it is extremely easy to get the SOAP services to work in the Silverlight control. See the documentation here: http://msdn.microsoft.com/en-us/library/cc879136.aspx That said I don't use the SOAP services at all. I haven't used them in nearly 3 years since the REST services came out. If you want to use the REST services in Silverlight then take a look at this post: http://rbrundritt.wordpress.com/2012/01/06/bing-maps-rest-service-net-libraries/


    http://rbrundritt.wordpress.com


    Monday, October 22, 2012 11:03 AM
  • Hi Many thanks for your help.

    I am getting the following error on using REST from your example.

    public static Response MakeRequest(string requestUrl) { try { HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest; using (HttpWebResponse response = request.GetResponse) { if (response.StatusCode != HttpStatusCode.OK) throw new Exception(String.Format( "Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription)); DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response)); object objResponse = jsonSerializer.ReadObject(response.GetResponseStream()); Response jsonResponse = objResponse as Response; return jsonResponse; } } catch (Exception e) { Console.WriteLine(e.Message); return null; } }

    Error    2    'System.Net.HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.HttpWebRequest' could be found (are you missing a using directive or an assembly reference?)

    If this can be fixed then project should compile.

    Many thanks


    Microsoft SharePoint 2007, Microsoft SQL 2008

    Saturday, October 27, 2012 11:33 AM