2017-08-01 5 views
1

地名を入力としてGoogleマップのその場所の緯度および経度の位置にピンを落とす機能があります。また、ピンをビューポートに設定するために、latおよびlngの位置を使用して境界を設定します。すべて正常に動作していますが、新しいマーカーを追加している間に古いマーカーは消去されません。マーカー配列をクリアしましたが、機能しません。ここに私のコードはGoogle Map新しい地図を追加しているときにマーカーが消去されない

ある
var place = args.address; 

     var bounds = new google.maps.LatLngBounds(); 

     var icon = { 
      url: place.icon, 
      size: new google.maps.Size(71, 71), 
      origin: new google.maps.Point(0, 0), 
      anchor: new google.maps.Point(17, 34), 
      scaledSize: new google.maps.Size(25, 25) 
     }; 

     var geocoder = new google.maps.Geocoder(); 
     geocoder.geocode({ 
      'address': place.formatted_address 
     }, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       console.log(results); 

       var markers = []; 

       markers.forEach(function(marker) { 
       marker.setMap(null); 
      }); 

       markers.push(new google.maps.Marker({ 
        map: map.map, 
        icon: icon, 
        title: place.name, 
        position: results[0].geometry.location 
       })); 


       if (results[0].geometry.viewport) { 
        bounds.union(results[0].geometry.viewport); 
       } else { 
        bounds.extend(results[0].geometry.location); 
       } 

       map.map.fitBounds(bounds); 

      } else { 
       console.log("error") 
      } 
     }); 

答えて

0

 var place = args.address; 

     var bounds = new google.maps.LatLngBounds(); 

     var markers = []; // 

     var icon = { 
      url: place.icon, 
      size: new google.maps.Size(71, 71), 
      origin: new google.maps.Point(0, 0), 
      anchor: new google.maps.Point(17, 34), 
      scaledSize: new google.maps.Size(25, 25) 
     }; 

     var geocoder = new google.maps.Geocoder(); 
     geocoder.geocode({ 
      'address': place.formatted_address 
     }, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       console.log(results);     
       markers.forEach(function(marker) { 
       marker.setMap(null); 

      }); 
       markers = []; // Empty here 
       markers.push(new google.maps.Marker({ 
        map: map.map, 
        icon: icon, 
        title: place.name, 
        position: results[0].geometry.location 
       })); 


       if (results[0].geometry.viewport) { 
        bounds.union(results[0].geometry.viewport); 
       } else { 
        bounds.extend(results[0].geometry.location); 
       } 

       map.map.fitBounds(bounds); 

      } else { 
       console.log("error") 
      } 
     }); 
javascript google-maps goog 
これを試してみてください
関連する問題