0

ID「アドレス」を割り当てたページに要素があります。

<p id="address">1600 Pennsylvania Ave NW, Washington, DC 20500, United States</p>

今、次を使用してGoogleマップを作成しようとしています:

<script type="text/javascript">
  var gecoder, map;
  var address = document.getElementById('address').innerHTML;
  function googlemap(address) {
    geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var myOptions = {
        zoom: 8,
        center: results[0].geometry.location,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      }
    });
  }
</script>

ただし、これは何も返しません。空白を取得します。私が間違っていることは何ですか?

4

1 に答える 1

0

これは私がやったことであり、うまくいきます。頭の中の JavaScript:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false" />
<script type="text/javascript">
    var geocoder, map;

    function googlemap() {
        var address = document.getElementById('address').innerHTML;

        geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var myPosition = results[0].geometry.location;
                alert(myPosition);

                var myOptions = {
                    zoom: 8,
                    center: myPosition,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                }
                map = new google.maps.Map(document.getElementById('map'), myOptions);

                var marker = new google.maps.Marker({
                    map: map,
                    position: myPosition
                });
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    }
</script>

本体:

<body onload="googlemap()">
<form id="form1" runat="server">
<div>
    <p id="address">1600 Pennsylvania Ave NW, Washington, DC 20500, United States</p>
    <div id="map" style="overflow:hidden; width:400px; height:400px"></div>
</div>
</form>

于 2013-04-09T20:28:45.630 に答える