home *** CD-ROM | disk | FTP | other *** search
/ Image Zone / ImageZone.iso / setup.exe / Graphics / html / jsEx / GeoCode.js < prev    next >
Encoding:
Text File  |  2009-12-10  |  8.9 KB  |  220 lines

  1.     
  2.     if (GBrowserIsCompatible()) { 
  3.  
  4.     var map;
  5.     var geo;
  6.     var reasons=[];
  7.     var marker;
  8.  
  9.     function load() {
  10.       map = new GMap2(document.getElementById("map"));
  11.  
  12.       // ====== Restricting the range of Zoom Levels =====
  13.       // Get the list of map types      
  14.       var mt = map.getMapTypes();
  15.       // Overwrite the getMinimumResolution() and getMaximumResolution() methods
  16.       for (var i = 0; i < mt.length; i++) {
  17.           mt[i].getMinimumResolution = function() { return 2; }
  18.           //mt[i].getMaximumResolution = function() { return 11; }
  19.       }
  20.  
  21.       map.addControl(new GLargeMapControl());
  22.       map.addControl(new GMapTypeControl());
  23.       map.setCenter(new GLatLng(38.892091, -77.024055), 2);
  24.       mapOverlaysCrossIcon();
  25.  
  26.  
  27.       // Register an event listener to display the latitude and longitude
  28.       GEvent.addListener(map, "zoomend", function() {
  29.           if (marker == 0)
  30.               return;
  31.           var point = marker.getLatLng(); //mapOverlaysCrossIcon();
  32.           map.setCenter(point);
  33.  
  34.       });
  35.  
  36.       // Register an event listener to display the latitude and longitude
  37.       GEvent.addListener(map, "moveend", function() {
  38.           mapOverlaysCrossIcon();
  39.       });
  40.  
  41.       // ====== Create a Client Geocoder ======
  42.       geo = new GClientGeocoder(); 
  43.  
  44.       // ====== Array for decoding the failure codes ======
  45.       reasons[G_GEO_SUCCESS]            = "Success";
  46.       reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
  47.       reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
  48.       reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
  49.       reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
  50.       reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
  51.       reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
  52.     }
  53.  
  54.  
  55.     // ===== list of words to be standardized =====
  56.     var standards = [   ["road","rd"],   
  57.                         ["street","st"], 
  58.                         ["avenue","ave"], 
  59.                         ["av","ave"], 
  60.                         ["drive","dr"],
  61.                         ["saint","st"], 
  62.                         ["north","n"],   
  63.                         ["south","s"],    
  64.                         ["east","e"], 
  65.                         ["west","w"],
  66.                         ["expressway","expy"],
  67.                         ["parkway","pkwy"],
  68.                         ["terrace","ter"],
  69.                         ["turnpike","tpke"],
  70.                         ["highway","hwy"],
  71.                         ["lane","ln"]
  72.                      ];
  73.  
  74.     // ===== convert words to standard versions =====
  75.     function standardize(a) {
  76.       for (var i=0; i<standards.length; i++) {
  77.         if (a == standards[i][0])  {a = standards[i][1];}
  78.       }
  79.       return a;
  80.     }
  81.  
  82.     // ===== check if two addresses are sufficiently different =====
  83.     function different(a,b) {
  84.       // only interested in the bit before the first comma in the reply
  85.       var c = b.split(",");
  86.       b = c[0];
  87.       // convert to lower case
  88.       a = a.toLowerCase();
  89.       b = b.toLowerCase();
  90.       // remove apostrophies
  91.       a = a.replace(/'/g ,"");
  92.       b = b.replace(/'/g ,"");
  93.       // replace all other punctuation with spaces
  94.       a = a.replace(/\W/g," ");
  95.       b = b.replace(/\W/g," ");
  96.       // replace all multiple spaces with a single space
  97.       a = a.replace(/\s+/g," ");
  98.       b = b.replace(/\s+/g," ");
  99.       // split into words
  100.       awords = a.split(" ");
  101.       bwords = b.split(" ");
  102.       // perform the comparison
  103.       var reply = false;
  104.       for (var i=0; i<bwords.length; i++) {
  105.         //GLog.write (standardize(awords[i])+"  "+standardize(bwords[i]))
  106.         if (standardize(awords[i]) != standardize(bwords[i])) {reply = true}
  107.       }
  108.       //GLog.write(reply);
  109.       return (reply);
  110.     }
  111.  
  112.  
  113.       // ====== Plot a marker after positive reponse to "did you mean" ======
  114.  
  115.     function place(lat, lng) {
  116.           var point = new GLatLng(lat, lng);
  117.  
  118.           map.setCenter(point, 14);
  119.           mapOverlaysCrossIcon();
  120.       }
  121.  
  122.       // ====== Geocoding ======
  123.       function showAddress() {
  124.         var search = document.getElementById("search").value;
  125.         // ====== Perform the Geocoding ======
  126.         geo.getLocations(search, function(result) {
  127.             map.clearOverlays();
  128.             marker = 0;
  129.             if (result.Status.code == G_GEO_SUCCESS) {
  130.                 // ===== If there was more than one result, "ask did you mean" on them all =====
  131.                 if (result.Placemark.length > 1) {
  132.                     document.getElementById("message").innerHTML = "Did you mean:";
  133.                     // Loop through the results
  134.                     for (var i = 0; i < result.Placemark.length; i++) {
  135.                         var p = result.Placemark[i].Point.coordinates;
  136.                         document.getElementById("message").innerHTML += "<br>" + (i + 1) + ": <a href='javascript:place(" + p[1] + "," + p[0] + ")'>" + result.Placemark[i].address + "<\/a>";
  137.                     }
  138.                 }
  139.                 // ===== If there was a single marker, is the returned address significantly different =====
  140.                 else {
  141.                     document.getElementById("message").innerHTML = "";
  142.                     if (different(search, result.Placemark[0].address)) {
  143.                         document.getElementById("message").innerHTML = "Did you mean: ";
  144.                         var p = result.Placemark[0].Point.coordinates;
  145.                         document.getElementById("message").innerHTML += "<a href='javascript:place(" + p[1] + "," + p[0] + ")'>" + result.Placemark[0].address + "<\/a>";
  146.                     } else {
  147.                         var p = result.Placemark[0].Point.coordinates;
  148.                         document.getElementById("message").innerHTML = "<a href='javascript:place(" + p[1] + "," + p[0] + ")'>" + "Set Location: " + result.Placemark[0].address + "<\/a>";
  149.                     }
  150.                 }
  151.             }
  152.             // ====== Decode the error status ======
  153.             else {
  154.                 var reason = "Code " + result.Status.code;
  155.                 if (reasons[result.Status.code]) {
  156.                     reason = reasons[result.Status.code]
  157.                 }
  158.                 alert('Could not find "' + search + '" ' + reason);
  159.             }
  160.         }
  161.         );
  162.       }
  163.  
  164.       // Creating icon which are always shown at center of the gmap2
  165.       function mapOverlaysCrossIcon() {
  166.           map.clearOverlays();
  167.           marker = 0;
  168.           var icon = new GIcon();
  169.           icon.image = "../images/geotag_center.png";
  170.           icon.iconSize = new GSize(21, 26);
  171.           icon.iconAnchor = new GPoint(10, 25);
  172.           var p = map.getCenter();
  173.           var lat = p.lat();
  174.           var lng = p.lng();
  175.           //update display with the new coords
  176.           document.getElementById("message").innerHTML = p.toString();
  177.  
  178.           //update the app for the "Apply" functionality
  179.           var setLocate = "HLGEOCHANGED:(" + lat + "," + lng + ")";
  180.           window.location.href = setLocate;
  181.           marker = new GMarker(p, { icon: icon, draggable: true, bouncy: true });
  182.  
  183.           //show new cooridnates:
  184.           var center = map.getCenter();
  185.           document.getElementById("message").innerHTML = center.toString();
  186.  
  187.           map.addOverlay(marker);
  188.           marker.enableDragging();
  189.  
  190.           GEvent.addListener(marker, "click", function() {
  191.               var p = marker.getPoint();
  192.               var lat = p.lat();
  193.               var lng = p.lng();
  194.               var msg = "<div class='apply'><a href='HLGEOSAVE:(" + lat + "," + lng + ")'>" + "Apply!" + "<\/a>";
  195.               msg += "<br><a href='HLGEOSAVEALL:(" + lat + "," + lng + ")'>" + "Apply to all!" + "<\/a><\/div>";
  196.               map.openInfoWindowHtml(p, msg);
  197.           });
  198.  
  199.           GEvent.addListener(marker, "dragend", function() {
  200.               var p = marker.getPoint();
  201.               var lat = p.lat();
  202.               var lng = p.lng();
  203.               //update display with the new coords
  204.               document.getElementById("message").innerHTML = p.toString();
  205.  
  206.               var setLocate = "HLGEOCHANGED:(" + lat + "," + lng + ")";
  207.               window.location.href = setLocate;
  208.       });
  209.           
  210.       }
  211.  
  212.     }
  213.  
  214.  
  215.     // display a warning if the browser was not compatible
  216.     else {
  217.       alert("Sorry, the Google Maps API is not compatible with this browser");
  218.     }
  219.  
  220.