Created
January 12, 2010 19:49
-
-
Save aaronrussell/275536 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var markers = []; | |
var geocoder = new google.maps.Geocoder(); | |
var local_search = new GlocalSearch(); | |
var default_point = new google.maps.LatLng(53.800651, -4.064941); | |
var default_map = { | |
zoom: 5, | |
center: default_point, | |
mapTypeControl: true, | |
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}; | |
$(document).ready(function() | |
{ | |
var map = new google.maps.Map(document.getElementById("map_canvas"), default_map); | |
$('input[name="postcode"]').blur(function(){ | |
clear_markers(); | |
var address = []; | |
$('#address_1, #address_2, #address_3, #address_4').each(function(){ | |
if ($(this).val()!='') address.push($(this).val()); | |
}); | |
address.push($(this).val(), 'UK'); | |
/* | |
geocoder.geocode( { 'address': address.join(', ')}, function(results, status) { | |
if (status == google.maps.GeocoderStatus.OK){ | |
var point = results[0].geometry.location; | |
map.setCenter(point); | |
map.setZoom(16); | |
var marker = new google.maps.Marker({ | |
map: map, | |
position: point, | |
draggable: true | |
}); | |
markers.push(marker); | |
update_coords(point); | |
google.maps.event.addListener(marker, 'dragend', function() { | |
update_coords(marker.get_position()); | |
}); | |
} else { | |
alert("Geocode was not successful for the following reason: " + status); | |
} | |
}); | |
*/ | |
my_geocoder(address, function(point){ | |
map.setCenter(point); | |
map.setZoom(16); | |
var marker = new google.maps.Marker({ | |
map: map, | |
position: point, | |
draggable: true | |
}); | |
markers.push(marker); | |
update_coords(point); | |
google.maps.event.addListener(marker, 'dragend', function() { | |
update_coords(marker.get_position()); | |
}); | |
}); | |
}); | |
}); | |
function update_coords(point){ | |
$('#lattitude').val(point.lat()); | |
$('#longitude').val(point.lng()); | |
} | |
function clear_markers(){ | |
for(var i=0; i<markers.length; i++){ | |
markers[i].set_map(null); | |
} | |
markers.length = 0; | |
} | |
function my_geocoder(address, callback_function){ | |
local_search.setSearchCompleteCallback(null, function(){ | |
if (local_search.results[0]){ | |
var lat = local_search.results[0].lat; | |
var lng = local_search.results[0].lng; | |
var point = new google.maps.LatLng(lat, lng); | |
callback_function(point); | |
} | |
else{ | |
alert("Postcode not found!"); | |
} | |
}); | |
local_search.execute(address + ", UK"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment