Answered Trying to use Geocoding

  • Monday, July 02, 2012 12:51 PM
     
      Has Code

    Hello everyone.

    I am new to the whole bingMaps and have been doing some work with the Geocoding side of things. I get an error that I can't seem to figure out.

    This is the error "Cannot implicitly convert the type 'LocatingLocalSuppliers.GeocodingService.GeocodLocation' to 'Microsoft.Maps.MapControl.Location"

    Can anyone help me on this.

    Here is the code I am using

    MainPage.xaml

    <UserControl x:Class="LocatingLocalSuppliers.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
        xmlns:bing="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid 
            x:Name="LayoutRoot" 
            Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <bing:Map
                Grid.RowSpan="2"
                x:Name="myMap" CredentialsProvider="MyKey"/>
        </Grid>
    </UserControl>

    MainPage.xaml.ca

    using System;
    using System.Collections.ObjectModel;
    using System.ServiceModel;
    using System.Windows;
    using System.Windows.Browser;
    using System.Windows.Controls;
    using System.Windows.Input;
    using Microsoft.Maps.MapControl;
    using Microsoft.Maps.MapControl.PlatformServices;
    using Microsoft.Maps.MapControl.Core;
    
    namespace LocatingLocalSuppliers
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent(); 
                string Address = "759 Jantanagar, Chandkheda, Gandhinagar, Gujarat, 382424";
                GeocodingService.GeocodeServiceClient myGeoCodeClient = new GeocodingService.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");          
                // create an event hanler for GeocodeCompleted event as the service would be call asynchronously
                myGeoCodeClient.GeocodeCompleted += new EventHandler<LocatingLocalSuppliers.GeocodingService.GeocodeCompletedEventArgs>(myGeoCodeClient_GeocodeCompleted);
                // create the request and pass the address to it
                GeocodingService.GeocodeRequest myGeoCodeRequest = new GeocodingService.GeocodeRequest();
                myGeoCodeRequest.Credentials = new GeocodingService.Credentials();
                myGeoCodeRequest.Credentials.ApplicationId=((ApplicationIdCredentialsProvider)myMap.CredentialsProvider).ApplicationId;
                myGeoCodeRequest.Query = Address;
    
                // Pass the request to GeocodeAsyn method
                myGeoCodeClient.GeocodeAsync(myGeoCodeRequest);
    
            }
    
            void myGeoCodeClient_GeocodeCompleted(object sender, LocatingLocalSuppliers.GeocodingService.GeocodeCompletedEventArgs e)
            {
                // create a map layer
                MapLayer myMapLayer = new MapLayer();
                myMap.Children.Add(myMapLayer);
    
                // create a location collection class
                LocationCollection myLocationColl = new LocationCollection();
    
                foreach (GeocodingService.GeocodeResult gr in e.Result.Results)
                {
                    Pushpin myPushPin = new Pushpin();
                    // set it to first found location
                    myPushPin.Location = gr.Locations[0];
                    // add it to location collection
                    // which would be used to set the map's bound
                    myLocationColl.Add(myPushPin.Location);
                    // Add the drawn point to the route layer.                   
                    myMapLayer.Children.Add(myPushPin);
                }
                var bounds = new LocationRect(myLocationColl);
                myMap.SetView(bounds);
            }
        }
    }

    Thanks all

All Replies

  • Monday, July 02, 2012 1:26 PM
    Owner
     
     
    Don't use the Bign MAps SOAP services. They are old and slow. Use the Bing Maps REST services. Information on how to use the REST services in .NET take a look at this blog post: http://rbrundritt.wordpress.com/2012/01/06/bing-maps-rest-service-net-libraries/

    http://rbrundritt.wordpress.com

  • Monday, July 02, 2012 1:37 PM
    Moderator
     
     Answered Has Code

    In your use case, you are binding the Location returned by your service (from the service reference you added, you got client proxy classes) to the Location inside your control.

    The error said that you're not using the right type of location, so basicaly, what you can easily do is to change the line where you're binding the location.

    Pushpin myPushPin = new Pushpin();
                    // set it to first found location
                    myPushPin.Location = new Microsoft.Maps.WPF.Location(gr.Locations[0].Latitude, gr.Locations[0].Longitude);
                    // add it to location collection
                    // which would be used to set the map's bound
                    myLocationColl.Add(myPushPin.Location);
                    // Add the drawn point to the route layer.                   
                    myMapLayer.Children.Add(myPushPin);
    


    MVP - Bing Maps - My blog (FR): http://blogs.developpeur.org/nicoboo/ Twitter: http://twitter.com/nicolasboonaert/

    • Marked As Answer by Texeco Monday, July 02, 2012 2:29 PM
    •  
  • Monday, July 02, 2012 2:30 PM
     
     

    That worked thanks. Can you breifly explain why I need to do that. So I can better understand it so I don't make the same mistake.

    Thanks

  • Monday, July 02, 2012 4:42 PM
    Owner
     
     
    The two different Location objects are from different Namespaces. The SOAP service is unaware of the Location class that is in WPF control and vice versa. The WPF control may have some internal methods to it that are not available in the Location object from the SOAP service. Thus, only Location objects from the WPF control can be used with the WPF map control.

    http://rbrundritt.wordpress.com

  • Monday, July 02, 2012 5:23 PM
     
     
    Cool Thanks for that