Javascript 谷歌地图Api-以标记为中心

Javascript 谷歌地图Api-以标记为中心,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我试着把地图放在标记的中心,现在你几乎看不到左角的标记 我的代码是: <script> function initialize() { var myLatlng = new google.maps.LatLng(<?php echo $hotelInformation['latitude']?>, <?php echo $hotelInformation['longitude']?>); var mapCanvas = document.

我试着把地图放在标记的中心,现在你几乎看不到左角的标记

我的代码是:

 <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>

函数初始化(){
var mylatng=newgoogle.maps.LatLng(,);
var mapCanvas=document.getElementById('map-canvas');
变量映射选项={
中心:myLatlng,
缩放:12,
}
var map=new google.maps.map(mapCanvas,mapOptions);
var marker=new google.maps.marker({
职位:myLatlng,
地图:地图,
标题:“
});
var latLng=marker.getPosition();
地图设置中心(latLng);
}
google.maps.event.addDomListener(窗口“加载”,初始化);
我已经尝试了多种方法让它成为中心,但没有成功
如何在地图上居中,以便标记在中间?

尝试使用此页上的以下代码:


window.initialize=函数(){
var mylatng=新的google.maps.LatLng(-28.0000138.0000);
var mapCanvas=document.getElementById('map-canvas');
变量映射选项={
中心:myLatlng,
缩放:12,
}
var map=new google.maps.map(mapCanvas,mapOptions);
var marker=new google.maps.marker({
职位:myLatlng,
地图:地图,
标题:“123”
});
var latLng=marker.getPosition();
地图设置中心(latLng);
}
google.maps.event.addDomListener(窗口“加载”,初始化);

我之所以建议你去学校,是因为我在学校里很痛苦。。。。让gmap正常工作。

你是先用谷歌搜索的吗,这是这么多…=map.setCenter()@Mayhem,我已经尝试了从你建议的链接,并尝试谷歌它,通过谷歌地图api。但我似乎还是无法让它发挥作用。我已经在顶部更新了我的代码-你能发现我做错了什么吗?你的代码看起来是正确的,检查你的浏览器控制台是否有任何错误..有(除了它没有提供真实的纬度或经度)。你的HTML/CSS是什么样子的?请提供一个演示问题的示例。您不需要创建
latLng
变量,您已经知道位置,只需使用
map.setCenter(myLatlng)这是他的代码的副本,只是一个使用它的在线示例。。嗯,减去必须复制n粘贴到w3。。。这是否回答了他的问题?
<!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>