javascript - 在 div 中时使我的 Google map 响应

标签 javascript html css google-maps responsive-design

我已经在我的网站上添加了一个谷歌地图,我是新手,我使用这个脚本来添加它:

    <script src="http://maps.google.com/maps/api/js?sensor=true"></script>
    <script>
      function writeAddressName(latLng) {
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({
          "location": latLng
        },
        function(results, status) {
          if (status == google.maps.GeocoderStatus.OK)
            document.getElementById("address").innerHTML = results[0].formatted_address;
          else
            document.getElementById("error").innerHTML += "Unable to retrieve your address" + "<br />";
        });
      }

      function geolocationSuccess(position) {
        var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        // Write the formatted address
        writeAddressName(userLatLng);

        var myOptions = {
          zoom : 16,
          center : userLatLng,
          mapTypeId : google.maps.MapTypeId.ROADMAP
        };
        // Draw the map
        var mapObject = new google.maps.Map(document.getElementById("map"), myOptions);
        // Place the marker
        new google.maps.Marker({
          map: mapObject,
          position: userLatLng
        });
        // Draw a circle around the user position to have an idea of the current localization accuracy
        var circle = new google.maps.Circle({
          center: userLatLng,
          radius: position.coords.accuracy,
          map: mapObject,
          fillColor: '#0000FF',
          fillOpacity: 0.5,
          strokeColor: '#0000FF',
          strokeOpacity: 1.0
        });
        mapObject.fitBounds(circle.getBounds());
      }

      function geolocationError(positionError) {
        document.getElementById("error").innerHTML += "Error: " + positionError.message + "<br />";
      }

      function geolocateUser() {
        // If the browser supports the Geolocation API
        if (navigator.geolocation)
        {
          var positionOptions = {
            enableHighAccuracy: true,
            timeout: 10 * 1000 // 10 seconds
          };
          navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError, positionOptions);
        }
        else
          document.getElementById("error").innerHTML += "Your browser doesn't support the Geolocation API";
      }

      window.onload = geolocateUser;
    </script>
    <style type="text/css">
      #map {
        width: 625px;
        height: 500px;
      }
      .google-maps {
        position: relative;
        padding-bottom: 75%; // This is the aspect ratio
        height: 0;
        overflow: hidden;
}
    </style>

这是我使用它的 div:

    <div  class='container-map'>
    <h3>Farmacias cercanas a ti</h3>
    <p><b>Address</b>: <span id="address"></span></p>

    <div id='map' class="google-maps"></div>
    <p id="error"></p>
    <button type="submit" class="btn btn-danger">Submit</button>
</div>

我看过很多响应式 iframe 嵌入谷歌地图的例子,但是找不到在 div 中使 map 响应式的方法...


文件

Here are the files of a demo, the map is in the payment section

最佳答案

你可以修改下面的css:

.container-map{
      width:625px;
}
#map{
     width:100%; /* if not working, use !important*/
}

关于javascript - 在 div 中时使我的 Google map 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28666651/

相关文章:

javascript - 跨子域使用 localStorage

javascript - d3.csv显示 "XMLHttpRequest cannot load "错误

javascript - jquery 用户界面错误 "undefined is not a function"

php - 在 Concrete5 中定位模板 CSS 文件

javascript - 在生产环境中以最低权限运行 sails.js

javascript - JQuery Unique() 函数无法正常工作

javascript - 确定:input type in jQuery

html - 为什么我的 css Class 应用不一致?

css - 媒体查询中的最大宽度什么都不做

javascript - 正确使用 JQuery 的 prepend(...) 函数