蘸我的腳趾谷歌地圖api(v3)。谷歌地圖api3動態更新與用戶輸入變化
要開始我能夠初始化與jQuery選擇器播種預填充值的地圖,如下所示,一切都按預期工作。
我遇到了動態更新這些值的困難。我確定有一個教程或一些例子可以在某處查到,但經過幾天的搜索後,我只是無法將其整理出來。
向正確的方向推動 - 或者更好的一個明確的例子將不勝感激。對於初學者Im,在根據對mapZoomReturn的更改進行縮放後更改,但將應用到列出的所有var。
非常感謝您的幫助。
<script>
//set up variables to contain our input values
var mapIdReturn = null;
var mapZoomReturn = null;
var mapWidthReturn = null;
var mapHeightReturn = null;
var mapTypeReturn = null;
var mapAddressReturn = null;
var mapAddressElement = null;
var mapMarkerReturn = null;
var mapInfoWindowReturn = null;
var infowindowPlace = null;
var mapMarkerImageReturn = null;
var mapKMLReturn = null;
var map = null;
var mapOptions = null;
var tr_gmaps = null;
var output = null;
jQuery(document).ready(function(jQuery) {
// populate initial values
mapIdReturn = jQuery('input[id=mapId]').val();
mapZoomReturn = jQuery('select[id=mapZoom]').val();
mapWidthReturn = jQuery('input[id=mapWidth]').val();
mapHeightReturn = jQuery('input[id=mapHeight]').val();
mapTypeReturn = jQuery('select[id=mapType]').val();
mapAddressReturn = jQuery('input[id=mapAddress]').val();
mapMarkerReturn = jQuery('select[id=mapMarker]').val();
mapInfoWindowReturn = jQuery('textarea[id=mapInfoWindow]').val();
mapMarkerImageReturn = jQuery('input[id=mapMarkerImage]').val();
mapKMLReturn = jQuery('input[id=mapKML]').val();
});
function initialize() {
// my start
//mapZoomReturn = jQuery('select[id=mapZoom]').change(function(event){ jQuery(this).val(); });
mapOptions = {
center: new google.maps.LatLng(43.703793, -72.326187),
zoom: parseFloat(mapZoomReturn),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var input = document.getElementById('mapAddress');
var autocomplete = new google.maps.places.Autocomplete(input);
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map
});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
//setTypes([]);
infowindow.close();
marker.setVisible(false);
input.className = '';
var place = autocomplete.getPlace();
if (!place.geometry) {
// Inform the user that the place was not found and return.
input.className = 'notfound';
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(parseFloat(mapZoomReturn));
}
var image = new google.maps.MarkerImage(
place.icon,
new google.maps.Size(71, 71),
new google.maps.Point(0, 0),
new google.maps.Point(17, 34),
new google.maps.Size(35, 35));
marker.setIcon(image);
marker.setPosition(place.geometry.location);
var icon = null;
var address = null;
var phone = null;
var web = null;
if (place.address_components) {
//console.log(place.address_components);
icon = place.icon;
address = place.formatted_address;
phone = place.formatted_phone_number;
web = place.website;
}
infowindowPlace = '<div class="marker inside">';
infowindowPlace += (icon !== null && icon !== undefined) ? '<img src="' + icon + '" class="marker icon"/>' : '';
infowindowPlace += '<strong>' + place.name + '</strong><br>';
infowindowPlace += (address !== null && address !== undefined) ? address + '<br>' : '';
infowindowPlace += (phone !== null && phone !== undefined) ? phone + '<br>' : '';
infowindowPlace += (web !== null && web !== undefined) ? '<a href="' + web +'" class="" target="_blank">'+ web +'</a><br>' : '';
infowindowPlace += (mapInfoWindowReturn !== null && mapInfoWindowReturn !==undefined) ? '<span class="marker extras">' + mapInfoWindowReturn + '</span>' : '';
infowindowPlace += '</div>';
infowindowPlace += '<a href="' + place.url +'" class="marker jumplink" target="_blank">external map</a>';
infowindow.setContent(infowindowPlace);
infowindow.open(map, marker);
});
}
</script>
謝謝你的提示重新parseInt函數 - 我後來把它轉換的初始化腳本'map.setZoom(parseFloat(mapZoomReturn))'它不是問題。我必須真的訴諸另一個插件來動態更新這些值嗎?我希望有人能指出我的方式來偵聽器添加到這些輸入 - 我真的不能讓文檔太大意義,圍繞事件理解圍繞在地圖本身 - 外沒有事件。 – orionrush
gmap3插件的確讓編碼變得更簡單了...就你而言 – charlietfl