将我的脚趾浸入谷歌地图 api (v3)。
首先,我能够使用使用 jquery 选择器播种的预填充值初始化地图,如下所示,一切都按预期工作。
我遇到了动态更新这些值的困难。我确定在某个地方有一个教程或一些示例,但是经过几天的搜索,我无法将其整理出来。
朝着正确的方向轻推 - 或者更好的是一个明确的例子,将不胜感激。对于初学者,我在根据对 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>