-1

マップを表示し、div 部分も表示しますが、マップ上にマーカー ポイントを表示しない次のコードは、事前に感謝します。

<!DOCTYPE html>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false">                                                            </script>

<script>
    var myCenter=new google.maps.LatLng(17.382094947877505,78.431396484375);
    var geocoder,map;

    function initialize()
    {
        geocoder = new google.maps.Geocoder();
        var mapProp = {
            center:myCenter,
            zoom:5,
            mapTypeId:google.maps.MapTypeId.ROADMAP
        };

        var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
    }

    function codeAddress() {
        var address = document.getElementById('address').value;
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status === google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
     }
     google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
    <div id="pannel">
        <input type="address" type="textbox" value="hyderbad,Andhra Pradesh">
        <input type="button" value="Geocode" onclick="codeAddress();">
    </div>
    <div id="googleMap" style="width:1200px;height:800px;"></div>
</body>
</html>
4

1 に答える 1

0

コードを機能させるために必要なことが 2 つあります。

  1. id入力フィールドに属性があることを確認してください(を使用しているためdocument.getElementById
  2. map変数を再初期化しない

したがって、アドレス入力は次のようになります。

<input id="address" type="text" value="hyderbad,Andhra Pradesh">

そしてマップのインスタンス化:

map = new google.maps.Map(document.getElementById("googleMap"),mapProp);

完全なコードは次のとおりです。

<!DOCTYPE html>
<html>
<head>
   <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
   <script>
      var myCenter=new google.maps.LatLng(17.382094947877505,78.431396484375);
      var geocoder,map;

      function initialize() {
         geocoder = new google.maps.Geocoder();
         var mapProp = {
            center:myCenter,
            zoom:5,
            mapTypeId:google.maps.MapTypeId.ROADMAP
         };

         map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
      }

      function codeAddress() {
         var address = document.getElementById('address').value;
         geocoder.geocode( { 'address': address}, function(results, status) {
            if (status === google.maps.GeocoderStatus.OK) {
               map.setCenter(results[0].geometry.location);
               var marker = new google.maps.Marker({
                  map: map,
                  position: results[0].geometry.location
               });
            } else {
               alert('Geocode was not successful for the following reason: ' + status);
            }
         });
      }

      google.maps.event.addDomListener(window, 'load', initialize);
   </script>
</head>
<body>
   <div id="panel">
      <input id="address" type="text" value="hyderbad,Andhra Pradesh">
      <input type="button" value="Geocode" onclick="codeAddress();">
   </div>
   <div id="googleMap" style="width:1200px;height:800px;"></div>
</body>
</html>
于 2013-06-22T09:01:54.863 に答える