function setMapSearch(params) {
var geocoder = new google.maps.Geocoder();
var position = new google.maps.LatLng(params.lat, params.lng);
var map_options = {
zoom: params.zoom,
center: position,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById(params.container), map_options);
var marker = new google.maps.Marker({
position: position,
map: map,
draggable: true
});
//マーカーのドラッグ時
google.maps.event.addListener(marker, "drag", function() {
updateLatLng();
});
//マーカーのドロップ時
google.maps.event.addListener(marker, "dragend", function() {
var latlng = marker.getPosition();
geocoder.geocode({latLng:latlng, language:"ja"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0].geometry) {
updateAddress(results[0].formatted_address);
}
}
});
});
updateLatLng();
setSearch();
//緯度・経度を書き換え
function updateLatLng() {
var latlng = marker.getPosition();
$("input#lat").val(latlng.lat());
$("input#lng").val(latlng.lng());
}
//住所を書き換え
function updateAddress(address) {
$("input#address").val(address);
}
//住所から検索して、マップを移動
function setSearch() {
$("input#search").click(function() {
var address = $("input#address").val();
geocoder.geocode({"address": address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker.setPosition(results[0].geometry.location);
updateLatLng();
}
});
return false;
});
}
}