【问题标题】:Google Maps Api - center around markerGoogle Maps Api - 以标记为中心
【发布时间】:2015-03-01 08:45:54
【问题描述】:

我正在尝试使地图以标记为中心,现在您几乎看不到左角的标记。

我的代码是:

 <script>
  function initialize() {
    var myLatlng = new google.maps.LatLng(<?php echo $hotelInformation['latitude']?>, <?php echo $hotelInformation['longitude']?>);
    var mapCanvas = document.getElementById('map-canvas');
    var mapOptions = {
        center: myLatlng,          
        zoom: 12,
    }
    var map = new google.maps.Map(mapCanvas, mapOptions);
    var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title:"<?php echo $hotelInformation['name']?>"
      });
    var latLng = marker.getPosition(); 
    map.setCenter(latLng);
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

尝试了多种方法使其居中,但没有成功 如何使地图居中以使标记位于中间?

【问题讨论】:

  • 你先谷歌了吗,这是很多stackoverflow.com/questions/6150409/…的重复... = map.setCenter();
  • @Mayhem,我已经尝试过您建议的链接并尝试通过谷歌地图 api 进行谷歌搜索。但我似乎仍然无法让它工作。我已经在顶部更新了我的代码 - 你能发现我做错了什么吗?
  • 您的代码看起来是正确的,请检查您的浏览器控制台是否有任何错误..
  • nothing wrong with the posted code(除了它不提供真正的纬度或经度)。你的 HTML/CSS 是什么样的?请提供一个 Minimal, Complete, Tested and Readable example 来证明该问题。

标签: javascript google-maps google-maps-api-3


【解决方案1】:

尝试在此页面上使用以下代码:http://www.w3schools.com/googleapi/tryit.asp?filename=tryhtml_map_first

<!DOCTYPE html>
<html>
<head>
<script
src="http://maps.googleapis.com/maps/api/js"></script>

<script>


window.initialize = function() {
    var myLatlng = new google.maps.LatLng(-28.0000,138.0000);
    var mapCanvas = document.getElementById('map-canvas');
    var mapOptions = {
        center: myLatlng,          
        zoom: 12,
    }
    var map = new google.maps.Map(mapCanvas, mapOptions);
    var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title:"123"
      });
    var latLng = marker.getPosition(); 
    map.setCenter(latLng);
  }
  google.maps.event.addDomListener(window, 'load', initialize);

</script>
</head>

<body>
<div id="map-canvas" style="width:500px;height:380px;"></div>

</body>
</html>

我建议 w3school 的原因,以及 jsfiddle 是为了让 gmaps 正常工作。

【讨论】:

  • 您不需要创建latLng 变量,您已经知道位置,只需使用map.setCenter(myLatlng);
  • 这是他的代码的副本,只是一个使用它的在线示例.. 减去必须复制 n 粘贴到 w3
  • ...那么它回答了他的问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-04
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 1970-01-01
  • 2013-04-11
  • 2016-08-19
相关资源
最近更新 更多