User Tools

Site Tools


source_code_geocodersamplemvc_addresslocator_cs.zip

Source Code GeocoderSampleMVC AddressLocator CS.zip

Global.asax.cs

 using System.Web.Http;  
 using System.Web.Mvc;  
 using System.Web.Routing;  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     // Note: For instructions on enabling IIS6 or IIS7 classic mode,  
     // visit http://go.microsoft.com/?LinkId=9394801  
 
     public class MvcApplication : System.Web.HttpApplication  
     {  
         protected void Application_Start()  
         {  
             AreaRegistration.RegisterAllAreas();  
 
             WebApiConfig.Register(GlobalConfiguration.Configuration);  
             RouteConfig.RegisterRoutes(RouteTable.Routes);  
         }  
     }  
 }  
 

RouteConfig.cs

 using System.Web.Mvc;  
 using System.Web.Routing;  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     public class RouteConfig  
     {  
         public static void RegisterRoutes(RouteCollection routes)  
         {  
             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
 
             routes.MapRoute(  
                 name: "Default",  
                 url: "{controller}/{action}/{id}",  
                 defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }  
             );  
         }  
     }  
 }  
 

WebApiConfig.cs

 using System.Web.Http;  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     public static class WebApiConfig  
     {  
         public static void Register(HttpConfiguration config)  
         {  
             config.Routes.MapHttpRoute(  
                 name: "DefaultApi",  
                 routeTemplate: "api/{controller}/{id}",  
                 defaults: new { id = RouteParameter.Optional }  
             );  
         }  
     }  
 }  
 

DefaultController.cs

 using System.Collections.Generic;  
 using System.Collections.ObjectModel;  
 using System.Data;  
 using System.Globalization;  
 using System.Text;  
 using System.Web.Mvc;  
 using System.Web.UI.WebControls;  
 using ThinkGeo.MapSuite.Core;  
 using ThinkGeo.MapSuite.MapSuiteGeocoder;  
 using ThinkGeo.MapSuite.MvcEdition;  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     public class DefaultController : Controller  
     {  
         public ActionResult Index()  
         {  
             Map Map1 = new Map("Map1");  
 
             // Initialize the map.  
             Map1.MapUnit = GeographyUnit.DecimalDegree;  
             Map1.Width = new Unit(100, UnitType.Percentage);  
             Map1.Height = new Unit(100, UnitType.Percentage);  
             Map1.CustomOverlays.Add(new WorldMapKitWmsWebOverlay("WMKOverlay"));  
             Map1.CustomOverlays.Add(new SimpleMarkerOverlay("MarkerOverlay"));  
             Map1.CurrentExtent = new RectangleShape(-88.3330001640625, 42.5966329101563, -86.9157638359375, 41.1629170898438);  
 
             return View(Map1);  
         }  
 
         [MapActionFilter]  
         public ActionResult SearchAddressResult(Map map, GeoCollection<object> args)  
         {  
             if (map == null) return null;  
 
             UsaGeocoder usaGeocoder = new UsaGeocoder(Server.MapPath("~/App_Data/"), MatchMode.ExactMatch);  
             Collection<GeocoderMatch> results;  
             try  
             {  
                 usaGeocoder.Open();  
                 results = usaGeocoder.Match(args[0].ToString());  
             }  
             finally  
             {  
                 usaGeocoder.Close();  
             }  
 
             SimpleMarkerOverlay simpleMarkerOverlay = (SimpleMarkerOverlay)map.CustomOverlays["MarkerOverlay"];  
             simpleMarkerOverlay.Markers.Clear();  
 
             Collection<GeocoderMatch> searchResultItems = new Collection<GeocoderMatch>();  
             MultipointShape resultPoints = new MultipointShape();  
             string searchType = int.Parse(args[1].ToString()) == 0 ? "Street" : string.Empty;  
             foreach (GeocoderMatch matchItem in results)  
             {  
                 if (string.IsNullOrEmpty(searchType) || matchItem.NameValuePairs.ContainsKey(searchType))  
                 {  
                     PointShape point = new PointShape(matchItem.NameValuePairs["CentroidPoint"]);  
                     resultPoints.Points.Add(point);  
                     Marker marker = new Marker(point) { WebImage = new WebImage("/Images/marker.png") };  
 
                     // Get the popup context.  
                     string popupContext = GetPopupContent(matchItem);  
                     CustomPopup popup = new CustomPopup("", point, popupContext);  
                     popup.BorderWidth = 1;  
                     popup.AutoSize = true;  
                     popup.IsVisible = false;  
 
                     marker.Popup = popup;  
                     simpleMarkerOverlay.Markers.Add(marker);  
 
                     searchResultItems.Add(matchItem);  
                 }  
             }  
 
             //Binding data.  
             ViewData["AddressData"] = GetAddressResult(searchResultItems);  
 
             if (resultPoints.Points.Count >= 1)  
             {  
                 ViewData["currentExtent"] = resultPoints.GetBoundingBox();  
             }  
 
             return View("AddressResult");  
         }  
 
         [MapActionFilter]  
         public ActionResult ClearAddressResult(Map map, GeoCollection<object> args)  
         {  
             SimpleMarkerOverlay markerOverlay = (SimpleMarkerOverlay)map.CustomOverlays["MarkerOverlay"];  
             markerOverlay.Markers.Clear();  
 
             ViewData["AddressData"] = GetAddressResult(new Collection<GeocoderMatch> { });  
             return View("AddressResult");  
         }  
 
         private static DataTable GetAddressResult(IList<GeocoderMatch> searchResultItems)  
         {  
             DataTable resultTable = new DataTable();  
             if (searchResultItems.Count > 0)  
             {  
                 foreach (var nameValuePair in searchResultItems[0].NameValuePairs)  
                 {  
                     DataColumn column = new DataColumn() { ColumnName = nameValuePair.Key };  
                     resultTable.Columns.Add(column);  
                 }  
 
                 foreach (var searchResultItem in searchResultItems)  
                 {  
                     DataRow row = resultTable.NewRow();  
                     foreach (var nameValuePair in searchResultItem.NameValuePairs)  
                     {  
                         row[nameValuePair.Key] = nameValuePair.Value;  
                     }  
                     resultTable.Rows.Add(row);  
                 }  
             }  
 
             return resultTable;  
         }  
 
         private static string GetPopupContent(GeocoderMatch matchItem)  
         {  
             string[] streetkeyList = { "Street", "Zip", "State" };  
             StringBuilder keyValuesBuilder = new StringBuilder();  
 
             foreach (string item in streetkeyList)  
             {  
                 keyValuesBuilder.Append(string.Format(CultureInfo.InvariantCulture, "{0} ", matchItem.NameValuePairs[item]));  
             }  
             string headerText = keyValuesBuilder.ToString();  
 
             StringBuilder popupHtml = new StringBuilder("<table>");  
             popupHtml.Append(string.Format(CultureInfo.InvariantCulture, "<tr><td colspan='2' class='popupTitle noBoder'>{0}</td></tr>", headerText));  
             popupHtml.Append("<tr><td colspan='2' class='noBoder'><div class='hrLine'></div></td></tr>");  
 
             int index = 0;  
             foreach (var pair in matchItem.NameValuePairs)  
             {  
                 if (index >= 5)  
                 {  
                     break;  
                 }  
                 popupHtml.Append(string.Format("<tr class='popupText'><td class='noBoder'>{0} : </td><td class='noBoder'>{1}</td></tr>", pair.Key, pair.Value));  
                 index++;  
             }  
             popupHtml.Append("</table>");  
 
             return popupHtml.ToString();  
         }  
     }  
 }  
 

Global.asax.cs

 using System.Web.Http;  
 using System.Web.Mvc;  
 using System.Web.Routing;  
 
 namespace ThinkGeo.MapSuite.GeoCoderExamples  
 {  
     // Note: For instructions on enabling IIS6 or IIS7 classic mode,  
     // visit http://go.microsoft.com/?LinkId=9394801  
 
     public class MvcApplication : System.Web.HttpApplication  
     {  
         protected void Application_Start()  
         {  
             AreaRegistration.RegisterAllAreas();  
 
             WebApiConfig.Register(GlobalConfiguration.Configuration);  
             RouteConfig.RegisterRoutes(RouteTable.Routes);  
         }  
     }  
 }  
 
source_code_geocodersamplemvc_addresslocator_cs.zip.txt · Last modified: 2015/09/09 03:33 by admin