tonytonglx

  1. First, add a service reference to the Bing Maps Geocode Service. In the Solution Explorer in Visual Studio, right click on the SilverlightApplication1 project and select Add Service Reference. Make sure you are adding the service references to the Silverlight project and not the Web project.

  2. Enter http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc into the Address field and click Go.

  3. Type GeocodeService into the Namespace field and click OK.

  4. Repeat steps 1-3 using http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc as the Address and RouteService as the Namespace.

 

 

 

private GeocodeService.GeocodeResult[] geocodeResults = new GeocodeService.GeocodeResult[2];
        private void Geocode(string strAddress,int waypointIndex)
        {
            //GeocodeService
            GeocodeService.GeocodeServiceClient geocodeService = new GeocodeService.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
            geocodeService.GeocodeCompleted += new EventHandler<GeocodeService.GeocodeCompletedEventArgs>(geocodeService_GeocodeCompleted);

            GeocodeService.GeocodeRequest geocodeRequest = new GeocodeService.GeocodeRequest();
            geocodeRequest.Credentials = new Microsoft.Phone.Controls.Maps.Credentials();
            geocodeRequest.Credentials.ApplicationId = ((ApplicationIdCredentialsProvider)this.map1.CredentialsProvider).ApplicationId;
            geocodeRequest.Query = strAddress;

            //async invoke.
            geocodeService.GeocodeAsync(geocodeRequest, waypointIndex);
        }


    
        void geocodeService_GeocodeCompleted(object sender, GeocodeService.GeocodeCompletedEventArgs e)
        {
            int waypointIndex = System.Convert.ToInt32(e.UserState);

            geocodeResults[waypointIndex] = e.Result.Results[0];
            System.Diagnostics.Debug.WriteLine("Lat= " + e.Result.Results[0].Locations[0].Latitude + ",Long= " + e.Result.Results[0].Locations[0].Longitude);

            bool doneGeocoding = true;

            foreach (GeocodeService.GeocodeResult gr in geocodeResults)
            {
                if (gr == null)
                {
                    doneGeocoding = false;
                }
            }          
            if (doneGeocoding)
            {
                CalculateRoute(geocodeResults);
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Geocode("dalian",0);
            Geocode("shenyang", 1);
          
        }

        private void CalculateRoute(GeocodeService .GeocodeResult [] geocodeResultList)
        {
            RouteService.RouteServiceClient routeService = new RouteService.RouteServiceClient("BasicHttpBinding_IRouteService");
            routeService.CalculateRouteCompleted += new EventHandler<RouteService.CalculateRouteCompletedEventArgs>(routeService_CalculateRouteCompleted);

            RouteService.RouteRequest routeRequest = new RouteService.RouteRequest();
            routeRequest.Credentials = new Credentials();
            routeRequest.Credentials.ApplicationId = ((ApplicationIdCredentialsProvider)this.map1.CredentialsProvider).ApplicationId;

            routeRequest.Options = new RouteService.RouteOptions();
            routeRequest.Options.RoutePathType = RouteService.RoutePathType.Points;

            routeRequest.Waypoints = new System.Collections.ObjectModel.ObservableCollection<RouteService.Waypoint>();

            foreach (GeocodeService.GeocodeResult result in geocodeResultList)
            {
                routeRequest.Waypoints.Add(GeocodeResultToWaypoint(result));
            }

            routeService.CalculateRouteAsync(routeRequest);

        }

        private RouteService.Waypoint GeocodeResultToWaypoint(GeocodeService.GeocodeResult result)
        {
              RouteService.Waypoint waypoint = new RouteService.Waypoint();
              waypoint.Description = result.DisplayName;
              waypoint.Location = new Microsoft.Phone.Controls.Maps.Platform.Location();
              waypoint.Location.Latitude = result.Locations[0].Latitude;
              waypoint.Location.Longitude = result.Locations[0].Longitude;
              return waypoint;
        }

        List<GeoCoordinate> list = new List<GeoCoordinate>();
        void routeService_CalculateRouteCompleted(object sender, RouteService.CalculateRouteCompletedEventArgs e)
        {
                if((e.Result .ResponseSummary .StatusCode ==  RouteService.ResponseStatusCode.Success ) & (e.Result .Result.Legs .Count != 0))
                {
                    Color routeColor = Colors.Blue ;
                    SolidColorBrush routeBrush = new SolidColorBrush (routeColor );
                    MapPolyline routeLine = new MapPolyline();
                    routeLine.Locations = new LocationCollection();
                    routeLine.Stroke = routeBrush;
                    routeLine.Opacity = 0.65;
                    routeLine.StrokeThickness = 5.0;

                    foreach (Location p in e.Result .Result .RoutePath .Points ){

                        GeoCoordinate geo = new GeoCoordinate
                        {
                            Longitude = p.Longitude,
                            Latitude = p.Latitude
                        };

                            routeLine .Locations .Add (geo);
                            list.Add (geo);
                     }

                    MapLayer myRouteLayer = new MapLayer();
                     myRouteLayer .Children .Add (routeLine );
                    this.map1.Children.Add(myRouteLayer);
                  
                    //set the map view.
                    LocationRect rect = LocationRect.CreateLocationRect(list);

                    this.map1.SetView(rect);

                   
                   
                }
        }

分类:

技术点:

相关文章: