2013-01-19 60 views
0

對不起,由於我缺乏知識,但我試圖在JavaScript中打印出html代碼中的變量(如果有人可以清理JS,這將是一個獎金)。我嘗試添加document.write,但從未工作。基本上我需要在地圖下打印出街道和國家。在HTML中編寫JavaScript變量

<!DOCTYPE HTML> 
<html> 
<head> 
<script src="http://maps.google.com/maps/api/js?sensor=false"> 
</script> 
<script type="text/javascript"> 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position){ 
     var latitude = position.coords.latitude; 
     var longitude = position.coords.longitude; 
     var coords = new google.maps.LatLng(latitude, longitude); 
     var mapOptions = { 
      zoom: 15, 
      center: coords, 
      mapTypeControl: true, 
      navigationControlOptions: { 
       style: google.maps.NavigationControlStyle.SMALL 
      }, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      map = new google.maps.Map(
       document.getElementById("mapContainer"), mapOptions 
       ); 
      var marker = new google.maps.Marker({ 
        position: coords, 
        map: map, 
        title: "Your current location!" 
      }); 

     }); 
    }else { 
     alert("Geolocation API is not supported in your browser."); 
    } 
</script> 

<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; 
       } 
      } 
     } 
     //city data 
     alert(city.short_name + " " + city.long_name) 


     } else { 
      alert("No results found"); 
     } 
     } else { 
     alert("Geocoder failed due to: " + status); 
     } 
    }); 
    } 
</script> 

<style type="text/css"> 
#mapContainer { 
    height: 500px; 
    width: 800px; 
    border:10px solid #eaeaea; 
} 
</style> 
</head> 
<body onload="initialize()"> 
    <div id="mapContainer"></div> 
</body> 
</html> 

回答

0

你可以用下面的構造來代替文件撰寫的

var x= document.getElementById("mapContainer"); 
x.innerHTML="your content goes here";