Skip to content

Instantly share code, notes, and snippets.

@dbeattie71
Created September 4, 2014 19:28
Show Gist options
  • Save dbeattie71/59659639b86000ef3029 to your computer and use it in GitHub Desktop.
Save dbeattie71/59659639b86000ef3029 to your computer and use it in GitHub Desktop.
ADC.Tool.ZoomTo = function() {
var doc = document,
defaultValue = "i.e. City, State, ZIP code",
geocoder, input, map = ADC.Map.getMap(),
pointFeature, urlBase = "http://rs1.adc4gis.com/img/icons/other/";
var vector = new OpenLayers.Layer.Vector("Zoom To");
map.addLayer(vector);
map.setLayerIndex(vector, 0);
function plotLocation(coords, zoom, location) {
if (pointFeature) {
vector.removeFeatures(pointFeature)
}
var lonlat = new OpenLayers.LonLat(coords[0], coords[1]);
lonlat = ADC.MapTools.projToOpenLayers(lonlat);
var point = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
pointFeature = new OpenLayers.Feature.Vector(point, null, {
externalGraphic: urlBase + (ADC.Util.IE6 ? "marker_ie6.gif" : "marker.png"),
graphicXOffset: -8,
graphicYOffset: -16,
pointRadius: 16
});
if (input && location) input.value = location;
vector.addFeatures(pointFeature);
map.setCenter(lonlat, zoom + 7)
}
var pub = {
createToolbarUI: function(div) {
input = doc.createElement("input");
input.className = "adc-toolbar-zoomto-input";
input.value = defaultValue;
input.onfocus = function() {
if (input.value === defaultValue) {
input.value = ""
}
};
input.onblur = function(e) {
if (input.value.replace(/ /g, "") === "") {
input.value = defaultValue
}
};
input.onkeydown = function(e) {
e = ADC.Event(e);
if (e.keyCode === 13 && input.value !== defaultValue) {
e.preventDefault();
pub.zoomToLocation(input.value);
if (ADC.Util.IE8) {
button.focus();
setTimeout(function() {
input.focus()
}, 20)
}
}
};
div.appendChild(input);
var button = doc.createElement("button");
button.className = "adc-toolbar-zoomto-button";
button.setAttribute("type", "button");
button.innerHTML = "Go";
button.onclick = function() {
if (input.value !== defaultValue) {
pub.zoomToLocation(input.value)
}
};
div.appendChild(button)
},
zoomToLocation: function(location) {
if (!geocoder) geocoder = new google.maps.Geocoder;
geocoder.geocode({
address: location,
region: "US"
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var coords = [results[0].geometry.location.lng(), results[0].geometry.location.lat()];
var accuracy = 4;
switch (results[0].geometry.location_type) {
case "APPROXIMATE":
accuracy = 5;
break;
case "GEOMETRIC_CENTER":
accuracy = 6;
break;
case "RANGE_INTERPOLATED":
accuracy = 7;
break;
case "ROOFTOP":
accuracy = 8;
break
}
plotLocation(coords, accuracy, location)
} else {
alert("You have entered an unknown address, please double check and try again.")
}
})
},
checkLocation: function(location, callback) {
if (!geocoder) geocoder = new google.maps.Geocoder;
geocoder.geocode({
address: location,
region: "US"
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var coords = [results[0].geometry.location.lng(), results[0].geometry.location.lat()];
callback(coords)
} else {
callback()
}
})
},
resetUI: function() {
if (input) input.value = defaultValue;
if (pointFeature) {
vector.removeFeatures(pointFeature)
}
}
};
return pub
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment