Google Maps API可以在iframe地图上获取地点文字

Pet*_*ete 0 google-maps google-maps-api-3 google-places-api

所以我找到了几种方法将地图放到我的页面上,第一个版本是使用iframe,如下所示:

<iframe src="https://www.google.com/maps/embed/v1/place?key=[APIKEY]&amp;q=place_id:[PlaceID]"></iframe>
Run Code Online (Sandbox Code Playgroud)

它给出了一个标记,如下图所示,标记旁边的位置名称:

在此输入图像描述

然而,这并不完美,因为它在左上方有一个巨大的白色盒子,根据很多SO帖子,你无法删除它.

所以我想我会尝试js路由,所以有以下代码:

var map = new google.maps.Map(this, {
  zoom: 15,
  center: { lat: options.latitude, lng: options.longitude }
});

marker = new google.maps.Marker({
  map: map,
  title: options.title
});

marker.setPlace({
  placeId: options.placeId,
  location: { lat: options.latitude, lng: options.longitude }
});

var infowindow = new google.maps.InfoWindow(
  {
    content: '<strong>' + options.title + '</strong><br />' + options.address
  });

google.maps.event.addListener(marker, 'click', function () {
  infowindow.open(map, marker);
});
Run Code Online (Sandbox Code Playgroud)

所有的东西options都填写适当的价值.但是,这会生成以下地图,因为它没有白框,所以更好:

在此输入图像描述

但是,第二张地图在地图上没有商店文字,即使它使用与第一张地图相同的地点ID.

有没有办法更改js代码,以便它包含iframe版本中的位置文本?我搜索了所有文档和示例,但找不到任何设置来执行此操作

geo*_*zip 5

您可以自己将该标签添加到地图中.一种选择是使用InfoBox:

function addMapLabel(text, latlng, map) {
  var labelText = "<b>" + text + "</b>";

  var myOptions = {
    content: labelText,
    boxStyle: {
      border: "none",
      textAlign: "center",
      fontSize: "8pt",
      width: "auto",
      color: "#800000"
    },
    disableAutoPan: true,
    pixelOffset: new google.maps.Size(10, -10),
    position: latlng,
    closeBoxURL: "",
    isHidden: false,
    pane: "mapPane",
    enableEventPropagation: true
  };

  var ibLabel = new InfoBox(myOptions);
  ibLabel.open(map);
}
Run Code Online (Sandbox Code Playgroud)

带有标签的Google Maps Javascript API v3地图

概念证明小提琴

代码段:

function addMapLabel(text, latlng, map) {
  var labelText = "<b>" + text + "</b>";

  var myOptions = {
    content: labelText,
    boxStyle: {
      border: "none",
      textAlign: "center",
      fontSize: "8pt",
      width: "auto",
      color: "#800000"
    },
    disableAutoPan: true,
    pixelOffset: new google.maps.Size(10, -10),
    position: latlng,
    closeBoxURL: "",
    isHidden: false,
    pane: "mapPane",
    enableEventPropagation: true
  };

  var ibLabel = new InfoBox(myOptions);
  ibLabel.open(map);
}
var map;
var options = {
  placeId: "ChIJ68TmaaTCe0gRy70pZDzQ17U",
  latitude: 53.7153659,
  longitude: -1.8790866,
  title: "Dickies Tiles",
  address: "Aachen Way, Halifax HX1 3ND, United Kingdom"
}

function initialize() {
  var map = new google.maps.Map(document.getElementById("map_canvas"), {
    zoom: 15,
    center: {
      lat: options.latitude,
      lng: options.longitude
    }
  });

  marker = new google.maps.Marker({
    map: map,
    title: options.title
  });

  marker.setPlace({
    placeId: options.placeId,
    location: {
      lat: options.latitude,
      lng: options.longitude
    }
  });

  var infowindow = new google.maps.InfoWindow({
    content: '<strong>' + options.title + '</strong><br />' + options.address
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map, marker);
  });
  addMapLabel(options.title, new google.maps.LatLng(options.latitude, options.longitude), map);
}
google.maps.event.addDomListener(window, "load", initialize);
Run Code Online (Sandbox Code Playgroud)
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/infobox/src/infobox.js"></script>
<div id="map_canvas"></div>
Run Code Online (Sandbox Code Playgroud)