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,我有一个谷歌地图,我想它显示不同的经销商,现在我有地图工作完美的标记落在地方的一切,但我想添加一个独特的标题,每个标记,但我正在努力得到正确的,如果我添加标题选项,然后标题显示在所有标记相同 所以我需要一种在每个标记上显示唯一标题的方法,这是我的代码: //如果要添加多个标记,可能需要将它们放到地图上 //连续地而不是一下子。此示例演示如何使用 //setTimeout()来分隔标记的动画。 var centerSA=new google.maps.LatLng(-30.289471,24.2

我有一个谷歌地图,我想它显示不同的经销商,现在我有地图工作完美的标记落在地方的一切,但我想添加一个独特的标题,每个标记,但我正在努力得到正确的,如果我添加标题选项,然后标题显示在所有标记相同

所以我需要一种在每个标记上显示唯一标题的方法,这是我的代码:

//如果要添加多个标记,可能需要将它们放到地图上
//连续地而不是一下子。此示例演示如何使用
//setTimeout()来分隔标记的动画。
var centerSA=new google.maps.LatLng(-30.289471,24.284893);
var邻域=[
新的google.maps.LatLng(-33.880754,18.634368),
新的google.maps.LatLng(-33.873804,18.634843),
新的google.maps.LatLng(-34.112384,18.844713),
新的google.maps.LatLng(-34.082823,18.840496),
新的google.maps.LatLng(-29.492389,31.218657),
新的google.maps.LatLng(-33.863724,18.578321),
新的google.maps.LatLng(-29.857765,31.021630),
新的google.maps.LatLng(-26.028311,28.004132),
新的google.maps.LatLng(-34.031815,23.045850),
新的google.maps.LatLng(-34.053906,23.370542),
新的google.maps.LatLng(-33.963073,22.459156),
新google.maps.LatLng(-32.989910,27.872604),
新的google.maps.LatLng(-33.302933,26.523438),
新的google.maps.LatLng(-28.76880532.019887),
新的google.maps.LatLng(-27.645871,27.229928),
新的google.maps.LatLng(-28.236912,28.308702),
新的google.maps.LatLng(-25.8722229.235887),
新的google.maps.LatLng(-27.751827,29.936295),
新的google.maps.LatLng(-28.734564,24.756509),
新的google.maps.LatLng(-25.656175,27.255232),
新的google.maps.LatLng(-26.956302,24.728201),
新的google.maps.LatLng(-26.964996,26.659299),
新的google.maps.LatLng(-26.808988,27.829341),
新的google.maps.LatLng(-26.489940,28.383779),
新的google.maps.LatLng(-26.459522,29.465437),
新的google.maps.LatLng(-28.411019,21.251776),
新的google.maps.LatLng(-22.556783,17.065234),
新的google.maps.LatLng(-22.95126814.515005),
新的google.maps.LatLng(-34.408175,19.253408),
新google.maps.LatLng(-28.803741,24.707523)
];
var标记=[];
var映射;
函数初始化(){
变量映射选项={
缩放:6,
中心:中心
};
map=new google.maps.map(document.getElementById('map-canvas'),mapOptions);
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
initialLocation=new google.maps.LatLng(position.coords.lation,position.coords.longitude);
地图设置中心(初始位置);
});
}
}
函数drop(){
clearMarkers();
for(var i=0;i

html,正文,#地图画布{
身高:100%;
保证金:0;
填充:0;
}
#面板{
位置:绝对位置;
顶部:5px;
左:50%;
左边距:-180px;
z指数:5;
背景色:;
填充顶部:55px;
}
/*
为ID和类提供以下样式,
其中ID表示实际存在的“面板”,带有
JS绑定到它的名称,而该类只是非映射
可能已与具有不同ID的内容
JS绑定到它的名称。
*/

展示经销商


将标题添加到您的
社区
数组中,并使其成为数组的数组:

var neighborhoods = [
 [new google.maps.LatLng(-33.880754, 18.634368), "title 1"],
 [new google.maps.LatLng(-33.873804, 18.634843), "title 2"],
 [new google.maps.LatLng(-34.112384, 18.844713), "title 3"],
 [new google.maps.LatLng(-34.082823, 18.840496), "title 4"],
...
然后将函数
addMarkerWithTimeout
更改为接受
title
param,并在创建标记时添加该参数:

function addMarkerWithTimeout(position, timeout, title) {
  window.setTimeout(function() {
    markers.push(new google.maps.Marker({
      position: position,
      map: map,
      animation: google.maps.Animation.DROP,
      title: title
    }));
  }, timeout);
}
最后,将函数
drop
更改为在
addMarkerWithTimeout
函数调用中包含
title
(邻域[i][1]):

function drop() {
  clearMarkers();
  for (var i = 0; i < neighborhoods.length; i++) {
    addMarkerWithTimeout(neighborhoods[i][0], i * 200, neighborhoods[i][1]);
  }
}
函数drop(){
clearMarkers();
for(var i=0;i
还要注意,
位置
参数现在是
邻里[i][0]
,而不是
邻里[i]


工作提琴:

为什么不创建一个数组来保存每个标记的坐标和标题?类似这样的:伊万·乔沃维奇一针见血,非常感谢大家的帮助,我今天学到了很多。相关问题:是的,但不是,我读过这个问题,它完全不是我想要的,因此,我尽了最大努力将其放在JSFIDLE中,以便人们能够看到我试图完成的任务,而伊万·约沃维奇的回答正好是我需要的,而且远没有那么复杂。@ErikThiart:如果我的帖子回答了你的问题,请注明:啊,对不起,我错了,我是一个noob,我是新来的,多复杂的组合:),呵呵,再次谢谢你。