javascript 用 panby 偏移谷歌地图中心

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17932054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:09:19  来源:igfitidea点击:

Offset google map center with panby

javascriptgoogle-maps

提问by Xav

I'm sure this is a stupidly simple question but I'm struggling with it :S

我确定这是一个愚蠢而简单的问题,但我正在努力解决它:S

I want to offset my map center using panByas mentioned in this questionbut I'm not entirely sure where to place mapObject.panBy(0,30)or if I need to change the bit that says mapObject

我想使用这个问题中panBy提到的方法来偏移我的地图中心,但我不完全确定该放在哪里,或者我是否需要更改表示 mapObject 的位mapObject.panBy(0,30)

Here is my code so far:

到目前为止,这是我的代码:

function initialize()
{

var mapProp = {
    center:new google.maps.LatLng<?php echo $entrylatlng;?>,
    zoom:14,
    scrollwheel: false,
    mapTypeId:google.maps.MapTypeId.ROADMAP
    };

var map=new google.maps.Map(document.getElementById("googleMap") ,mapProp);

var contentString = 
        if( gmapsstring.gmapaddresspostcode.length > 0 ) {
        contentString += '<p>' + gmapsstring.gmapaddresspostcode + '</p>';
        };

var infowindow = new google.maps.InfoWindow({
        content: contentString
});

var point = new google.maps.LatLng<?php echo $entrylatlng;?>;
var marker = new google.maps.Marker({
      position: point,
      map: map
    });
google.maps.event.addListener(marker, 'click', function() {
     infowindow.open(map,marker);
});
infowindow.open(map,marker);
}

google.maps.event.addDomListener(window, 'load', initialize); 

Sorry for the reams of code, I'm not sure what needs to be included. Thanks for any help.

很抱歉有大量代码,我不确定需要包含哪些内容。谢谢你的帮助。

回答by burnedikt

Basically you can start panning directly after you initialized the map. So your initialize function might look like this:

基本上,您可以在初始化地图后直接开始平移。所以你的初始化函数可能是这样的:

function initialize(){
  var mapProp = {
    center: new google.maps.LatLng<?php echo $entrylatlng;?>,
    zoom: 14,
    scrollwheel: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(document.getElementById("googleMap") ,mapProp);
  // start panning
  map.panBy(0, 30);

  var contentString = 
    if( gmapsstring.gmapaddresspostcode.length > 0 ) {
    contentString += '<p>' + gmapsstring.gmapaddresspostcode + '</p>';
  };
  /**
  * the rest of your code goes here
  * ...
  */
}