Bing-Map-API我做错了什么?

时间:2014-12-07 13:26:03

标签: c# wpf bing-maps

我正在尝试让我的学校使用Bing Map API并使用GeocodeAdress。我构建了这个应用程序:http://msdn.microsoft.com/en-us/library/dd221354.aspx,问题是我每次都会收到此错误。

它位于第62行:此方法:GeocodeServiceClient geocodeService = new GeocodeServiceClient();

!InvalidOperationException未处理 System.ServiceModel.dll中发生未处理的“System.InvalidOperationException”类型异常

其他信息:无法加载合同“GeocodeService.IGeocodeService”的端点配置部分,因为找到了该合同的多个端点配置。请按名称指明首选端点配置部分。

以下是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using BingMapsSample.GeocodeService;
using BingMapsSample.SearchService;
using BingMapsSample.ImageryService;
using BingMapsSample.RouteService;

namespace BingMapsSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
    }

    private String GeocodeAddress(string address)
    {
        string results = "";
        string key = "Validate Bing Map Education Code";
        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();
        GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);

        if (geocodeResponse.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 string ReverseGeocodePoint(string locationString)
    {
        string results = "";
        string key = "Validate Bing Map Education Code";
        ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();

        // Set the credentials using a valid Bing Maps key
        reverseGeocodeRequest.Credentials = new GeocodeService.Credentials();
        reverseGeocodeRequest.Credentials.ApplicationId = key;

        // Set the point to use to find a matching address
        GeocodeService.Location point = new GeocodeService.Location();
        string[] digits = locationString.Split(',');

        point.Latitude = double.Parse(digits[0].Trim());
        point.Longitude = double.Parse(digits[1].Trim());

        reverseGeocodeRequest.Location = point;

        // Make the reverse geocode request
        GeocodeServiceClient geocodeService = new GeocodeServiceClient();
        GeocodeResponse geocodeResponse = geocodeService.ReverseGeocode(reverseGeocodeRequest);

        if (geocodeResponse.Results.Length > 0)
            results = geocodeResponse.Results[0].DisplayName;
        else
            results = "No Results found";

        return results;
    }

    private string SearchKeywordLocation(string keywordLocation)
    {
        String results = "";
        String key = "Validate Bing Map Education Code";
        SearchRequest searchRequest = new SearchRequest();

        // Set the credentials using a valid Bing Maps key
        searchRequest.Credentials = new SearchService.Credentials();
        searchRequest.Credentials.ApplicationId = key;

        //Create the search query
        StructuredSearchQuery ssQuery = new StructuredSearchQuery();
        string[] parts = keywordLocation.Split(';');
        ssQuery.Keyword = parts[0];
        ssQuery.Location = parts[1];
        searchRequest.StructuredQuery = ssQuery;

        //Define options on the search
        searchRequest.SearchOptions = new SearchOptions();
        searchRequest.SearchOptions.Filters =
            new FilterExpression()
            {
                PropertyId = 3,
                CompareOperator = CompareOperator.GreaterThanOrEquals,
                FilterValue = 8.16
            };

        //Make the search request 
        SearchServiceClient searchService = new SearchServiceClient();
        SearchResponse searchResponse = searchService.Search(searchRequest);

        //Parse and format results
        if (searchResponse.ResultSets[0].Results.Length > 0)
        {
            StringBuilder resultList = new StringBuilder("");
            for (int i = 0; i < searchResponse.ResultSets[0].Results.Length; i++)
            {
                resultList.Append(String.Format("{0}. {1}\n", i + 1,
                    searchResponse.ResultSets[0].Results[i].Name));
            }

            results = resultList.ToString();
        }
        else
            results = "No results found";

        return results;
    }



    private void Geocode_Click(object sender, RoutedEventArgs e)
    {
        labelResults.Content = GeocodeAddress(textInput.Text);
    }

    private void ReverseGeocode_Click(object sender, RoutedEventArgs e)
    {
        labelResults.Content = ReverseGeocodePoint(textInput.Text);
    }

    private void Search_Click(object sender, RoutedEventArgs e)
    {
        labelResults.Content = SearchKeywordLocation(textInput.Text);
    }

}
}

我发现解决方案62应该是:

var geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");

1 个答案:

答案 0 :(得分:0)

打开web.config文件或app.config文件。在那里,您将看到该服务的基本和自定义绑定配置。删除自定义绑定部分。清理并构建您的项目,它应该可以工作。

作为旁注。 Bing Maps SOAP服务非常陈旧,功能有限。他们大约8年前被释放。大约4年前发布了一个较新的基于REST的服务。 REST速度更快,响应包更小,功能更多。您可以在此处找到有关Bing Maps REST服务的更多信息:http://msdn.microsoft.com/en-us/library/ff701713.aspx