HOME BLOG

MAPAS API

Galeria de exemplos: Serviço de Geocode (Geocoder)

Este exemplo demonstra como se poderá utilizar o serviço de geocode, com o tipo Geocoder.

				<html>
					<head>
						<title>SAPO MAPS With OpenLayers - Geocoder</title>		
						<script type="text/javascript" src="http://js.sapo.pt/Bundles/SAPOMapsAPI.js"></script>
						
						<script type="text/javascript">
							
							var markers = false;
							var map = false;
							function init(){
								map = new SAPO.Maps.Map("map");
								markers = new SAPO.Maps.Markers();
								
								map.addMarkers(markers);
							}
							
							function search(){
								var geocoder = new SAPO.Maps.Geocoder();
								//Request with max results
								geocoder.getLocations(document.getElementById("query").value, complete, error, {maxResults: 10});
							}
							
							function complete(pois){
								markers.removeMarkers();
								
								var marker = false, lonlat = false;
								
								for (var i = 0; i < pois.length; ++i) {					
									lonlat = new OpenLayers.LonLat(Number(pois[i].Longitude), Number(pois[i].Latitude));
									marker = new SAPO.Maps.Marker(lonlat);
									marker.html = '<h2>' + pois[i].Name + '</h2>';
									
									markers.addMarker(marker);
									
									marker.registerEvent('click', this, function(marker){
										marker.openPopup(marker.html);
									});
								}
							}
							
							function error(){
								alert("Error while geocoding");
							}
						</script>
					</head>
					<body onload="init();">
						<div id="map" style="width:980px; height: 400px"></div>
						<input id="query" type="text" value="Av. da Liberdade"/>
						<input type="button" onclick="search();" value="Search" />
						<div>This example shows you, how to geocode a place.</div>
					</body>
				</html>