This is a migrated thread and some comments may be shown as answers.

can't get GeocodeAsync() to work

5 Answers 175 Views
Map
This is a migrated thread and some comments may be shown as answers.
Damian Slee
Top achievements
Rank 1
Damian Slee asked on 22 Mar 2010, 03:08 AM
I get a silverlight application error when trying to use the geocodeasync.   I could only find an example of the reverseGeocode usage, so i may be configuring the request wrong?

this is the application exception i get

Route Service Exception: An exception occurred during the operation, making the result invalid.  Check InnerException for exception details.

   at Telerik.Windows.Controls.Map.BingGeocodeProvider.GeocodeServiceGeocodeCompleted(Object sender, GeocodeCompletedEventArgs e)
   at Telerik.Windows.Controls.Map.BingGeocodeService.GeocodeServiceClient.OnGeocodeCompleted(Object state)



Sample code;

        private void GeocodeAddress()
        {
            BingGeocodeProvider _geocodeProvider = new BingGeocodeProvider();
           _geocodeProvider.MapControl = this.MainMap;
            _geocodeProvider.ApplicationId = _bingMapApplicationId;
            _geocodeProvider.GeocodeCompleted += Provider_GeocodeCompleted;

            GeocodeRequest geocodeRequest = new GeocodeRequest();
            geocodeRequest.Address = new Address();
            geocodeRequest.Address.FormattedAddress = "1 rodeo drive beverly hills";
            _geocodeProvider.GeocodeAsync(geocodeRequest);
        }

        void Provider_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
        {
            var response = e.Response;
            var result = response.Results[0];
            var location = result.Locations[0];
        }



5 Answers, 1 is accepted

Sort by
0
Damian Slee
Top achievements
Rank 1
answered on 22 Mar 2010, 03:18 AM

by post code worked, returned 90210, CA   and 90210, Finland. 

Is there anyway to allow geocode a free form entered address?

geocodeRequest.Address.PostalCode = "90210";

0
Damian Slee
Top achievements
Rank 1
answered on 22 Mar 2010, 05:39 AM
I have had some more success by having the country set
geocodeRequest.Address.CountryRegion = "USA";

The geocoding results also seems to be relative to where the map is positioned.  Is it possible to turn this off?

The problem i had earlier, i was viewing sydney, then did a geocode request to "1 rodeo drive beverly hills"

0
Andrey
Telerik team
answered on 24 Mar 2010, 04:25 PM

Hello Damian,

There must be some misunderstanding here -- geocoding is not relative to the map position in any way. To geocode a free form entered address you have to use the Query property of the GeocodeRequest object.
The Address property is used in cases where you would like to specify particular address properties.

Please, take a look into the code below:

<UserControl x:Class="Telerik.Silverlight.RadMap.MainPage"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Telerik.Silverlight.RadMap"
             xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
             xmlns:map="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.DataVisualization"
             xmlns:layer="clr-namespace:Telerik.Windows.Controls.Map;assembly=Telerik.Windows.Controls.DataVisualization"
             xmlns:grid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
             Width="800"
             Height="600">
    <Grid x:Name="LayoutRoot">
        <Grid.Resources>
            <DataTemplate x:Key="RingTemplate">
                <Grid Width="14" Height="14">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="14" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <layer:MapLayer.HotSpot>
                        <layer:HotSpot X="0.5" Y="0.5" />
                    </layer:MapLayer.HotSpot>
                    <Path Fill="Yellow">
                        <Path.Data>
                            <GeometryGroup>
                                <EllipseGeometry Center="7,7" RadiusX="3" RadiusY="3" />
                                <EllipseGeometry Center="7,7" RadiusX="7" RadiusY="7" />
                            </GeometryGroup>
                        </Path.Data>
                    </Path>
                </Grid>
            </DataTemplate>
        </Grid.Resources>
          
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="600" />
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
  
        <map:RadMap x:Name="radMap" 
                    Center="37.7040701207995,-121.882780875908"
                    ZoomLevel="10">
            <layer:InformationLayer Name="informationLayer" ItemTemplate="{StaticResource RingTemplate}">
            </layer:InformationLayer>
        </map:RadMap>
          
        <StackPanel Grid.Column="1">
            <TextBox Name="txtLocation" />
            <Button Content="Geocode" Click="GeocodeHandler" />
            <grid:RadGridView Name="gvAddresses"
                              AutoGenerateColumns="False" 
                              DataLoadMode="Asynchronous" 
                              EnableRowVirtualization="True"
                              RowIndicatorVisibility="Collapsed"
                              ShowGroupPanel="False"
                              SelectionMode="Single"
                              SelectionChanged="AddressSelected">
                <grid:RadGridView.Columns>
                    <grid:GridViewToggleRowDetailsColumn />
                    <grid:GridViewSelectColumn />
                    <grid:GridViewDataColumn DataMemberBinding="{Binding Path=Address.FormattedAddress}" 
                                             Header="Formatted adress" 
                                             IsReadOnly="True"
                                             IsSortable="False"
                                             IsReorderable="False"/>
                </grid:RadGridView.Columns>
                <grid:RadGridView.RowDetailsTemplate>
                    <DataTemplate>
                        <Grid>
                            <grid:RadGridView Name="gvLocations"
                                              AutoGenerateColumns="False" 
                                              DataLoadMode="Asynchronous" 
                                              EnableRowVirtualization="True"
                                              RowIndicatorVisibility="Collapsed"
                                              ShowGroupPanel="False"
                                              ItemsSource="{Binding Path=Locations}"
                                              SelectionChanged="LocationSelected">
                                <grid:RadGridView.Columns>
                                    <grid:GridViewSelectColumn />
                                    <grid:GridViewDataColumn DataMemberBinding="{Binding Path=Latitude}" 
                                                             Header="Latitude" 
                                                             IsReadOnly="True"
                                                             IsSortable="False"
                                                             IsReorderable="False"/>
                                    <grid:GridViewDataColumn DataMemberBinding="{Binding Path=Longitude}" 
                                                             Header="Longitude" 
                                                             IsReadOnly="True"
                                                             IsSortable="False"
                                                             IsReorderable="False"/>
                                </grid:RadGridView.Columns>
                            </grid:RadGridView>
                        </Grid>
                    </DataTemplate>
                </grid:RadGridView.RowDetailsTemplate>
            </grid:RadGridView>
        </StackPanel>
    </Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Resources;
using System.Windows.Data;
using Telerik.Windows.Controls.Map;
  
namespace Telerik.Silverlight.RadMap
{
    public partial class MainPage : UserControl
    {
        private BingGeocodeProvider geocodeProvider;
        private string applicationId = "YOUR Bing ApplicationID";
  
        public MainPage()
        {
            InitializeComponent();
  
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
  
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            BingMapProvider provider = new BingMapProvider(MapMode.Aerial, true, this.applicationId);
            this.radMap.Provider = provider;
  
            // Init geocode provider.
            geocodeProvider = new BingGeocodeProvider()
            {
                ApplicationId = this.applicationId,
                MapControl = this.radMap
            };
            geocodeProvider.GeocodeCompleted += new EventHandler<GeocodeCompletedEventArgs>(Provider_GeocodeCompleted);
        }
  
        private void GeocodeHandler(object sender, RoutedEventArgs e)
        {
            GeocodeRequest geocodeRequest = new GeocodeRequest()
            {
                Query = txtLocation.Text
            };
            this.geocodeProvider.GeocodeAsync(geocodeRequest);
        }
  
        private void Provider_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
        {
            GeocodeResponse response = e.Response;
            this.gvAddresses.ItemsSource = response.Results;
        }
  
        private void AddressSelected(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                GeocodeResult result = e.AddedItems[0] as GeocodeResult;
                if (result != null)
                {
                    if (result.Locations.Count > 0)
                    {
                        this.radMap.Center = result.Locations[0];
                    }
                    this.informationLayer.ItemsSource = result.Locations;
                }
            }
        }
  
        private void LocationSelected(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                Location location = (Location)e.AddedItems[0];
                this.informationLayer.ItemsSource = new Location[] { location };
            }
        }
    }
}

It uses free form entered address to geocode and then shows the results in the RadGridView.
You can expand grid view row to see locations associated with the address. If you select address in the grid view, then the corresponding locations are shown.

Kind regards,
Andrey Murzov
the Telerik team


Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Damian Slee
Top achievements
Rank 1
answered on 25 Mar 2010, 01:30 AM
I think the geocode using Address object is relative to the map position.  Haven't tried query object yet.

if I for example have the country set to "USA" and the AddressLine set to "Wall St".  If i am zoomed in on australia, i get one set of results; if i zoom the map out to the whole world, i get different results, including Wall St New York.
0
Andrey
Telerik team
answered on 26 Mar 2010, 04:12 PM
Hi Damian,

We do not do anything special to bind geocoding to the map location. I've investigated deeper into this problem and I found, that Bing geocoding service does this by itself. It looks like it narrows geocoding results to the current map location. It seems like they bind imagery and geocoding services using Bing application ID, because geocoding request doesn't contain any information about map location.

Kind regards,
Andrey Murzov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Map
Asked by
Damian Slee
Top achievements
Rank 1
Answers by
Damian Slee
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or