首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >javascript -向gmap标记添加文本

javascript -向gmap标记添加文本
EN

Stack Overflow用户
提问于 2012-09-08 10:45:13
回答 1查看 3.2K关注 0票数 1

我使用gmap进行地理定位,即在google地图上设置特定位置的标记。现在我想要实现的是设置标记,只要用户单击其中一个标记,信息窗口就会打开并显示特定的文本。每个标记都有自己的文本。

现在的问题是,我无法确定用户单击了哪个标记,因此无法设置正确的文本。

下面是一个代码片段:

代码语言:javascript
复制
    //**Global variables**/
    var thingsOfInterest = new Array(new Array(), new Array(),new Array());
    var map = null;
    //End Global variables

    //init google map

function initGoogleMaps(){
    map = new google.maps.Map(document.getElementById("map_canvas"));
    var centerMap = new google.maps.LatLng(48.337881,14.320323);
    $('#map_canvas').gmap({'zoom':7,'center': centerMap});
    $('#map_canvas').gmap('option', 'zoom', 10);

   //This is not working on my ios-simulator, no idea why. Anyway....
   forge.geolocation.getCurrentPosition(function(position) {
            alert("Your current position is: "+position);
         }, function(error) {
    });
}
/*End init map*/

/*Set the markers on the map*/
function setMarkers() {
    /* thingsOf Interest contains:
     * thingsOfInterest[i][0] -> the text that the marker should hold
     * thingsOfInterest[i][1] -> the latitude
     * thingsOfInterest[i][2] -> the longitude
     */

    for (var i = 0; i < thingsOfInterest.length; i++) { //Iterate through all things
      var item = thingsOfInterest[i];  //get thing out of array
      var itemLatLng = new google.maps.LatLng(item[1], item[2]);  

      $('#map_canvas').gmap('addMarker', {'position': new google.maps.LatLng(item[1],item[2]) } ).click(function(e) {
                    $('#map_canvas').gmap('openInfoWindow', {'content': 'dummyContent'}, this); ///////SET REAL CONTENT HERE
      });
    }

}

现在,这一切都很好,但我错过的是获得用户在函数()-eventHandler中单击的标记。如果我能得到特定的标记,我就可以在上面设置文本。

我希望这是足够清楚的。

任何帮助都是非常感谢的。

谢谢,

EN

回答 1

Stack Overflow用户

发布于 2012-09-08 10:52:27

假设您的代码与虚拟文本是有效的,您可以立即传递您的文本。

代码语言:javascript
复制
$('#map_canvas').gmap('addMarker', {'position': new google.maps.LatLng(item[1],item[2])})
   .click(function(e) {
      $('#map_canvas').gmap('openInfoWindow', {'content': item[0]}, this); 
   });

或者另一种办法是:

代码语言:javascript
复制
function setMarkers() {

    for (var i = 0; i < thingsOfInterest.length; i++) {

        var item = thingsOfInterest[i];  
        var itemLatLng = new google.maps.LatLng(item[1], item[2]);

        var marker = new google.maps.Marker({ position: itemLatLng, map: map });

        google.maps.event.addListener(marker, 'click', function () {
            var infowindow = new google.maps.InfoWindow({ content: item[0] });
            infowindow.open(map, marker);
        });
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12330016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档