Jump to content

Ao tentar alterar texto no DOM usando document.write() dá erro


PF2G

Recommended Posts

Boas,

Eu estou a fazer uma página com geolocalização HTML5/Javascript.

E neste momento tenho o código que pretendo, mas dá-me o alert e o que eu pretendo é que ele escreva na página; ja troquei por document.write, mas não acontece nada, não aparece o alert nem escreve o

results[0].formatted_address

Est é o código completo:

<script type="text/javascript">
 var geocoder;
 if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
codeLatLng(lat, lng)
}
function errorFunction(){
alert("Geocoder failed");
}
 function initialize() {
geocoder = new google.maps.Geocoder();
 }
 function codeLatLng(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
  console.log(results)
	if (results[1]) {
	 //formatted address
	alert(results[0].formatted_address)
	//find country name
		 for (var i=0; i<results[0].address_components.length; i++) {
		for (var b=0;b<results[0].address_components[i].types.length;b++) {
		//there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
			if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
				//this is the object you are looking for
				city= results[0].address_components[i];
				break;
			}
		}
	}


	} else {
	  alert("No results found");
	}
  } else {
	alert("Geocoder failed due to: " + status);
  }
});
 }
</script>
</head>
<body onload="initialize()">
</body>
</html>
Edited by PF2G
Link to comment
Share on other sites

Tu já tens mais de 200 posts neste forum. Já deves saber como é que queremos os títulos dos tópicos.

Altera o título do tópico, sff, "Alert - document.write" não é um título que descreva o tópico. Não me faças ser eu a fazê-lo.

Depois de alterares o titulo do tópico para algo suficientemente apropriado eu ajudo-te nisso.

Edited by brunoais

"[Os jovens da actual geração]não lêem porque não envolve um telecomando que dê para mirar e atirar, não falam porque a trapalhice é rainha e o calão é rei" autor: thoga31

Life is a genetically transmitted disease, induced by sex, with death rate of 100%.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.