2017-04-14 12 views
0

JavaScript 및 Google지도를 사용하여 마커를 표시하고 숨기려면 어떻게해야합니까?Google지도는 체크 박스를 사용하여 마커를 보이거나 숨 깁니다.

마커를 표시하고 숨기려면 확인란을 사용하고 싶습니다. 분당 두 개의 마커와 세 개의 체크 박스가 있습니다. 하나는 모두 표시하고 숨기고 다른 하나는 각 표식을 표시하거나 숨 깁니다. 나는 그들을 체크 박스에 연결하고 그것들을 보이거나 숨길 수있는 방법을 모르겠다.

var Jorvik = createMarker({ 
     position: {lat: 53.95697, lng: -1.08100}, 
    map: map, 
     icon: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'}, 
      "<h1>JORVIK Viking Centre</h1><p>This is the home marker.<br/>test</p>"); 

codepen : 내가 사용하고 마커의

샘플 https://codepen.io/mike43237/pen/qmEVLE

답변

0

당신은 Working Fiddle

가 toggleGroup() 함수를

+0

내 마커가 동일한 방식으로 작동합니까? 그들은 다르게 창조 된 것 같습니다. – 3245737

+0

예! 그것을 전역 배열로 집어 넣고 루프를 통과시키고 각 마커에 대해 setVisible (true/false) 함수를 사용하고 다른 마커에 대해 마찬가지로 마커를 사용합니다. – Dee

0

사용을 참조 마커 setVisible(true/false) 방법을 사용할 수 있습니다 marker.setVisible 함수를 true 또는 false로 설정합니다.

구글 맵

iMap.initialize('map'); 
var Jorvik = iMap.markerCreate('title', 53.95697, -1.08100,true); 

사용자 정의 클래스 (당신이 당신의 옵션을 설정할 수 있습니다) :

$('#your_checkbox_id').change(function() {  
    Jorvik.setVisible($(this).is(":checked"));    
}); 

이 구글 맵을 작성

var iMap = { 
marker: null, 
initialize: function(mapElementId) { 
    var mapOptions = { 
     zoom: 15, 
     streetViewControl: this.streetViewControl, 
     scrollwheel: this.scrollwheel, 
     navigationControl: this.navigationControl, 
     minZoom: this.minZoom, 
     maxZoom: this.maxZoom, 
     center: new google.maps.LatLng(53.95697, -1.08100), 
     mapTypeId: 'Styled', 
     mapTypeControlOptions: { 
      mapTypeIds: ['Styled', google.maps.MapTypeId.HYBRID] 
     } 
    };    
    var styelMap = [ 
     { 
      featureType: 'poi', 
      elementType: 'geometry', 
      stylers: [ 
       { hue: '#ffffff' }, 
       { saturation: -100 }, 
       { lightness: 100 }, 
       { visibility: 'on' } 
      ] 
     }, { 
      featureType: 'landscape', 
      elementType: 'all', 
      stylers: [ 
       { hue: '#ffffff' }, 
       { saturation: -100 }, 
       { lightness: 100 }, 
       { visibility: 'on' } 
      ] 
     }, { 
      featureType: 'transit', 
      elementType: 'geometry', 
      stylers: [ 
       { hue: '#ffffff' }, 
       { saturation: 0 }, 
       { lightness: 100 }, 
       { visibility: 'on' } 
      ] 
     }, { 
      featureType: 'administrative', 
      elementType: 'geometry', 
      stylers: [ 
       { hue: '#ffffff' }, 
       { saturation: 0 }, 
       { lightness: 100 }, 
       { visibility: 'on' } 
      ] 
     } 
    ]; 

    this.map = new google.maps.Map(document.getElementById(mapElementId), mapOptions); 
    this.map.mapTypes.set('Styled', new google.maps.StyledMapType(styelMap, { name: 'Compact map' }));  

}, 
markerCreate: function (title, lat, lng ,draggable ) {    
    this.marker= new google.maps.Marker({ 
      map: this.map, 
      title: title, 
      position: new google.maps.LatLng(lat, lng), 
      animation: google.maps.Animation.DROP, 
      draggable: draggable 

     });  
    google.maps.event.addListener(pin, "drag", function() { 
     // $('#lat').html(pin.position.lat()); 
     // $('#lng').html(pin.position.lng()); 
    }); 

    return this.marker; 


} 

}

+0

체크 박스를 다시 클릭하면 어떻게 표시됩니까? – 3245737

+0

"createMarker"기능이 작동하지 않는다고 생각합니다. –

+0

오, 이제 막 Google지도 문서를 읽었습니다. 내 코드를 향상시키는 방법에 대한 제안이 있으십니까? – 3245737

관련 문제