function searchByGps() {
$("#Zip").val(""); $("#Key").val("")
search("https://www.mapquestapi.com/search/v1/radius" +
"?key=" + mq_key + "&radius=" + $("#inradius").val() + "&callback=processPOIs&maxMatches=" + inmatch +
"&origin=" + encodeURIComponent($("#Gps").val()) + "&hostedData=" + intable);
}
function searchByZip() {
$("#Gps").val(""); $("#Key").val("")
search("https://www.mapquestapi.com/search/v1/radius" +
"?key=" + mq_key + "&radius=" + $("#inradius").val() + "&callback=processPOIs&maxMatches=" + inmatch +
"&origin=" + encodeURIComponent($("#Zip").val()) + "&hostedData=" + intable);
}
function search(url){
MQA.IO.doJSONP(url);
}
function processPOIs(results) {
if (results.searchResults != null && results.searchResults.length > 0) {
drawPOIsOnMap(results.searchResults);
drawResultTable(results.searchResults);
if ($("#Key").val()) {
$("#Zip").val(results.searchResults[0].fields.address + " " + results.searchResults[0].fields.city + ", " +
results.searchResults[0].fields.state + " " + results.searchResults[0].fields.postal);
}
$.ajax('/Home/GetLocationFeatures', {
data: JSON.stringify(parseToEntityObjects(results.searchResults)),
dataType: "json",
type: "post",
contentType: "application/json",
success: function (featuredata) { processFeatures(featuredata, results.searchResults); }
});
}
else if (results.info && results.info.statuscode == 610) {
// ambiguities
// results.collections[1] is To, 0 is From
$("#Gps").val(results.collections[0][0].latLng.lat + ',' + results.collections[0][0].latLng.lng);
searchByGps();
}
else {
$('#results .frame').html("<h1>Oops! We couldn’t find any results. Please try your search again.</h1>");
}
}
function processOLOs(oloData, searchResults) {
$.each(searchResults, function (i, result) {
if (oloData != null) {
$.each(oloData.restaurants, function () {
if (this.telephone == result.fields.Phone) {
var oloid = '#olo' + result.fields.RecordId;
$(oloid).append($("<a></a>", { href: this.url, "class": "ololinks", target: "_blank", text: "Place an Order" }));
}
}); } }); }
function processFeatures(featureData, searchResults) {
$.each(searchResults, function (i, result) {
if (featureData != null) {
$.each(featureData, function () {
if (this.StoreNumber == result.fields.RecordId) {
var fid = '#feature' + result.fields.RecordId;
$(fid).append($("<a></a>", {href:this.Url, "class":"featurelinks", target:"_blank", text:this.Label}));
} }); } }); }
function drawResultTable(results) {
$("#maploading").hide();
$('#results').html("<h1>Search Results</h1>");
$.each(results, function (i, result) {
$("<div></div>", { "class": 'resultrow' })
.append("<p class='addressrowresult'>" +
result.fields.address + "<br/ >" +
result.fields.city + ", " + result.fields.state + "<br/ >" +
result.fields.Phone + "<br /></p>")
.append("<div>" + getRoundedDistance(result) + " Mi." + getMapItLink(result) + "</div>")
.append("<div class='olos' id='olo" + result.fields.RecordId + "'></div>") // olo can find this span$(#olo#storenumber#) later
.append("<div class='features' id='feature" + result.fields.RecordId + "'></div>") // feature can find this span$(#feature#storenumber#) later
.appendTo("#results");
}); }
function parseToEntityObjects(data) {
var locations = [];
$.each(data, function () {
locations.push({
StoreNumber: this.fields.RecordId,
Address1: this.fields.address,
City: this.fields.city,
ZipCode: this.fields.postal,
Phone: this.fields.Phone
});
});
return locations;
}
function getRoundedDistance(result) {
if (result.distance > 10) {
return Math.round(result.distance);
} else if (result.distance > 1) {
return Math.round(result.distance * 10) / 10;
}
return Math.round(result.distance * 100) / 100;
}
function getMapItLink(result){
var destination = result.fields.address + ',' + result.fields.city + ',' + result.fields.state + ',' + result.fields.postal;
var link = $("<a>", { href: "#", "class": "mapitlink", title: "Map It!", onclick: "mapIt(getOrigin(), '" + destination + "');return false;", text: "Map It!" })
.attr({ "data-category": "Find", "data-event": "Map Quest" });
return $('<div>').append(link.clone()).html();// hack to get string not js object for mqa
}
function getPOIRollover(result) {
var rollover = $("<div></div>", { "class": "poirollover" }).append($('<h4></h4>').text(result.fields.N))
.append($('<span></span>').text(result.fields.address))
.append($('<br />')).append($('<span></span').text(result.fields.city + ", " + result.fields.state))
.append($('<br />')).append($('<span></span>').text(result.fields.Phone))
.append($('<br />')).append($('<span></span>').text(getRoundedDistance(result) + " Mi."))
.append(getMapItLink(result));
return $('<div>').append(rollover.clone()).html(); // hack to get string not js object for mqa
}
function mapIt(origin, destination) {
// build a url and send to the Directions Web Service
MQA.IO.doJSONP("http://www.mapquestapi.com/directions/v1/route?" +
"key=" + mq_key + "&" +
"from=" + origin + "&" +
"to=" + destination + "&" +
"shapeFormat=raw&generalize=0.1&" +
"callback=drawDirections");
}