Ejemplo n.º 1
0
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            string address = txtAddressName.Text.Trim();

            if (string.IsNullOrEmpty(address))
            {
                MessageBox.Show("Please enter address.");
                return;
            }

            // Initialize geocoder
            Geocoder streetGeocoder = new Geocoder();

            streetGeocoder.MatchingPlugIns.Add(new StreetMatchingPlugin(@"..\..\Data\GeoCoderIndex", MatchMode.ExactMatch));
            try
            {
                streetGeocoder.Open();

                // Get the ShapeFileFeatureLayer which includes the street data.
                FeatureLayer roadFeatureLayer = ((LayerOverlay)Map1.Overlays[roadOverlayName]).Layers[0] as FeatureLayer;
                roadFeatureLayer.Open();

                // Get the marker overlay to display the searched results.
                SimpleMarkerOverlay markerOverlay = Map1.Overlays[markerOverlayName] as SimpleMarkerOverlay;
                markerOverlay.Markers.Clear();

                Collection <SearchResult> searchedResult = new Collection <SearchResult>();
                // Geocoder the address input, such as "6701 PECAN".
                Collection <GeocoderMatch> matchedRoads = streetGeocoder.Match(address);

                Proj4Projection projection = new Proj4Projection();
                projection.InternalProjectionParametersString = Proj4Projection.GetWgs84ParametersString();
                projection.ExternalProjectionParametersString = "+proj=lcc +lat_1=32.1333333333333 +lat_2=33.9666666666667 +lat_0=31.6666666666667 +lon_0=-98.5 +x_0=600000 +y_0=2000000 +ellps=GRS80 +datum=NAD83 +units=us-ft +no_defs";
                projection.Open();

                foreach (GeocoderMatch geocoderMatched in matchedRoads)
                {
                    // Display the searched result as marker.
                    BaseShape  streetCenter = new Feature(geocoderMatched.NameValuePairs["CentroidPoint"]).GetShape();
                    PointShape center       = projection.ConvertToExternalProjection(streetCenter) as PointShape;
                    markerOverlay.Markers.Add(new Marker(center)
                    {
                        ImageSource = new BitmapImage(new Uri("/Resources/AQUA.png", UriKind.RelativeOrAbsolute)),
                        Width       = 20,
                        Height      = 34,
                        YOffset     = -17,
                        ToolTip     = geocoderMatched.NameValuePairs["Street"]
                    });

                    // Get the feature id of the street where the searched result locates.
                    string featureId = geocoderMatched.NameValuePairs["UID"];

                    // Get the street where the searched result locates.
                    Feature feature = roadFeatureLayer.QueryTools.GetFeatureById(featureId, new[] { "ROAD_NAME" });
                    searchedResult.Add(GetSearchResult(feature, geocoderMatched));
                }

                projection.Close();
                roadFeatureLayer.Close();
                searchResults.ItemsSource = searchedResult;

                // Set the highlight layer into the current extent.
                InMemoryFeatureLayer highlightRoadLayer = ((LayerOverlay)Map1.Overlays[roadHighlightOverlayName]).Layers[0] as InMemoryFeatureLayer;
                if (highlightRoadLayer.InternalFeatures.Count > 0)
                {
                    Map1.CurrentExtent = highlightRoadLayer.GetBoundingBox();
                    Map1.ZoomOut();
                }

                Map1.Refresh();
            }
            finally
            {
                streetGeocoder.Close();
            }
        }