ANTON072
4/7/2015 - 11:01 AM

googlemapの情報ウインドウにフォームを表示する http://bl.ocks.org/ANTON072/f19a3eb2e02f3d723d7d

googlemapの情報ウインドウにフォームを表示する http://bl.ocks.org/ANTON072/f19a3eb2e02f3d723d7d

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
  #map_canvas {
    width: 600px;
    height: 450px;
  }
  </style>
  <script src="//maps.googleapis.com/maps/api/js?key=AIzaSyDg13xe7Kt-Kwb0K-3ThoyyXkO38hvWt9I&?sensor=TRUE"></script>
</head>
<body>

  <div id="map_canvas"></div>

  <form id="markerPropertyForm">
    <div>
      marker icon:<br>
      <input type="text" id="iconTxt" size="32">
      <input type="button" id="changeBtn" value="変更">
    </div>
  </form>

  <script>

  function initialize(){
    var mapDiv = document.getElementById('map_canvas');
    var mapCanvas = new google.maps.Map(mapDiv, {
      center: new google.maps.LatLng(35.681382, 139.766084),
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    // マーカーを表示
    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(35.681382, 139.766084),
      map: mapCanvas
    });

    // フォームを取得
    var form = document.getElementById('markerPropertyForm');
    var changeBtn = document.getElementById('changeBtn');
    var iconTxt = document.getElementById('iconTxt');
    google.maps.event.addDomListener(changeBtn, 'click', function(){
      marker.setIcon(iconTxt.value);
    });

    // 情報ウインドウの表示
    var infofWnd = new google.maps.InfoWindow({
      position: new google.maps.LatLng(35.681382, 139.766084),
      content: form
    });
    infofWnd.open(null, marker);

    google.maps.event.addListener(mapCanvas, 'zoom_changed', function(){
      console.log('zoom change');
      var zoom = mapCanvas.getZoom();
      console.log(zoom);
      // 最大2
      var iconSize = -(1/20) * zoom + 2;
      console.log(iconSize);
      var imgUrl = 'http://chart.googleapis.com/chart?chst=d_map_spin&chld=' + iconSize + '|0|FF8429|23|b|' + zoom;
      marker.setIcon(imgUrl);
    });

  }

  google.maps.event.addDomListener(window, 'load', initialize);
  </script>

</body>
</html>