0

オートフィル検索機能はマップ上でうまく機能しており、検索された住所にズームしてカスタムピンを配置しますが、ピンはすべてのズームレベルにとどまります。私の目標は、ユーザーが過去のレベル18のズームアウトをズームアウトしたときにピンを削除/隠すことです。私の推測では、 'zoom_changed'のgoogle.maps.event.addListenerは動作するかもしれませんが、このコード内にどこに配置するかわかりません。事前に助けていただきありがとうございます。ズームアウト時にGoogleマップ検索アイコンを削除する方法はありますか?

// Create the search box and link it to the UI element. 
    var input = document.getElementById('pac-input'); 
    var searchBox = new google.maps.places.SearchBox(input); 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 

    // Bias the SearchBox results towards current map's viewport. 
    map.addListener('bounds_changed', function() { 
     searchBox.setBounds(map.getBounds()); 
    }); 

    var markersS = []; 
    // Listen for the event fired when the user selects a prediction and   
    // retrieve more details for that place. 
    searchBox.addListener('places_changed', function() { 
     var places = searchBox.getPlaces(); 

     if (places.length == 0) { 
     return; 
     } 

     // Clear out the old markersS. 
     markersS.forEach(function(marker) { 
     marker.setMap(null); 
     }); 
     markersS = []; 

     // For each place, get the icon, name and location. 
     var bounds = new google.maps.LatLngBounds(); 
     places.forEach(function(place) { 
     if (!place.geometry) { 
      console.log("Returned place contains no geometry"); 
      return; 
     } 
     var icon = { 
      url: '.../HOME.png', 
      anchor: new google.maps.Point(41.5, 64), 
     }; 

     // Create a marker for each place and also set Zoom Condition. 
     google.maps.event.addListener(map, 'zoom_changed', function() { 
     var zoomS = map.getZoom(); 
     if (zoomS>=18) { 
      markersS.push(new google.maps.Marker({ 
      map: map, 
      icon: icon, 
      title: place.name, 
      position: place.geometry.location, 
      //animation: google.maps.Animation.DROP 
      })); 
      } 
     else if (zoomS<18) { 
      markersS.forEach(function(marker) { 
      marker.setMap(null); 
      }); 
      markersS = []; 
      } 
     }); 

     if (place.geometry.viewport) { 
      // Only geocodes have viewport. 
      bounds.union(place.geometry.viewport); 
     } else { 
      bounds.extend(place.geometry.location); 
     } 
     }); 
     map.fitBounds(bounds); 

    }); 
+0

新しいIFを追加すると、すべてが機能しますが、新しいアドレスを検索すると家のアイコンが新しい場所にマークされますが、古い検索済みのホームアイコンは2つのアイコンを表示したままです。以前に検索したアイコンを削除し、新しい/現在のアイコンのみを読み込むコードに追加できるものはありますか? – ckingchris

答えて

-1

ズームはあなたのinitMap関数にあります。条件文を使うだけです。

+0

ちょっと分かりました。 if、else if関数を追加し、以下のコードに作業していました。 markersS.push(新しいgoogle.maps.Marker({ 地図マップ、 アイコン:アイコン、 タイトル:place.name、 位置:place.geometry.location、 アニメーション:google.maps.Animation.DROP } )); – ckingchris

+0

あなたはそれを持っています。 :) – tanwill

+0

おかげでツイル!上記のコードを更新しました。新しい問題が発生しました。新しいアドレスを検索すると、古いアイコンはまだ残っています。新しいアドレスが検索されたときにそれを完全に削除するステップがありませんか? – ckingchris

関連する問題