谷歌地图setCenter使用标记不能正常工作,因为它显示市场在左上角而不是中间

Google maps setCenter using marker is not working properly as its is showing the market at left-top instead of center

本文关键字:显示市 因为 左上角 中间 工作 setCenter 不能 常工作 谷歌地图      更新时间:2023-09-26

谷歌地图setCenter使用标记不能正常工作,因为它显示市场在左上角而不是中心。下面是我使用的代码:

<body id="ViewMapScreen">
<div id="map" style="width: 600px; height: 349px;"></div>
   <script type="text/javascript"> 
   var address = 'Pune, Maharastra, India';
   var map = new google.maps.Map(document.getElementById('map'), { 
       mapTypeId: google.maps.MapTypeId.ROADMAP,
       zoom: 8
   });   
   var geocoder = new google.maps.Geocoder();
   geocoder.geocode({
      'address': address
   },
   function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
         new google.maps.Marker({
            position: results[0].geometry.location,
            map: map
         });
         map.setCenter(results[0].geometry.location);
      }
   });   
   </script> 
</body>

改变div的宽度和高度,然后它会正常工作。

<div id="map" style="width: 1270px; height: 850px;"></div>
相关文章: