Google Maps Api从标记中获取标题 [英] Google Maps Api getting the title from the marker

查看:114
本文介绍了Google Maps Api从标记中获取标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道这是有效的,因为当我使用console.log的时候 console.log(markersArray); 它返回以下内容。标记的标题是隔壁。




下面是我的标记点击,打开信息窗口,但是当我安慰时。注销名为 mll 的数据,它里面没有标题。

  google.maps.event.addListener(marker,'click',function(mll){
console.log(mll);
var html =< div style ='color :#000; background-color:#fff; padding:5px; width:150px;'>< p>+ mll +< / p>< / div>;
iw = new google .maps.InfoWindow({content:html});
iw.open(map,marker);
});



如何获得点击函数来拉入标题当数组有它并推动它成功时

解决方案

我记得之前做过类似的事情。在我的情况下,我用了圆圈标记的infoWindows。实质上,我有两个单独的数组。当我制作圆形标记时,我给了它一个独特的值,称为place,它基本上是count(创建的 n 圈的值,是 n )) 。在事件监听器上,我根据当前圆圈的位置从另一个数组中调用了infoWindow。

您可以制作一个数组 var titles = []; 来存放标题。
每当您制作一个新标记时,都会增加一个 var count = 0; ,记录您拥有多少标记。
在您的标记选项中,添加 place:count 。当您需要特定标题时,您可以调用 titles [marker.place];

  var infos = []; 
var count = 0;

var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);

var lastWindow;

count ++;
var populationOptions = {
//保留缺省值
地方:count
};

var circle = new google.maps.Circle(populationOptions);
lastCircle = circle;

var contentString ='只是一个字符串...';

var infowindow = new google.maps.InfoWindow({
content:contentString,
position:new google.maps.LatLng(data.lon,data.lad)
});

infos.push(infowindow);

google.maps.event.addListener(circle,'mouseover',function(){
if(lastWindow){
lastWindow.close();
}

infos [circle.place] .open(map);
lastWindow = infos [circle.place];
});


When I push a marker to the array it plots the marker correctly and adds a title to the array too.

I know this works because when I console.log the console.log(markersArray); it returns the following. the title of the marker is next door.

The below is my marker click that opens up the info window, but when I console.log out the data which is called mll it doesn't have the title inside it.

google.maps.event.addListener(marker, 'click', function(mll) {
    console.log(mll);
    var html= "<div style='color:#000;background-color:#fff;padding:5px;width:150px;'><p>"+mll+"</p></div>";
    iw = new google.maps.InfoWindow({content:html});
    iw.open(map,marker);
});

How would I be able to get the click function to pull in the title when the array has it and has pushed it successfully?

解决方案

I remember doing something similar before. In my case, I used infoWindows with circle markers. Essentially, I had both in separate arrays. When I made the circle marker, I gave it a unique value, called place, which was basically it's count (the value of the n-th circle created, was n). On the event listener, I called the infoWindow from the other array based on the position of the current circle.

You can make an array var titles = []; to hold titles. Each time you make a new marker, increment a var count = 0;, keeping track of how many markers you have. In your marker options, add place: count. When you need a specific title, you can call titles[marker.place];

  var infos = []; 
  var count = 0; 

  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var lastWindow; 

  count++;
  var populationOptions = {
    //leaving out defaults
    place: count
  };

  var circle = new google.maps.Circle(populationOptions);
  lastCircle = circle; 

  var contentString = 'just a string...';

  var infowindow = new google.maps.InfoWindow({
    content: contentString,
    position: new google.maps.LatLng(data.lon, data.lad)
  });

  infos.push(infowindow); 

  google.maps.event.addListener(circle, 'mouseover', function() {
    if(lastWindow){
      lastWindow.close(); 
    }

    infos[circle.place].open(map);
    lastWindow = infos[circle.place]; 
  });

这篇关于Google Maps Api从标记中获取标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆