Information Dynamics
BLOG.INFORMATIONDYNAMICS.US

Geo location through the browser

Here is the page-level script code to do this:







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;
}
}

else
{
var loc = GEOIP.GeoLocation.GetLocation();
map1.Latitude = loc.Latitude;
map1.Longitude = loc.Longitude;
FormattedAdr.Text = loc.City + " " + loc.Region + " " +
loc.PostalCode;
map1.DataBind();
}
}

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());


Location loc = new Location();
loc.CountryCode = s2[0];
loc.CountryName = s2[1];
loc.City = s2[2];
loc.Region = s2[3];
loc.RegionName = s2[4];
loc.Latitude = double.Parse(s2[5]);
loc.Longitude = double.Parse(s2[6]);
loc.PostalCode = s2[7];
loc.AreaCode = s2[8];
loc.MetroCode = s2[9];
return loc;
}
catch
{
return null;
}
}
}
}

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.

http://eggheadcafe.com/FileUpload/1145921998_GeoLocation.zip

Transferring Page Date - 8 ways

*1. Use the querystring:*

protected void QueryStringButton_Click(object sender, EventArgs e)
{
Response.Redirect("QueryStringPage.aspx?Data=" +
Server.UrlEncode(DataToSendTextBox.Text));
}
*
**2. Use HTTP POST:*

PostBackUrl="~/HttpPostPage.aspx" onclick
="HttpPostButton_Click"/>

protected void HttpPostButton_Click(object sender, EventArgs e)
{
// The PostBackUrl property of the Button takes care of where to send
it!
}

* 3. Use Session State:*

protected void SessionStateButton_Click(object sender, EventArgs e)
{
Session["Data"] = DataToSendTextBox.Text;
Response.Redirect("SessionStatePage.aspx");
}

*4. Use public properties:*

public string DataToSend
{
get
{
return DataToSendTextBox.Text;
}
}

protected void PublicPropertiesButton_Click(object sender,
EventArgs e)
{
Server.Transfer("PublicPropertiesPage.aspx");
}

*5. Use PreviousPage Control Info:*

protected void ControlInfoButton_Click(object sender, EventArgs e)
{
Server.Transfer("ControlInfoPage.aspx");
}

// target page:
protected void Page_Load(object sender, EventArgs e)
{
var textbox =
PreviousPage.FindControl("DataToSendTextbox") asTextBox;
if (textbox != null)
{
DataReceivedLabel.Text = textbox.Text;
}
}

*6. Use HttpContext Items Collection:*

protected void HttpContextButton_Click(object sender, EventArgs e)
{
HttpContext.Current.Items["data"] = DataToSendTextBox.Text;
Server.Transfer("HttpContextItemsPage.aspx");
}

// target page:
protected void Page_Load(object sender, EventArgs e)
{
this.DataReceivedLabel.Text =(String) HttpContext.Current
..Items["data"];
}

*7. Use Cookies:*

protected void CookiesButton_Click(object sender, EventArgs e)
{
HttpCookie cook = new HttpCookie("data");
cook.Expires = DateTime.Now.AddDays(1);
cook.Value = DataToSendTextBox.Text;
Response.Cookies.Add(cook);
Response.Redirect("HttpCookiePage.aspx");
}

// target page:
protected void Page_Load(object sender, EventArgs e)
{
DataReceivedLabel.Text = Request.Cookies["data"].Value;
}

*8. Use Cache:*

protected void CacheButton_Click(object sender, EventArgs e)
{
Cache["data"] = DataToSendTextBox.Text;
Server.Transfer("CachePage.aspx");
}
// target page:
protected void Page_Load(object sender, EventArgs e)
{
this.DataReceivedLabel.Text = (string) Cache["data"];
}

Tools

Hey, why build my own page of good tools when someone else has done it
already:
http://www.hamishgraham.net/page/My-Favourite-Tools.aspx

QR Maker

Nice QR creator
http://www.waspbarcode.com

favicon

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" />

Logger

http://www.pnpguidance.net/post/ManagedExtensibilityFrameworkTutorialMEF.aspx

Silverlight Project (Using MEF, Separation of Concerns, Loosely Coupled Components, Service Locator pattern)

VS2010
Silverlight Business Application

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

Fun way to learn simple basic web concepts

http://www.20thingsilearned.com/en-US

IoC and Dependency Injection pattern

http://www.martinfowler.com/articles/injection.html

MEF, Prism (Silverlight)

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.

Calendar

February 2012
SuMoTuWeThFrSa
1234
567891011
12131415161718
19202122232425
26272829

Monthly Archives

Recent Posts

  1. Lambda Expressions
    Wednesday, February 08, 2012
  2. Agile
    Tuesday, February 07, 2012
  3. Visual Studio Toolbox items greyed out
    Friday, January 27, 2012
  4. Serialization
    Friday, January 13, 2012
  5. Silverlight graphic
    Thursday, January 12, 2012
  6. HTML5
    Wednesday, January 11, 2012
  7. C# HTML5
    Wednesday, January 11, 2012
  8. Free Web Services and Database
    Tuesday, January 10, 2012
  9. Books to read
    Thursday, January 05, 2012
  10. ASP.NET some little used tips and tricks
    Tuesday, January 03, 2012

Recent Comments

  1. Deb on Agile
    2/7/2012
  2. Deb on Silverlight graphic
    1/12/2012
  3. Deb on Silverlight graphic
    1/12/2012
  4. Deb on Silverlight graphic
    1/12/2012
  5. Deb on HTML editor
    12/19/2011
  6. Deb on HTML editor
    12/19/2011
  7. Deb on HTML editor
    12/19/2011
  8. Deb on HTML editor
    12/19/2011
  9. Deb on HTML editor
    12/17/2011
  10. Deb on HTML editor
    12/7/2011

Subscribe


Tag Cloud