Using Bing Maps SOAP Services API in Windows Phone Applications

1067 views 0 comments posted at about 7 years ago Raymond Tang Fahao

In my previous article, I demonstrated how to use Bing Maps Control in Windows Phone applications; in this article, I will show several other demos about Bing Maps SOAP Services. Please refer to this link for more details about Bing Maps SOAP Services API.

Bing Maps SOAP Services

There are four kinds of services provided by Bing Maps SOAP Services:

I will demonstrate all of these services in the following walkthrough. The project will be based on the previous project I used in Using Bing Maps Control in Windows Phone Applications.

Geocode Service

Geocode service allows you to match an address or place to a map location/geo coordinate (geocoding) or return geographic entities or addresses found at a given location (reverse geocoding).  The URI of this service is http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc. This service contains two main methods: Geocode and ReverseGeocode.

Let’s first service reference to it in Visual Studio: Project BingMapsExample –> Right Click –> Add Service Reference –> Change Namespace as BingMapsGeocodeService.

Bing Maps SOAP Services in Windows Phone Screenshot - 1

Add the below codes to invoke this service:

     private void MapMainLoaded(object sender, RoutedEventArgs e)
        {
            GeocodeServiceTest();
        }

        private void GeocodeServiceTest()
        {
            var geocodeRequest = new GeocodeRequest
             {
                 Options = new GeocodeOptions
                 {
                     Filters = new ObservableCollection<FilterBase>{
                        new ConfidenceFilter {MinimumConfidence = Confidence.High}
                                                                     }
                 }
             };
            //initialize client
            var serviceClient = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
            geocodeRequest.Query = "1 Microsoft Way, Redmond, WA";
            serviceClient.GeocodeCompleted +=
                (o, args) =>
                {
                    //if no error, display all the results in the map
                    if (args.Error != null) return;
                    var response = args.Result;
                    var results = from result in response.Results
                                  select new Pushpin
                                             {
                                                 Content = result.DisplayName,
                                                 Background = (Brush)Application.Current.Resources["PhoneAccentBrush"],
                                                 Location = result.Locations[0]
                                             };
                    var coordinates = new LocationCollection();
                    foreach (var pushpin in results)
                    {
                        mapMain.Children.Add(pushpin);
                        coordinates.Add(pushpin.Location);
                    }
                    if (coordinates.Count > 1)
                    {
                        mapMain.SetView(LocationRect.CreateLocationRect(coordinates));
                    }
                    else if (coordinates.Count ==1)
                    {
                        mapMain.Center = coordinates[0];
                        mapMain.ZoomLevel = 17;
                    }
                };

            //Set map control's credentials to this request and begin to request
            mapMain.CredentialsProvider.GetCredentials(credentials =>
            {
                geocodeRequest.Credentials = credentials;
                serviceClient.GeocodeAsync(geocodeRequest);
            });
        }

Below is the screenshot for the query of "1 Microsoft Way, Redmond, WA" and “651 North 34th St # 101, Seattle, WA” :

Bing Maps SOAP Services in Windows Phone Screenshot - 2Bing Maps SOAP Services in Windows Phone Screenshot - 3

Imaginary Service

Imaginary service allows you to retrieve map imaginary information including map URI; with these info, you can display them in your own controls. There are two main methods : GetImageryMetadata and GetMapUri.The URI of this service is http://dev.virtualearth.net/webservices/v1/imageryservice/imageryservice.svc.

As the above case, we will add service reference first with namespace BingMapsImaginaryService.

Below are the sample code to invoke this service and return the URI of the map image of Microsoft Way:

private void ImaginaryServiceTest()
        {
            var location = new Location { Latitude = 47.6396, Longitude = -122.1306 };
            var request = new MapUriRequest
            {
                Options = new MapUriOptions
                {
                    ImageType = ImageType.Png,
                    Style = MapStyle.Aerial,
                    ImageSize =
                        new BingMapsImaginaryService.SizeOfint { Width = 450, Height = 600 },
                },
                Pushpins = new ObservableCollection<Pushpin> { new Pushpin { Location = location} },
                Center = location
            };

            var client = new ImageryServiceClient("BasicHttpBinding_IImageryService");
            client.GetMapUriCompleted +=
                (o, args) =>
                {
                    if (args.Error != null) return;
                    var image = new Image
                                    {
                                        Source =
                                            (ImageSource)
                                            new ImageSourceConverter().ConvertFromString(args.Result.Uri)
                                    };
                    mapMain.Children.Add(image);
                };
            mapMain.CredentialsProvider.GetCredentials(credential =>
            {
                request.Credentials = credential;
                client.GetMapUriAsync(request);
            });
        }

Below is the screenshot of the result:

Bing Maps SOAP Services in Windows Phone Screenshot - 4

Route Service

Route service allows you to calculate route directions to and from multiple locations on a map. The URI of this service is http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc. It contains two methods CalculateRoute and CalculateRoutesFromMajorRoads.

As we did just now, add service reference with namespace BingMapsRouteService.

In the below sample, I will demonstrate how to use this service to draw a poly line from "1 Microsoft Way, Redmond, WA"(47.6381818, -122.1314904) to “651 North 34th St # 101, Seattle, WA” (47.6486964, -122.3503014).

private void RouteServiceTest()
        {
            var request = new RouteRequest
            {
                Options = new RouteOptions { RoutePathType = RoutePathType.Points },
                ExecutionOptions = new ExecutionOptions{ SuppressFaults = true}
            };
            var microsoft = new Location { Latitude = 47.6381818, Longitude = -122.1314904 };
            var google = new Location { Latitude = 47.6486964, Longitude = -122.3503014 };
            request.Waypoints = new ObservableCollection<Waypoint>
                                  {new Waypoint{ Location = microsoft},
                                   new Waypoint {Location = google}
                                  };
            var client = new RouteServiceClient("BasicHttpBinding_IRouteService");
            client.CalculateRouteCompleted +=
                (o, args) =>
                {
                    if (args.Error == null)
                    {
                        var line = new MapPolyline
                        {
                            Locations = new LocationCollection(),
                            Stroke = (Brush)Application.Current.Resources["PhoneAccentBrush"],
                            StrokeThickness = 4

                        };
                        foreach (var point in args.Result.Result.RoutePath.Points)
                        {
                            line.Locations.Add(point);
                        }
                        mapMain.Children.Add(line);
                        mapMain.SetView(LocationRect.CreateLocationRect(line.Locations));
                        //mapMain.ZoomLevel = 2;
                    }
                };
            var pushpinMicrosft = new Microsoft.Phone.Controls.Maps.Pushpin
            {
                Content = "Microsoft",
                Location = microsoft,
                Background = (Brush)Application.Current.Resources["PhoneAccentBrush"]
            };
            var pushpinGoogle = new Microsoft.Phone.Controls.Maps.Pushpin
            {
                Content = "Google",
                Location = google,
                Background = (Brush)Application.Current.Resources["PhoneAccentBrush"]
            };
            mapMain.Children.Add(pushpinMicrosft);
            mapMain.Children.Add(pushpinGoogle);
            mapMain.CredentialsProvider.GetCredentials(credentials =>
                                                           {
                                                               request.Credentials = credentials;
                                                               client.CalculateRouteAsync(request);
                                                           });
        }

Below is the screenshot of Route Service:

Bing Maps SOAP Services in Windows Phone Screenshot - 5

Search Service

Search service allows you to parse a search query as well as return search results. It has only one method: Search. The Uri is http://dev.virtualearth.net/webservices/v1/searchservice/searchservice.svc.

As the way to use this service is the same as the previous samples in this article, I won’t demonstrate any sample. We can use this service to search the restaurants, cinemas, shopping malls around a location.  You can refer to http://msdn.microsoft.com/en-us/library/cc980960.aspx.

Add comment

Comments (0)

No comments yet.
In this Page