单击时更改标记位置时,不会删除信息窗口

时间:2016-02-12 12:16:39

标签: javascript google-maps google-maps-api-3

当我在点击时更改标记位置时,最后位置的地图信息窗口未删除。我的代码如下。例如,当我点击谷歌地图时,标记位置会被w-r-t更改为该纬度和长度。信息窗口显示latlong。然后,当我再次更改标记的位置并打开信息窗口时,它会显示latlong。当我点击关闭窗口时,它会显示它背后的最后一个信息窗口。然后我需要关闭那个窗口。我该如何解决?

var marker2 = "";
function customMap(ulat,ulong,blat,blong) 
{
      //console.log(params);
      var map_div = "map-canvas";
      // var map_div = params.mapDiv;

      var myLatlng = new google.maps.LatLng(ulat, ulong);
      var myLatlng1 = new google.maps.LatLng(blat,blong);
      var loc_latlng = new google.maps.LatLng(23.8212783,90.4278342);
      var mapOptions = {
        zoom: 17,
        center: myLatlng1
      }
      var map = new google.maps.Map(document.getElementById( map_div ), mapOptions);


      var content_data = "<center>User</center>";
      var content_data3 = "<center>Location</center>";
      var content_data2 = "<div><strong>Location</strong><br>Lat:&nbsp;"+blat+"<br>Long:&nbsp;"+blong+"<br>Click on Map to change location<br><a class='del_icon' onclick='DeleteMarker();'>Delete Location</a></div>";
     // show full map button 
     map.controls[google.maps.ControlPosition.TOP_RIGHT].push(
     FullScreenControl(map));

      var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        icon: this_domain_cp+"/mvc/images/user_map_icon.png",
        //title: 'User'
      });
      attachMessage(marker, content_data);

      marker2 = new google.maps.Marker({
        position: myLatlng1,
        map: map,
        //title: 'building'
      });
      attachMessage(marker2, content_data2);

      marker3 = new google.maps.Marker({
        position: loc_latlng,
        map: map,
        icon: this_domain_cp+"/mvc/images/loc_icon.png",
        title: 'Location'
      });
      attachMessage(marker3, content_data3);

    //update lat long of building (red marker)
    google.maps.event.addListener(map, "click", function (event) {

        var blat = event.latLng.lat();
        var blong = event.latLng.lng();

        var myLatlng1 = new google.maps.LatLng(blat, blong);
        var newloc = marker2.setPosition(myLatlng1);

        var content_data2 = "<div><strong>Location</strong><br>Lat:&nbsp;"+blat+"<br>Long:&nbsp;"+blong+"<br>Click on Map to change location<br><a class='del_icon' onclick='DeleteMarker();'>Delete Location</a></div>";
        attachMessage(marker2, content_data2);
        infowindow.close();

        $('#building_latitude').val(blat);
        $('#building_longitude').val(blong);


    });

}

    function attachMessage(marker, content_data) {
        //    var message = 'This is my';
        var infowindow = new google.maps.InfoWindow({
            content: content_data
        });

        google.maps.event.addListener(marker, 'mouseover', function() {
            infowindow.open(marker.get('map'), marker);
        });

    }


    function DeleteMarker(id) {
        marker2.setMap(null);
        $('#building_latitude').val(0);
        $('#building_latitude').attr("disabled","disabled");
        $('#building_longitude').attr("disabled","disabled");
        $('#building_longitude').val(0);
    }

1 个答案:

答案 0 :(得分:0)

一个选项是保持对infowindow的引用,如果在同一个infowindow上再次运行attachMessage函数,则关闭它:

function attachMessage(marker, content_data) {
    //    var message = 'This is my';
    if (marker.infowindow) {
       marker.infowwindow.close();
    }
    var infowindow = new google.maps.InfoWindow({
        content: content_data
    });
    marker.infowindow = infowindow;
    google.maps.event.addListener(marker, 'mouseover', function() {
        infowindow.open(marker.get('map'), marker);
    });

}

proof of concept fiddle

如果您只想打开一个infowindow,请在全局上下文中创建一个,并将其重用于任何单击的标记。