Google Maps Application Developing —— Search Address

 
<script type="text/javascript">
(function(){
	var map, geocoder, marker, infowindow;

	window.onload = function(){
		
		var mapOptions = {
			zoom:14,
			center:new google.maps.LatLng(39.9, 116.4),
			mapTypeId:google.maps.MapTypeId.ROADMAP
		};

		map = new google.maps.Map(document.getElementById('map'), mapOptions);

		var form = document.getElementById('addressForm');

			form.onsubmit = function(){
			var address = document.getElementById('address').value;

			getCoordinates(address);

			return false;
		}
	}

function getCoordinates(address){
	if(!geocoder){
		geocoder = new google.maps.Geocoder();
	}

	var geocoderRequest = {address:address}
		
	geocoder.geocode(geocoderRequest, function(results, status){
		if(status == google.maps.GeocoderStatus.OK){
			map.setCenter(results[0].geometry.location);

			if(!marker){
				marker = new google.maps.Marker({map:map});
			}
			marker.setPosition(results[0].geometry.location);

			if(!infowindow){
				infowindow = new google.maps.InfoWindow();
			}

			var content = '<strong>' + results[0].formatted_address + 
					'</strong><br/>';
			content += '纬度: ' + results[0].geometry.location.lat() + '<br/>';
			content += '经度: ' + results[0].geometry.location.lng();

			infowindow.setContent(content);

			infowindow.open(map, marker);
		}
	});
}
})();
posted @ 2012-01-28 23:00  Create Chen  阅读(906)  评论(0编辑  收藏  举报