var geocoder; var map; var marker; // initialise the google maps objects, and add listeners function gmaps_init(){ // center of the universe var latlng = new google.maps.LatLng(45.03478270000001,10.744630599999937); var options = { zoom: 7, center: latlng, scrollwheel: false, mapTypeId: google.maps.MapTypeId.ROADMAP }; // create our map object map = new google.maps.Map(document.getElementById("gmaps-canvas"), options); // the geocoder object allows us to do latlng lookup based on address geocoder = new google.maps.Geocoder(); // the marker shows us the position of the latest address marker = new google.maps.Marker({ map: map, position: new google.maps.LatLng(45.03478270000001,10.744630599999937), draggable: true }); // event triggered when marker is dragged and dropped google.maps.event.addListener(marker, 'dragend', function() { geocode_lookup( 'latLng', marker.getPosition()); }); // event triggered when map is clicked google.maps.event.addListener(map, 'click', function(event) { marker.setPosition(event.latLng) geocode_lookup( 'latLng', event.latLng); }); $('#gmaps-error').hide(); } // move the marker to a new position, and center the map on it function update_map( geometry ) { map.fitBounds( geometry.viewport ); marker.setPosition( geometry.location ); geocode_lookup( 'latLng', marker.getPosition() ); $('#coordinate').html(geometry.location.toString()); } // fill in the UI elements with new position data function update_ui( address, latLng ) { $('#gmaps-input-address').autocomplete("close"); $('#gmaps-input-address').val(address); } // Query the Google geocode object // // type: 'address' for search by address // 'latLng' for search by latLng (reverse lookup) // // value: search query // // update: should we update the map (center map and position marker)? function geocode_lookup( type, value, update ) { // default value: update = false update = typeof update !== 'undefined' ? update : false; request = {}; request[type] = value; geocoder.geocode(request, function(results, status) { var message = { results: results, status: status }; var stringMessage = JSON.stringify(message); sendMessage(stringMessage); $('#coordinate').html(value.toString()); if (status == google.maps.GeocoderStatus.OK) { // Only update the map (position marker and center map) if requested if( update ) { update_map( results[0].geometry ) } } }); }; // initialise the jqueryUI autocomplete element function autocomplete_init() { $("#gmaps-input-address").autocomplete({ // source is the list of input options shown in the autocomplete dropdown. // see documentation: http://jqueryui.com/demos/autocomplete/ source: function(request,response) { // the geocode method takes an address or LatLng to search for // and a callback function which should process the results into // a format accepted by jqueryUI autocomplete geocoder.geocode( {'address': request.term }, function(results, status) { response($.map(results, function(item) { return { label: item.formatted_address, // appears in dropdown box value: item.formatted_address, // inserted into input element when selected geocode: item // all geocode data: used in select callback event } })); }) }, // event triggered when drop-down option selected select: function(event,ui){ update_ui( ui.item.value, ui.item.geocode.geometry.location ) update_map( ui.item.geocode.geometry ) } }); // triggered when user presses a key in the address box $("#gmaps-input-address").bind('keydown', function(event) { if(event.keyCode == 13) { geocode_lookup( 'address', $('#gmaps-input-address').val(), true ); // ensures dropdown disappears when enter is pressed $('#gmaps-input-address').autocomplete("disable") } else { // re-enable if previously disabled above $('#gmaps-input-address').autocomplete("enable") } }); }; // autocomplete_init // Send a message to the parent var sendMessage = function (msg) { // Make sure you are sending a string, and to stringify JSON window.parent.postMessage(msg, '*'); }; $(document).ready(function() { if( $('#gmaps-canvas').length ) { gmaps_init(); autocomplete_init(); }; $("#moveMarker").click(function() { var lat = parseFloat(document.getElementById("latitudine").value); var lng = parseFloat(document.getElementById("longitudine").value); var newLatLng = new google.maps.LatLng(lat, lng); marker.setPosition(newLatLng); map.setCenter(newLatLng); geocode_lookup( 'latLng', newLatLng); }); });