私はうまくいくGoogleマップを持つウェブページを持っています。代わりに、そこに「ボーフム」にハードコード都市名を有するので、私は、ヘッダーにJavascript:id "map"のh3ヘッダーの内容を見つける方法は?
<h3 id="city"><i>Bochum</i></h3>
を見つけて、私のinit()関数内でその値を使用したいと思います。
私はおそらく以下のコードで何かマイナーなものがありません。私を助けてください、そのような "子供"のAPIリファレンスに私を参照してください、私のJavascriptのスキルは非常に錆びています。
また、私のマーカーの色のように、私のh3ヘッダーにもう1つの価値を渡すことができますか?
ありがとうございました! アレックス
<html>
<head>
<style type="text/css">
h1,h2,h3,p { text-align: center; }
#map { width: 400; height: 200; margin-left: auto; margin-right: auto; }
</style>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function init() {
for (var child in document.body.childNodes) {
child = document.body.childNodes[child];
if (child.nodeName == "H3")
alert(child);
}
// use the #city value here instead
city = 'Bochum';
if (city.length > 1)
findCity(city);
}
function createMap(center) {
var opts = {
zoom: 9,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
return new google.maps.Map(document.getElementById("map"), opts);
}
function findCity(city) {
var gc = new google.maps.Geocoder();
gc.geocode({ "address": city}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var pos = results[0].geometry.location;
var map = createMap(pos);
var marker = new google.maps.Marker({
map: map,
title: city,
position: pos,
});
}
});
}
</script>
<head>
<body onload="init();">
<h3 id="city"><i>Bochum</i></h3>
<div id="map"></div>
</body>
</html>
まず、jQueryを実際に使用する必要があります:http://jquery.com/ – CarneyCode
1行で80kbのライブラリを読み込むのはなぜですか? –
@ Dr.Molle:一度圧縮されたのはわずか26kbです。アプリケーションコードをより単純で信頼性の高いものから完全なページサイズを小さくすることの間では、トレードオフです。たとえば、$( '#city')。text()を使用すると、それは短くなり、タグが削除された場合には破損しません。 – Douglas