Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在谷歌地图上直接放置文本标签_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 如何在谷歌地图上直接放置文本标签

Javascript 如何在谷歌地图上直接放置文本标签,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我有一个关于谷歌JavaScript地图API的问题 当在maps.google.com(即星巴克)上搜索地点时,谷歌地图会在其旁边添加一个标记,该标记的文字为Starbucks: 是否可以对自定义标记和文本执行相同的操作?我能够看到如何通过,但我无法找到如何添加标签文本。我在API中找到的示例是使用Info窗口,但这对我来说有点笨重。是扩展Google Maps JavaScript API V3Google.Maps.Marker类的一部分,允许您使用关联标签定义标记 正如您所料,如果标记

我有一个关于谷歌JavaScript地图API的问题

当在maps.google.com(即星巴克)上搜索地点时,谷歌地图会在其旁边添加一个标记,该标记的文字为Starbucks:

是否可以对自定义标记和文本执行相同的操作?我能够看到如何通过,但我无法找到如何添加标签文本。我在API中找到的示例是使用Info窗口,但这对我来说有点笨重。

是扩展Google Maps JavaScript API V3
Google.Maps.Marker
类的一部分,允许您使用关联标签定义标记

正如您所料,如果标记是可拖动的,那么标签也是可拖动的。此外,带有标签的标记以与常规标记相同的方式响应所有鼠标事件。它还触发鼠标事件和“属性更改”事件,就像常规标记一样

创建基本标记 下面的示例显示了如何使用MarkerWithLabel创建一个标记,该标记在一个小框中,其下方有一个居中的标签。通过为label DIV定义一个具有所需属性的CSS类,可以最容易地设置标签的样式。在本例中,该类称为“labels”,该名称在labelClass参数中传递给MarkerWithLabel。可以在labelStyle参数中传递其他样式信息。标签的文本在labelContent中传递。可以传递给MarkerWithLabel的其他参数与可以传递给google.maps.Marker的参数相同

 <style type="text/css">
   .labels {
     color: red;
     background-color: white;
     font-family: "Lucida Grande", "Arial", sans-serif;
     font-size: 10px;
     font-weight: bold;
     text-align: center;
     width: 40px;     
     border: 2px solid black;
     white-space: nowrap;
   }
 </style>

     var latLng = new google.maps.LatLng(49.47805, -123.84716);
     var homeLatLng = new google.maps.LatLng(49.47805, -123.84716);

     var map = new google.maps.Map(document.getElementById('map_canvas'), {
       zoom: 12,
       center: latLng,
       mapTypeId: google.maps.MapTypeId.ROADMAP
     });

     var marker1 = new MarkerWithLabel({
       position: homeLatLng,
       draggable: true,
       raiseOnDrag: true,
       map: map,
       labelContent: "$425K",
       labelAnchor: new google.maps.Point(22, 0),
       labelClass: "labels", // the CSS class for the label
       labelStyle: {opacity: 0.75}
     });

     var iw1 = new google.maps.InfoWindow({
       content: "Home For Sale"
     });
     google.maps.event.addListener(marker1, "click", function (e) { iw1.open(map, this); });

.标签{
颜色:红色;
背景色:白色;
字体系列:“Lucida Grande”,“Arial”,无衬线;
字体大小:10px;
字体大小:粗体;
文本对齐:居中;
宽度:40px;
边框:2件纯黑;
空白:nowrap;
}
var latLng=新的google.maps.latLng(49.47805,-123.84716);
var homeLatLng=新的google.maps.LatLng(49.47805,-123.84716);
var map=new google.maps.map(document.getElementById('map_canvas'){
缩放:12,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var marker1=新的MarkerWithLabel({
职位:homeLatLng,
真的,
是的,
地图:地图,
标签内容:“42.5万美元”,
labelAnchor:新的google.maps.Point(22,0),
labelClass:“labels”,//标签的CSS类
标签样式:{不透明度:0.75}
});
var iw1=新的google.maps.InfoWindow({
内容:“待售房屋”
});
google.maps.event.addListener(marker1,“click”,函数(e){iw1.open(map,this);});

这个特性在API文档中有很好的记录:我不是在问这个标记。我正在询问标记旁边的标签文本。谢谢@Dr.Molle这正是我想要的。元讨论。