2016-02-12 76 views
-1

當我單擊更改標記位置時,最後位置的地圖信息窗口沒有刪除。我的代碼如下。例如,當我點擊谷歌地圖時,標記位置已經改變爲w-r-t到那個經緯度。信息窗口顯示lat,long。之後當我再次更改標記的位置並打開信息窗口時,它顯示lat,long。當我點擊關閉窗口時,它會顯示它後面的最後一個信息窗口。然後我需要關閉那個窗口。我該如何解決它?當點擊更改標記位置時,信息窗口不會刪除

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); 
    } 
+0

請提供一個[最小,完整,測試和可讀示例](http://stackoverflow.com/help/mcve)來說明問題,包括任何必需的HTML/CSS。 – geocodezip

+0

你只想要一個infowindow嗎? – geocodezip

回答

0

一種選擇是保留對信息窗口的引用,並關閉它,如果你在同一個信息窗口再次運行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打開,在全局上下文中創建一個infiveindow,並將其重用於任何被點擊的標記。