|
var MODULO = MODULO || {}; |
|
|
|
MODULO.timer = null; |
|
|
|
MODULO.init = function () { |
|
$("#direccion").keypress( MODULO.keyup_delay ); |
|
MODULO.googleMap.init(); |
|
} |
|
|
|
|
|
MODULO.keyup_delay = function () { |
|
clearTimeout( MODULO.timer ); |
|
MODULO.timer= setTimeout( function(){ |
|
MODULO.googleMap.updateAddress( $('#direccion').val() ); |
|
}, 1000); |
|
} |
|
|
|
|
|
|
|
MODULO.googleMap = {}; |
|
|
|
MODULO.googleMap.map = null; |
|
|
|
MODULO.googleMap.coord = { lat: -33.0137926, lng: -71.5189799}; |
|
|
|
MODULO.googleMap.markers = []; |
|
|
|
MODULO.googleMap.updateAddress = function ( address ) { |
|
var geocoder = new google.maps.Geocoder(); |
|
geocoder.geocode( { 'address': address }, function ( results, status ) { |
|
|
|
$("#log").html(""); |
|
|
|
if (status == google.maps.GeocoderStatus.OK) { |
|
|
|
var retorno_google = results[0]; |
|
|
|
$("#latitud").val( retorno_google.geometry.location.lat() ); |
|
$("#longitud").val( retorno_google.geometry.location.lng() ); |
|
|
|
for ( item in retorno_google.address_components ) { |
|
var direccion = retorno_google.address_components[item]; |
|
$("#log").append(direccion.types[0] + " : " + direccion.long_name + "<br>"); |
|
|
|
} |
|
$("#log").append("Latitud : " + retorno_google.geometry.location.lat() + "<br>"); |
|
$("#log").append("Longitud : " + retorno_google.geometry.location.lng() + "<br>"); |
|
|
|
MODULO.googleMap.coord.lat = retorno_google.geometry.location.lat(); |
|
MODULO.googleMap.coord.lng = retorno_google.geometry.location.lng(); |
|
MODULO.googleMap.map.setCenter( MODULO.googleMap.coord ); |
|
MODULO.googleMap.addMarker(); |
|
} |
|
}); |
|
} |
|
|
|
MODULO.googleMap.addMarker = function () { |
|
// Limpia los marcadores |
|
for (var i=0; i<MODULO.googleMap.markers.length; i++) { |
|
MODULO.googleMap.markers[i].setMap(null); |
|
} |
|
MODULO.googleMap.markers = []; |
|
var marker = new google.maps.Marker({ |
|
position : MODULO.googleMap.coord, |
|
map: MODULO.googleMap.map |
|
}); |
|
MODULO.googleMap.markers.push(marker); |
|
} |
|
|
|
MODULO.googleMap.init = function () { |
|
|
|
MODULO.googleMap.map = new google.maps.Map(document.getElementById('maps'), { |
|
center: MODULO.googleMap.coord, |
|
zoom: 15, |
|
mapTypeId: google.maps.MapTypeId.TERRAIN |
|
}); |
|
MODULO.googleMap.addMarker(); |
|
|
|
} |
|
|
|
$("document").ready( MODULO.init ); |