The first script uses Google's gears object (used in Chrome, for example) which stores the browser's geolocation information. The second script is an implementation that looks for different device types and handles the "quirks". The inline third script simply implements all the above and populates two hidden fields with the latitude and longitude. Since I am using the Artem.GoogleMap server control in this case, we have to trigger a postback to get the control to render a map.
Now what if none of this works? Let's have a look at the ASP.NET page codebehind:
protected void Page_Load(object sender, EventArgs e) { if (!String.IsNullOrEmpty(Lat.Value) && !String.IsNullOrEmpty(Lng.Value)) { double latitude = double.Parse(Lat.Value); double longitude = double.Parse(Lng.Value); var request = new GeoRequest(latitude, longitude); var response = request.GetResponse(); if (response.Status == GeoStatus.OK) { FormattedAdr.Text = response.Results[0].FormattedAddress; map1.Address = FormattedAdr.Text; } }
You can see above that if the hidden fields are not empty, we issue a GoogleGeocoding request and map the coordinates. And, if that fails, I use the free MaxMind Geolocation javascript API, but I do this server-side as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net;
namespace GEOIP { public static class GeoLocation {
public static Location GetLocation() { try { WebClient wc = new WebClient(); string s = wc.DownloadString(" http://j.maxmind.com/app/geoip.js"); wc.Dispose(); /* SAMPLE function geoip_country_code() { return 'US'; } function geoip_country_name() { return 'United States'; } function geoip_city() { return 'Deland'; } function geoip_region() { return 'FL'; } function geoip_region_name() { return 'Florida'; } function geoip_latitude() { return '29.0575'; } function geoip_longitude() { return '-81.3344'; } function geoip_postal_code() { return ''; } function geoip_area_code() { return '386'; } function geoip_metro_code() { return '534'; } */
s = s.Replace("function geoip_country_code() { return '", "" ); s = s.Replace("function geoip_country_name() { return '", "" ); s = s.Replace("function geoip_city() { return '", "" ); s = s.Replace("function geoip_region() { return '", "" ); s = s.Replace("function geoip_region_name() { return '", "" ); s = s.Replace("function geoip_latitude() { return '", "" ); s = s.Replace("function geoip_longitude() { return '", "" ); s = s.Replace("function geoip_postal_code() { return '", "" ); s = s.Replace("function geoip_area_code() { return '", "" ); s = s.Replace("function geoip_metro_code() { return '", "" ); s = s.Replace("'; }", ""); string[] s2 = s.Split(" ".ToCharArray());
public class Location { public string CountryCode { get; set; } public string CountryName { get; set; } public string City { get; set; } public string Region { get; set; } public string RegionName { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } public string PostalCode { get; set; } public string AreaCode { get; set; } public string MetroCode { get; set; }
}
The result is that we can get a map for virtually any browser or device.
Pick or make your picture. Open it in IrfanView Image>Resize to 32X32 Save image as ico file type upload it to the root directory of your website Add it to your web.config as public if you have security enabled Modify <head> with <link href="/favicon.ico" rel="icon" type="image/x-icon" />
Right click project>Add New Solution folder (sf) Use these folders to separate Assemblies, Client and Service sections
*Client*(sf) contains -Client(f) (References to all Client projects) ~App.xaml initializes IoC and checks silverlight installation (plus has source for extending view xaml) ~Web service references created in the Service Section (ServiceReferences.ClientConfig) -Data(f) (References to none) ~Objects -Common(f) (References to Data) ~Views(f) Interfaces (used to bridge to View project) ~Models(f) Interfaces (used to bridge to Model project) ~ViewModels(f) Interfaces (used to bridge to ViewModel project)
~RelayCommand class (xaml buttons bind to these) ~IoC class ~WebContext class
-View(f) (References to Common, Control, Data) ~XAML ~Load into IoC (Uses MEF to load the ViewModel)
-ViewModel(f) (References to Common, Data) ~Observable(f) (Objects used in the View) ~Wires up RelayCommand ~Instantiates objects that do the heavy lifting
-Model(f) (References to Common, Data) ~Web service references created in the Service Section (ServiceReferences.ClientConfig) ~BaseModel.cs with MEF Export
-Controls(f) (References to Common, Data) ~Menu(f) ~Basically XAML helpers
*Server*(sf) -Web(f) (References to Calculate, ServiceProviderWeb, ServiceData) ~Service.svc (cs files) that define what methods are exposed through a contract ~aspx
-ServiceProviderWeb(f) (References to ServiceData) ~References to external services ~Loads the objects from the database with stored procedures
-ServiceData(f) (References to none) ~ Links to classes (objects) in Client Data ~Interfaces to use with ServiceProviderWeb ~Database Utilities class
-Calculate(f) (References to ServiceData) ~Web references to other services used
Instead of this explicit registration of available components, MEF provides a way to discover them implicitly, via composition. A MEF component, called a part, declaratively specifies both its dependencies (known as imports) and what capabilities (known as exports) it makes available.
Prism was the code name for the guidance formally known as the Composite Application Guidance for WPF and Silverlight. For brevity and conciseness, and due to customer demand, this guidance is now referred to simply as Prism.
Prism assumes you have hands-on experience with WPF or Silverlight. There are a few important concepts that Prism uses heavily, and you should become familiar with them. They include the following: - XAML (Extensible Application Markup Language). The language to declaratively define and initialize the user interface in WPF and Silverlight applications. - Data binding. This is how UI elements are connected to components and data in WPF and Silverlight. - Resources. These are how styles, data templates, and control templates are created and managed in WPF and Silverlight. - Commands. These are how user gestures and input are connected to controls. - User controls. These are components that provide custom behavior or custom appearance. - Dependency properties. These are extensions to the common language runtime (CLR) property system to enable property setting and monitoring in support of data binding, routed commands, and events. - Behaviors. Behaviors are objects that encapsulate interactive functionality that can be easily applied to controls in the user interface.
It should be noted that while Prism is not difficult to learn, developers must be ready and willing to embrace patterns and practices that may be new to them. Management understanding and commitment is crucial, and the project deadline must accommodate an investment of time up front for learning these patterns and practices.
learning new techniques by examining source code.
IoC - Inversion of Control Another way to do this is to have the framework define events and have the client code subscribe to these events. .NET is a good example of a platform that has language features to allow people to declare events on widgets. You can then bind a method to the event by using a delegate.