Skip to content

Instantly share code, notes, and snippets.

@renooble
Created January 28, 2014 03:29
Show Gist options
  • Save renooble/8661838 to your computer and use it in GitHub Desktop.
Save renooble/8661838 to your computer and use it in GitHub Desktop.
autocomplete address with
// this is the get the address with hitting the enter button without submitting
$(function() {
$("#id_address")
.geocomplete()
.bind("geocode:result", function(event, result){
$.log(result.formatted_address);
$("input#id_lat").val(result.geometry.location.lat());
$("input#id_lng").val(result.geometry.location.lng());
$("#id_hid")
.geocomplete("find", result.geometry.location.lat() + "," + result.geometry.location.lng())
.bind("geocode:result", function (_event, _result) {
$("input#id_location_type").val(_result.geometry.location_type);
});
})
.bind("geocode:error", function(event, status){
$.log(status);
})
.bind("geocode:multiple", function(event, results){
$.log("Multiple: " + results.length + " results found");
});
});
$.log = function(message){
var $logger = $("#address");
$logger.val(message); // write the found address to the hidden form
}
var marker, map;
$(function(){
var options = {
map: ".map_canvas",
// location: [ {{ auction.location.x }}, {{ auction.location.y }} ],
//details: "#data",
mapOptions: {
zoom: 18,
mapTypeId: "satellite",
scrollwheel: true,
noClear: true,
},
markerOptions: {
disabled: true
}
};
var useFormattedAddress = true;
// if the lat and lng are specified, use them
if (
( $("#id_lat").val().length > 0 )
&&
( $("#id_lng").val().length > 0 )
)
{
options.location = new Array(
parseFloat( $("#id_lat").val() ),
parseFloat( $("#id_lng").val() )
);
useFormattedAddress = false;
}
// useFormattedAddress = false; //for testing
$("#id_initial_address").geocomplete(options);
map = $("#id_initial_address").geocomplete("map");
marker = new google.maps.Marker({
position: new google.maps.LatLng(0, 0),
title: "Current Address",
draggable: true
});
google.maps.event.addListener(marker, 'dragend', function (e) {
var latLng = e.latLng;
$("input[name=lat]").val(latLng.lat());
$("input[name=lng]").val(latLng.lng());
$("#id_formatted_address2")
.geocomplete("find", latLng.lat() + "," + latLng.lng())
.bind("geocode:result", function(event, result){
$("#id_address").val( result.formatted_address );
// the following lines grab results.types from the response
// var t = result.types.join(', ');
// if (!t.length) t = "unknown";
// $("input#id_location_type").val(t);
// This is to grab the refult.geometry.location_type
$("input#id_location_type").val(result.geometry.location_type);
$(".straight-corners").show("slow"); // this shows the email form after dragging the pin
})
;
$("#reset").show();
});
function show_marker(loc)
{
marker.setPosition(loc);
marker.setMap(map);
map.setCenter(loc);
map.setZoom(18);
}
$("#id_initial_address")
.bind("geocode:result", function(event, result){
show_marker(result.geometry.location);
// the initial value:
$("input#id_location_type").val(result.geometry.location_type);
// $("input#id_location_type").val(result.types.join(', '));
})
;
if (!useFormattedAddress)
{
show_marker(new google.maps.LatLng( options.location[0], options.location[1] ));
}
// no lat and lng in the hidden fields? -> use formatted address
else
{
$("#id_initial_address").trigger("geocode");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment