【问题标题】:Google maps custom control always appears below the default controls谷歌地图自定义控件总是出现在默认控件下方
【发布时间】:2014-01-21 18:30:34
【问题描述】:

我想向 Google Maps v3 地图添加自定义地图控件。我的自定义控件是下面屏幕截图中灰显的“位置”图标。

问题是我需要自定义控件位于平移('箭头')控件下方,但 高于街景小人/街景控件。我尝试在用于控件的 div 上设置“index = -3”(参见v3 custom control positioning docs),但没有成功。

wrapperDiv = document.createElement('div');
/* Some code appends an image to wrapperDiv in my actual code */
wrapperDiv.index = -3;
map.controls[google.maps.ControlPosition.LEFT_TOP].push( wrapperDiv );

有什么想法吗?

更新 - 找到解决方案

使用 geocodezip 提供的答案,我的自定义控件现在位于 pan 控件和 pegman 控件之间:

大多数控件现在比正常位置更靠左,但据我所知,似乎没有办法解决这个问题。

跟进问题

既然我的自定义控件在正确的位置,有没有办法让街景小人和缩放控件在平移控件下方居中,就像它们在第一个屏幕截图中一样?

【问题讨论】:

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


【解决方案1】:
  • 一个想法/选项(不是最佳的,但可以满足您的需求)。
  1. 将平移控件置于 TOP_LEFT 控件位置。
  2. 将自定义控件放在 LEFT_TOP 中的索引 -1(在所有普通控件之前)
  var mapOptions = {
    zoom: 12,
    center: chicago,
    disableDefaultUI: true,
    mapTypeControl: true,
    mapTypeControlOptions: {
        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
        position: google.maps.ControlPosition.RIGHT_TOP
    },
    scaleControl: true,
    panControl: true,
    panControlOptions: {
        position: google.maps.ControlPosition.TOP_LEFT
    },
    streetViewControl: true,
    streetViewControlOptions: {
        position: google.maps.ControlPosition.LEFT_TOP
    },
    zoomControl: true,
    zoomControlOptions: {
        position: google.maps.ControlPosition.LEFT_TOP
    }
  }

  map = new google.maps.Map(mapDiv, mapOptions);
  var homeControl = new HomeControl(homeControlDiv, map);

  map.controls[google.maps.ControlPosition.LEFT_TOP].push(homeControlDiv);

example

  • 第二个选项是创建自定义缩放控件,然后您可以控制顺序(我不知道如何访问预定义的 Google 地图控件,只是将自定义控件放在前面或在他们之后)。

example custom zoom/pan control 来自this question on SO

  var PanControl = new geocodezip.web.PanControl(map);
  PanControl.index = -2;
  var homeControl = new HomeControl(homeControlDiv, map);
  homeControlDiv.index = -1;
  map.controls[google.maps.ControlPosition.LEFT_TOP].push(PanControl);
  map.controls[google.maps.ControlPosition.LEFT_TOP].push(homeControlDiv);

Example with a modified ZoomPanControl (just the pan control)

使用上面示例中的代码编写 sn-p 代码:

var map = null;
var chicago = new google.maps.LatLng(41.850033, -87.6500523);

/**
 * The HomeControl adds a control to the map that simply
 * returns the user to Chicago. This constructor takes
 * the control DIV as an argument.
 * @constructor
 */
function HomeControl(controlDiv, map) {

  // Set CSS styles for the DIV containing the control
  // Setting padding to 5 px will offset the control
  // from the edge of the map
  controlDiv.style.padding = '5px';

  // Set CSS for the control border
  var controlUI = document.createElement('div');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '2px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = 'Click to set the map to Home';
  controlDiv.appendChild(controlUI);

  // Set CSS for the control interior
  var controlText = document.createElement('div');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '12px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = '<b>Home</b>';
  controlUI.appendChild(controlText);

  // Setup the click event listeners: simply set the map to
  // Chicago
  google.maps.event.addDomListener(controlUI, 'click', function() {
    map.setCenter(chicago)
  });

}

function initialize() {
  // Create the DIV to hold the control and
  // call the HomeControl() constructor passing
  // in this DIV.
  var homeControlDiv = document.createElement('div');


  var mapDiv = document.getElementById('map-canvas');
  var mapOptions = {
    zoom: 12,
    center: chicago,
    disableDefaultUI: true,
    mapTypeControl: true,
    mapTypeControlOptions: {
      style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
      position: google.maps.ControlPosition.RIGHT_TOP
    },
    scaleControl: true,
    panControl: false,
    streetViewControl: true,
    streetViewControlOptions: {
      position: google.maps.ControlPosition.LEFT_TOP
    },
    zoomControl: true,
    zoomControlOptions: {
      position: google.maps.ControlPosition.LEFT_TOP
    }
  }

  map = new google.maps.Map(mapDiv, mapOptions);
  var PanControl = new geocodezip.web.PanControl(map);
  PanControl.index = -2;
  var homeControl = new HomeControl(homeControlDiv, map);
  homeControlDiv.index = -1;
  map.controls[google.maps.ControlPosition.LEFT_TOP].push(PanControl);
  map.controls[google.maps.ControlPosition.LEFT_TOP].push(homeControlDiv);

}

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

/**
 * @param {string} tagName
 * @param {Object.<string, string>} properties
 * @returns {Node}
 */
function CreateElement(tagName, properties) {
  var elem = document.createElement(tagName);
  for (var prop in properties) {
    if (prop == "style")
      elem.style.cssText = properties[prop];
    else if (prop == "class")
      elem.className = properties[prop];
    else
      elem.setAttribute(prop, properties[prop]);
  }
  return elem;
}

/**
 * @constructor
 * @param {google.maps.Map} map
 */
function PanControl(map) {
  this.map = map;
  this.originalCenter = map.getCenter();

  var t = this;
  var panContainer = CreateElement("div", {
    'style': "position: relative; padding: 5px;"
  });

  //Pan Controls
  var PanContainer = CreateElement("div", {
    'style': "position: relative; left: 2px; top: 5px; width: 56px; height: 56px; padding: 5px; overflow: hidden;"
  });
  panContainer.appendChild(PanContainer);
  var div = CreateElement("div", {
    'style': "width: 56px; height: 56px; overflow: hidden;"
  });
  div.appendChild(CreateElement("img", {
    'alt': ' ',
    'src': 'http://maps.gstatic.com/intl/en_ALL/mapfiles/mapcontrols3d5.png',
    'style': "position: absolute; left: 0px; top: -1px; -moz-user-select: none; border: 0px none; padding: 0px; margin: 0px; width: 59px; height: 492px;"
  }));
  PanContainer.appendChild(div);

  div = CreateElement("div", {
    'style': "position: absolute; left: 0px; top: 19px; width: 18.6667px; height: 18.6667px; cursor: pointer;",
    'title': 'Pan left'
  });
  google.maps.event.addDomListener(div, "click", function() {
    t.pan(PanDirection.LEFT);
  });
  PanContainer.appendChild(div);
  div = CreateElement("div", {
    'style': "position: absolute; left: 37px; top: 19px; width: 18.6667px; height: 18.6667px; cursor: pointer;",
    'title': 'Pan right'
  });
  google.maps.event.addDomListener(div, "click", function() {
    t.pan(PanDirection.RIGHT);
  });
  PanContainer.appendChild(div);
  div = CreateElement("div", {
    'style': "position: absolute; left: 19px; top: 0px; width: 18.6667px; height: 18.6667px; cursor: pointer;",
    'title': 'Pan up'
  });
  google.maps.event.addDomListener(div, "click", function() {
    t.pan(PanDirection.UP);
  });
  PanContainer.appendChild(div);
  div = CreateElement("div", {
    'style': "position: absolute; left: 19px; top: 37px; width: 18.6667px; height: 18.6667px; cursor: pointer;",
    'title': 'Pan down'
  });
  google.maps.event.addDomListener(div, "click", function() {
    t.pan(PanDirection.DOWN);
  });
  PanContainer.appendChild(div);
  div = CreateElement("div", {
    'style': "position: absolute; left: 19px; top: 19px; width: 18.6667px; height: 18.6667px; cursor: pointer;",
    'title': 'Reset center'
  });
  google.maps.event.addDomListener(div, "click", function() {
    t.map.setCenter(t.originalCenter);
  });
  PanContainer.appendChild(div);

  return panContainer;
}

/** @param {PanDirection} direction */
PanControl.prototype.pan = function(direction) {
  var panDistance = 50;
  if (direction == PanDirection.UP || direction == PanDirection.DOWN) {
    panDistance = Math.round(this.map.getDiv().offsetHeight / 2);
    this.map.panBy(0, direction == PanDirection.DOWN ? panDistance : -1 * panDistance);
  } else {
    panDistance = Math.round(this.map.getDiv().offsetWidth / 2);
    this.map.panBy(direction == PanDirection.RIGHT ? panDistance : -1 * panDistance, 0);
  }
}

/** @enum */
var PanDirection = {
  LEFT: 0,
  RIGHT: 1,
  UP: 3,
  DOWN: 4
}

window["geocodezip"] = window["geocodezip"] || {};
window["geocodezip"]["web"] = window["geocodezip"]["web"] || {};
window["geocodezip"]["web"]["PanControl"] = PanControl;
html,
body,
#map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>

<div id="map-canvas"></div>

【讨论】:

  • 谢谢,成功了!正如您所提到的,我必须将平移控件设置为 TOP_LEFT,然后将街景和缩放控件设置为 LEFT_TOP。我的控件设置为使用 LEFT_TOP,索引为 -1。
  • 将索引设置为 -1 解决了我的问题!谢谢!
猜你喜欢
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
相关资源
最近更新 更多