I'm working with the MapQuest API (Geocoding + Please Search end points) using jQuery. The goal is to store the lat/long value that was populated from user input in the first API and passing these values in the next API within the 'circle' parameter. Full code below:
codeBreakApp.searchResults = (location, query) => {
$.ajax({
url: codeBreakApp.searchEndPoint,
method: 'GET',
dataType: 'JSON',
data: {
key: codeBreakApp.apiKey,
location: location,
},
}).then((res) => {
console.log(res);
console.log(res.results[0].locations[0].latLng);
let locationLng = (res.results[0].locations[0].latLng.lng);
let locationLat = (res.results[0].locations[0].latLng.lat);
console.log(locationLng);
console.log(locationLat);
$.ajax({
url: codeBreakApp.placeEndPoint,
method: 'GET',
dataType: 'JSON',
data: {
key: codeBreakApp.apiKey,
circle: [locationLng, locationLat, 5000],
q: query,
sort: 'relevance',
pageSize: 10,
},
}).then((res) => {
console.log(res);
console.log('Found something');
}).fail((err) => {
console.log(err);
console.log('Failed');
});
});
}
const userSearch = function() {
$('.form').on('submit', function(e) {
e.preventDefault();
// Start location input
let userLocationInput = $('input').val();
console.log(userLocationInput);
// Places input
let selectedPlaceInput = $('#places option:selected').text();
console.log(selectedPlaceInput);
codeBreakApp.searchResults(userLocationInput, selectedPlaceInput);
});
}
// Initializing functions
codeBreakApp.init = function () {
codeBreakApp.searchResults((''), (''));
userSearch();
}
// Document ready
$(function () {
codeBreakApp.init();
});
My console logs return the info I'm looking for from the form input values and I'm able to retrieve the lat/long from the first API results object - but when I run the second API, nothing happens. Any ideas on how to get around this?
Thanks!