-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocoder.js
34 lines (33 loc) · 1.31 KB
/
geocoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$(document).ready(function() {
$("#new_listing").submit(function(event) {
// disable the submit button to prevent repeated clicks
// $(".btn-success").attr("disabled", "disabled");
// get address from the form
var address = $("#listing_address").val();
var city_state = $("#listing_secondary_city_id").find(":selected").text();
var zip = $("#listing_zip").val();
// pass this data to geocoder function
geocoder(address+', '+city_state+', '+zip);
return false;
});
});
function geocoder(full_address) {
var form$ = $("#new_listing");
$.ajax({
dataType: "json",
url: "http://maps.googleapis.com/maps/api/geocode/json?sensor=false",
data: { "address": full_address },
success: function(data, textStatus){
// insert long/lat in the form before it gets submitted
console.log(data);
form$.append("<input type='hidden' name='listing[longitude]' value='"+data.results[0].geometry.location.lng+"' />");
form$.append("<inpute type='hidden' name='listing[latitude]' value='"+data.results[0].geometry.location.lat+"' />");
// and submit
form$.get(0).submit();
},
error: function(){
$(".submit-button").removeAttr("disabled");
$("#geocode-error").html("An error occured determining your location. Please try again.");
}
});
};