If you notice, there are variables of street_address
, route
, intersection
re-used in the entire code base. They are actually from Google Maps geocoder address components.
How can I refactor this one so that I don't have to declare them one by one?
Note that in the geocoder address components, those 3 mentioned above are just part of the 20+ components. I just removed the rest for simplicity sake.
I have tried declaring the address components in a new array, and using for
loop, but it gets messy and more complicated.
$(function() {
$('#shop_formatted_address').autocomplete({
// This bit uses the geocoder to fetch address values
source: function(request, response) {
geocoder.geocode( {'address': request.term }, function(results, status) {
response($.map(results, function(item) {
// Get address_components
for (var i = 0; i < item.address_components.length; i++)
{
var addr = item.address_components[i];
var get_street_address, get_route, get_intersection;
if (addr.types[0] == 'street_address')
get_street_address = addr.long_name;
if (addr.types[0] == 'route')
get_route = addr.long_name;
if (addr.types[0] == 'intersection')
get_intersection = addr.long_name;
}
return {
label: item.formatted_address,
value: item.formatted_address,
lat: item.geometry.location.lat(),
lng: item.geometry.location.lng(),
street_address: get_street_address,
route: get_route,
intersection: get_intersection
}
}));
})
},
// This bit is executed upon selection of an address
select: function(event, ui) {
clearValue();
// Get values
$('#shop_lat').val(ui.item.lat);
$('#shop_lng').val(ui.item.lng);
$('#shop_street_address').val(ui.item.street_address);
$('#shop_route').val(ui.item.route);
$('#shop_intersection').val(ui.item.intersection);
var location = new google.maps.LatLng(ui.item.lat, ui.item.lng);
marker.setPosition(location);
map.setCenter(location);
},
// Changes the current marker when autocomplete dropdown list is focused
focus: function(event, ui) {
var location = new google.maps.LatLng(ui.item.lat, ui.item.lng);
marker.setPosition(location);
map.setCenter(location);
}
});
});
// Add listener to marker for reverse geocoding
google.maps.event.addListener(marker, 'drag', function() {
geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
clearValue();
if (results[0]) {
// Get address_components
for (var i = 0; i < results[0].address_components.length; i++)
{
var addr = results[0].address_components[i];
if (addr.types[0] == 'street_address')
$('#shop_street_address').val(addr.long_name);
if (addr.types[0] == 'route')
$('#shop_route').val(addr.long_name);
if (addr.types[0] == 'intersection')
$('#shop_intersection').val(addr.long_name);
}
$('#shop_formatted_address').val(results[0].formatted_address);
$('#shop_lat').val(marker.getPosition().lat());
$('#shop_lng').val(marker.getPosition().lng());
} else {
alert('No results found. Please revise the address or adjust the map marker.');
}
}
});
});
$.map(results, ...
you're declaring these variablesvar get_street_address, get_route, get_intersection;
but you're overwriting them in thefor
loop (assuming the loop runs more than once) so when the values are used in the returned object, you're only getting the latest values. Are you certain that's what you want? \$\endgroup\$