PF2G Posted August 24, 2012 at 10:27 AM Report Share #473348 Posted August 24, 2012 at 10:27 AM (edited) 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 August 24, 2012 at 11:52 AM by PF2G Link to comment Share on other sites More sharing options...
brunoais Posted August 24, 2012 at 10:39 AM Report Share #473350 Posted August 24, 2012 at 10:39 AM (edited) 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 August 24, 2012 at 10:41 AM 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 More sharing options...
KiNgPiTo Posted August 24, 2012 at 10:43 AM Report Share #473351 Posted August 24, 2012 at 10:43 AM Talvez seja melhor criares um p onde queres que te apareça a mensagem de erro: <p id="erro"></p> E alteras o valor com javascript: function errorFunction(){ document.getElementById('erro').innerHTML = "Geocoder failed"; } Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now