2017-04-08 181 views
-3

我一直有试图隐藏/清除我的地图上的标记的问题。我已经看过Google的文档,并且我有正确的代码,我也在这里看过并看到了一些答案,但似乎没有任何工作对我来说,我添加了一个清晰的地图按钮并创建了一个函数,但是这没有工作。谷歌地图,无法清除/隐藏标记

另外,我尝试添加在地理位置方面,但也没有工作可言,但是当我使用的代码谷歌的API给它只是不是我用我的代码来实现它

我不知道我是否在错误的地方得到了代码,或者没有,请帮助。

var map; 
    var infoWindow; 
    var service; 


    function initMap() { 

     map = new google.maps.Map(document.getElementById('map'), { 
      center: {lat: 51.545213, lng: -0.0307699}, 
      zoom: 15, 
      styles: [{ 
       stylers: [{visibility: 'simplified'}] 
      }, { 
       elementType: 'labels', 
       stylers: [{visibility: 'on'}] 
      }] 
     }); 

     var input = /** @type {!HTMLInputElement} */(
       document.getElementById('pac-input')); 

     var types = document.getElementById('type-selector'); 
     map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 
     map.controls[google.maps.ControlPosition.TOP_LEFT].push(types); 

     var autocomplete = new google.maps.places.Autocomplete(input); 
     autocomplete.bindTo('bounds', map); 

     var infowindow = new google.maps.InfoWindow(); 
     var marker = new google.maps.Marker({ 
      map: map, 
      anchorPoint: new google.maps.Point(0, -29) 
     }); 

    autocomplete.addListener('place_changed', function() { 
     infowindow.close(); 
     marker.setVisible(false); 
     var place = autocomplete.getPlace(); 
     if (!place.geometry) { 
     // User entered the name of a Place that was not suggested and 
     // pressed the Enter key, or the Place Details request failed. 
     window.alert("No details available for input: '" + place.name + "'"); 
     return; 
     } 

     // If the place has a geometry, then present it on a map. 
     if (place.geometry.viewport) { 
     map.fitBounds(place.geometry.viewport); 
     } else { 
     map.setCenter(place.geometry.location); 
     map.setZoom(17); // Why 17? Because it looks good. 
     } 
     marker.setIcon(/** @type {google.maps.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(35, 35) 
     })); 
     marker.setPosition(place.geometry.location); 
     marker.setVisible(true); 

     var address = ''; 
     if (place.address_components) { 
     address = [ 
      (place.address_components[0] && place.address_components[0].short_name || ''), 
      (place.address_components[1] && place.address_components[1].short_name || ''), 
      (place.address_components[2] && place.address_components[2].short_name || '') 
     ].join(' '); 
     } 

     infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address); 
     infowindow.open(map, marker); 
    }); 
    // Sets a listener on a radio button to change the filter type on Places 
    // Autocomplete. 
    function setupClickListener(id, types) { 
     var radioButton = document.getElementById(id); 
     radioButton.addEventListener('click', function() { 
     autocomplete.setTypes(types); 
     }); 
    } 

    setupClickListener('changetype-all', []); 
    setupClickListener('changetype-address', ['address']); 
    setupClickListener('changetype-establishment', ['establishment']); 
    setupClickListener('changetype-geocode', ['geocode']); 

    infoWindow = new google.maps.InfoWindow(); 
    service = new google.maps.places.PlacesService(map); 

    // The idle event is a debounced event, so we can query & listen without 
    // throwing too many requests at the server. 
    map.addListener('idle', performSearch(places)); 
    } 



    function handleLocationError(browserHasGeolocation, infoWindow, pos) { 
    infoWindow.setPosition(pos); 
    infoWindow.setContent(browserHasGeolocation ? 
          'Error: The Geolocation service failed.' : 
          'Error: Your browser doesn\'t support geolocation.'); 
    } 

    function performSearch(places) { 

     var places = places; 
    var request = { 
     bounds: map.getBounds(), 
     keyword: places 
    }; 
    service.radarSearch(request, callback); 

    } 

    function callback(results, status) { 
    if (status !== google.maps.places.PlacesServiceStatus.OK) { 
     console.error(status); 
     return; 
    } 
    for (var i = 0, result; result = results[i]; i++) { 
     addMarker(result); 
    } 
    } 
    function addMarker(place) { 
    var marker = new google.maps.Marker({ 
     map: map, 
     position: place.geometry.location, 
    // icon: { 
     // url: 'https://developers.google.com/maps/documentation/javascript/images/circle.png', 
     //anchor: new google.maps.Point(10, 10), 
     //scaledSize: new google.maps.Size(10, 17) 
     //} 

    }); 


    google.maps.event.addListener(marker, 'click', function() { 
     service.getDetails(place, function(result, status) { 
     if (status !== google.maps.places.PlacesServiceStatus.OK) { 
      console.error(status); 
      return; 
     } 
     infoWindow.setContent(result.name); 
     infoWindow.open(map, marker); 
     }); 
    }); 
    } 
    function initAutocomplete() { 
var map = new google.maps.Map(document.getElementById('map'), { 
    center: { lat: -33.8688, lng: 151.2195 }, 
    zoom: 13, 
    mapTypeId: 'roadmap' 
}); 

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

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

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


searchBtn.onclick = function() { 
    displaySearchResults(map,searchBox,markers); 
} 
} 






function displaySearchResults(map, searchBox, markers) { 
var places = searchBox.getPlaces(); 

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

// Clear out the old markers. 


// 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: 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) 
    }; 

    // Create a marker for each place. 
    markers.push(new google.maps.Marker({ 
     map: map, 
     icon: icon, 
     title: place.name, 
     position: place.geometry.location 

    })); 
    function clearMarkers() { 
     for (var i = 0; i < markers.length; i++) { 
      markers[i].setMap(map); 
     } 
    } 
    if (place.geometry.viewport) { 
     // Only geocodes have viewport. 
     bounds.union(place.geometry.viewport); 
    } else { 
     bounds.extend(place.geometry.location); 
    } 
}); 
map.fitBounds(bounds); 
} 
+0

请提供[MCVE]演示问题以及如何重现它的发布代码的描述。 “工作”对我来说,但我只是使用地方搜索功能(并且只为我一次显示一个标记)。 – geocodezip

回答

0

没有大的测试:

在您的 “功能clearMarkers()” 你必须使用:

标记[I] .setMap(NULL);

而不是标记[i] .setMap(map);这将设置标志再次

+0

我试过,它没有工作之前,我现在再试一次,但仍然是相同的结果,我添加了一个按钮,当点击时调用函数a nd应该清除地图。即使这样也行不通。 –

0

附加:

声明:本

var marker; 
var markers[]; 

在你的代码的全球顶级。函数内部只使用“marker”,而不是“var marker”。如果稍后要删除的标记已经通过“marker = new google.maps.Marker({...”)被定义,则将其随后推入具有markers.push(标记)的标记数组中。

同样在“//创建每个地方的标志。”我会用标准的方式定义和存储:

marker = new google.maps.Marker({ 
     ...}); 
markers.push(marker). 
+0

我做了建议的更改,但地图仍然不清楚,当我选择我的标记以获取不同标记位置的名称时,它只显示一个标记 –

+0

嗯,那就是点,我在哪里需要一个完整的,基本可行的例子来提供进一步的帮助。 – ReFran

+0

我创建了一个JSfiddle这是链接https://jsfiddle.net/myxo9sce/,希望它可以帮助更多 –