GeoCoderProxy.asmx collapse
<%@ WebService Language="C#" CodeBehind="~/App_Code/GeoCoderProxy.cs" Class="GeoCoderProxy" %>
App_Code/GeoCoderProxy.cs collapse
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using RFocusWS;

/// <summary>
/// Summary description for GeoCoderProxy
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class GeoCoderProxy : System.Web.Services.WebService
{
    /// <summary>
    /// Geocodes the IP address and returns a <c>GeocodeResult</c>
    /// </summary>
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public GeocodeResult IPAddressLookupJson(string ipAddress)
    {
        return new RFocusWS.GeoCoder().IPAddressLookup(ipAddress);
    }

    /// <summary>
    /// Geocodes the physical address and returns a <c>GeocodeResult</c>
    /// </summary>
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public GeocodeResult PhysicalAddressLookupJson(string address)
    {
        return new RFocusWS.GeoCoder().PhysicalAddressLookup(address);
    }

    /// <summary>
    /// Reverse geocodes the latitude/longitude pair and returns a <c>GeocodeResult</c>
    /// </summary>
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public GeocodeResult ReverseGeocodeJson(double latitude, double longitude)
    {
        return new RFocusWS.GeoCoder().ReverseGeocode(latitude, longitude);
    }
}
AddressLookup.aspx collapse
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddressLookup.aspx.cs" Inherits="AddressLookup" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Address Lookup</title>
    <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAXw0Gn2f0e-KVCjvRQu5UwxTgIssoOZSoEMkKklFmzu8AXooxOBS7VXcSWa2ThfSRTjfnxpJJOfbr8g"></script>
    <script type="text/javascript">
    google.load("maps", "2.x");
    var map;
    
    function initialize() 
    {
        map = new google.maps.Map2(document.getElementById("map"));
        map.setCenter(new google.maps.LatLng(41.03943, -111.956442), 15);
        map.addControl(new GLargeMapControl());
        map.enableScrollWheelZoom();

        var boundaries = new GLatLngBounds(new GLatLng(41.0385, -111.9563), new GLatLng(41.0396, -111.9534));
        var overlay = new GGroundOverlay("logo.png", boundaries);
        map.addOverlay(overlay);

        document.body.onunload = GUnload;
    }
    google.setOnLoadCallback(initialize);
    
    function plotPoint(geocodeResult)
    {
        if (geocodeResult.Point != null)
        {
            var latLng = new GLatLng(geocodeResult.Point.Latitude, geocodeResult.Point.Longitude);
            var marker = new GMarker(latLng);
            GEvent.addListener(marker, "click", function() {
                var html = 
                    geocodeResult.Address.Street1 + "<br/>" +
                    (geocodeResult.Address.Street2 != null ? geocodeResult.Address.Street2 : "") +
                    geocodeResult.Address.City + ", " + geocodeResult.Address.StateOrProvince + " " + geocodeResult.Address.PostalCode + "<br/>" +
                    (geocodeResult.Address.CountryCode != null ? geocodeResult.Address.CountryCode: geocodeResult.Address.Country);
                marker.openInfoWindowHtml(html);
            });
            map.addOverlay(marker);
            map.panTo(latLng);
        }
        else
        {
            alert("Cannot find any location for this address");
        }
    }
    </script>
</head>
<body>
    <form id="form1" onsubmit="getPoint(); return false" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="GeoCoderProxy.asmx" />
            </Services>
        </asp:ScriptManager>
        <div style="width: 400px">
            <div id="map" style="height: 400px; width: 400px">
            
            </div>
            <div style="text-align: right; padding-top: 5px">
                <label for="addressTextBox">Address: </label>
                <input type="text" id="addressTextBox" /> 
                <input type="button" onclick="getPoint()" value="Plot Address" />
            </div>
        </div>
    </form>
    <script type="text/javascript">
    function getPoint()
    {
        GeoCoderProxy.PhysicalAddressLookupJson(document.getElementById("addressTextBox").value, 
            plotPoint);
    }
    </script>
</body>
</html>
IPLookup.aspx collapse
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="IPLookup.aspx.cs" Inherits="IPLookup" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>IP Address Lookup</title>
    <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAXw0Gn2f0e-KVCjvRQu5UwxTgIssoOZSoEMkKklFmzu8AXooxOBS7VXcSWa2ThfSRTjfnxpJJOfbr8g"></script>
    <script type="text/javascript">
    google.load("maps", "2.x");
    var map;
    
    function initialize() 
    {
        map = new google.maps.Map2(document.getElementById("map"));
        map.setCenter(new google.maps.LatLng(41.03943, -111.956442), 15);
        map.addControl(new GLargeMapControl());
        map.enableScrollWheelZoom();
        
        var boundaries = new GLatLngBounds(new GLatLng(41.0385, -111.9563), new GLatLng(41.0396, -111.9534));
        var overlay = new GGroundOverlay("logo.png", boundaries);
        map.addOverlay(overlay);
        
        document.body.onunload = GUnload;
        getPoint();
    }
    google.setOnLoadCallback(initialize);
    
    function plotPoint(geocodeResult)
    {
        if (geocodeResult.Point != null)
        {
            var latLng = new GLatLng(geocodeResult.Point.Latitude, geocodeResult.Point.Longitude);
            map.addOverlay(new GMarker(latLng));
            map.panTo(latLng);
        }
        else
        {
            alert("Cannot find any location associated with this IP address");
        }
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server">
            <Services>
                <asp:ServiceReference Path="GeoCoderProxy.asmx" />
            </Services>
        </asp:ScriptManager>
        <div id="map" style="height: 400px; width: 400px">
        
        </div>
        <input type="button" onclick="getPoint()" value="Force IP Address Lookup" />
    </form>
    <script type="text/javascript">
    function getPoint()
    {
        GeoCoderProxy.IPAddressLookupJson('<%= Request.ServerVariables["REMOTE_ADDR"] %>', plotPoint);
    }
    </script>
</body>
</html>
ReverseGeocode.aspx collapse
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReverseGeocode.aspx.cs" Inherits="ReverseGeocode" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Reverse Geocode</title>
    <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAXw0Gn2f0e-KVCjvRQu5UwxTgIssoOZSoEMkKklFmzu8AXooxOBS7VXcSWa2ThfSRTjfnxpJJOfbr8g"></script>
    <script type="text/javascript">
    //<![CDATA[
    google.load("maps", "2");
    var map, questionMarkIcon;
    var lastClick = {lat: 0, lng: 0};
    
    function initialize() 
    {
        map = new google.maps.Map2(document.getElementById("map"));
        map.setCenter(new google.maps.LatLng(41.03943, -111.956442), 15);
        map.addControl(new GLargeMapControl());
        map.enableScrollWheelZoom();

        var boundaries = new GLatLngBounds(new GLatLng(41.0385, -111.9563), new GLatLng(41.0396, -111.9534));
        var overlay = new GGroundOverlay("logo.png", boundaries);
        map.addOverlay(overlay);
        
        questionMarkIcon = new GIcon();
        questionMarkIcon.image = "question-mark.png";
        questionMarkIcon.iconSize = new GSize(23, 36);
        questionMarkIcon.shadowSize = new GSize(48, 36);
        questionMarkIcon.iconAnchor = new GPoint(15, 36);
        questionMarkIcon.infoWindowAnchor = new GPoint(15, 0);
        questionMarkIcon.shadow = "question-mark-shadow.png";
        questionMarkIcon.infoShadowAnchor = new GPoint(20, 35);
        
        document.body.onunload = GUnload;
        
        GEvent.addListener(map, "click", function(overlay, latLng) {
            if (!overlay) //if overlay is not null, the user has clicked on an object on the map
            {
                lastClick.lat = latLng.lat();
                lastClick.lng = latLng.lng();
                var icon = new GIcon(questionMarkIcon);
                var markerOptions = {icon: icon};
                //Place marker on click location
                var marker = new GMarker(latLng, markerOptions);
                GEvent.addListener(marker, "click", function() {
                    var html = 
                        "Original click:<br/>" +
                        latLng.lat().toFixed(4) + " latitude<br/>" +
                        latLng.lng().toFixed(4) + " longitude";
                    marker.openInfoWindowHtml(html);
                });
                map.addOverlay(marker);
                GeoCoderProxy.ReverseGeocodeJson(latLng.lat(), latLng.lng(), plotPoint);
            }
        });
    }
    google.setOnLoadCallback(initialize);
    
    function plotPoint(geocodeResult)
    {
        if (geocodeResult.Address != null && geocodeResult.Point != null)
        {
            var latLng = new GLatLng(parseFloat(geocodeResult.Point.Latitude), parseFloat(geocodeResult.Point.Longitude));
            var marker = new GMarker(latLng);
            GEvent.addListener(marker, "click", function() {
                var html = 
                    geocodeResult.Address.Street1 + "<br/>" +
                    geocodeResult.Address.City + ", " + geocodeResult.Address.StateOrProvince + " " + geocodeResult.Address.PostalCode + "<br/>" +
                    geocodeResult.Address.Country;
                marker.openInfoWindowHtml(html);
            });
            //Place marker on reverse-geocode result location
            map.addOverlay(marker);
            
            //Zoom the map to the correct level based on the distance between the two points
            var minLat, minLng, maxLat, maxLng;
            if (lastClick.lng > parseFloat(geocodeResult.Point.Longitude))
            {
                minLng = parseFloat(geocodeResult.Point.Longitude);
                maxLng = lastClick.lng;
            }
            else
            {
                minLng = lastClick.lng;
                maxLng = parseFloat(geocodeResult.Point.Longitude);
            }
            if (lastClick.lat > parseFloat(geocodeResult.Point.Latitude))
            {
                minLat = parseFloat(geocodeResult.Point.Latitude);
                maxLat = lastClick.lat;
            }
            else
            {
                minLat = lastClick.lat;
                maxLat = parseFloat(geocodeResult.Point.Latitude);
            }
            
            var pointSW = new GLatLng(minLat, minLng);
            var pointNE = new GLatLng(maxLat, maxLng);
            
            var bounds = new GLatLngBounds(pointSW, pointNE);
            map.setZoom(map.getBoundsZoomLevel(bounds));
            
            //Convert data to float type
            minLat = parseFloat(minLat);
            maxLat = parseFloat(maxLat);
            minLng = parseFloat(minLng);
            maxLng = parseFloat(maxLng);
            
            //Zoom to the center of the two points
            var centerLatLng = new GLatLng((minLat + maxLat) / 2, (minLng + maxLng) / 2);
            map.panTo(centerLatLng);
        }
    }
    //]]>
    </script>
</head>
<body>
    <form runat="server">
        <asp:ScriptManager runat="server">
            <Services>
                <asp:ServiceReference Path="GeoCoderProxy.asmx" />
            </Services>
        </asp:ScriptManager>
        <div id="map" style="height: 400px; width: 400px">
        
        </div>
        <p>Click anywhere on the map to trigger a reverse geocode attempt</p>
    </form>
</body>
</html>