Trigger event with infoWindow or InfoBox on click Google Map API V3

前端 未结 5 696
遥遥无期
遥遥无期 2020-11-30 08:20

I want to know how to trigger an event when I click on an infoWindow using Google Maps API v3. In this example, when I click on a marker, an info window shows up with a uni

5条回答
  •  被撕碎了的回忆
    2020-11-30 08:41

    Thanx! Erin is tha man! You fixed my problem! I was bashing it for 4 days now! Closures are a pain in the * if you are new to them.

    Here's is my working code (Sencha Touch 2 and Google maps API v3). It will oly allow one infowindow to be open.

    var openedInfoWindow = null;
    var boxList = [];
    var marker, i;
    
    //build info box object
    var infoWindow = new google.maps.InfoWindow({
        enableEventPropagation: true,
    });
    
    //handle close
    google.maps.event.addListener(infoWindow, 'closeclick', function() {
        openedInfoWindow = null;
    });
    
    //loop trough store data...
    for(i in data) {
        //data
        var itemData = data[i].getData();
    
        //marker
        var geopos = new google.maps.LatLng(itemData['lat'], itemData['lng']);
        var marker = new google.maps.Marker({
            position: geopos,
            //icon: image,
            map: map,
            title: itemData['title'],
            id: i,
            animation: google.maps.Animation.DROP
        });
    
        //add info window content to DOM
        var boxText = document.createElement("div");
        boxText.id = i;
        boxText.className = "labelText" + i;
        boxText.data = itemData;
        boxText.innerHTML = '' + itemData['title'] + '' + (itemData['distance'] ? ' (' + itemData['distance'] + ')' : '') + '
    ' + (itemData['address'] != itemData['title'] ? itemData['address'] : '') + (itemData['price'] ? '
    ' + itemData['price'] : '') + '
    Meer info'; boxList.push(boxText); //add marker click event google.maps.event.addListener(marker, 'click', (function(marker, i) { if (openedInfoWindow != null) openedInfoWindow.close(); return function() { infoWindow.setContent(boxList[this.id]); infoWindow.open(map, marker); openedInfoWindow = infoWindow; } })(marker, i)); //when infowindow is clicked, open view... google.maps.event.addDomListener(boxList[i],'click',(function(marker, i) { return function(){ iStuddy.app.pushView('kames_detail',boxList[i].data); } })(marker, i)); }

提交回复
热议问题