Remove all functions from the data layer

I used something like:

var map; function initialize() { map = new google.maps.Map(document.getElementById('map-canvas'), { zoom: 4, center: {lat: -28, lng: 137.883} }); map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json'); } google.maps.event.addDomListener(window, 'load', initialize); 

to upload a geojson form file to the map.data layer of my map. There are several classes of functions in the form file that define the polygons that need to be drawn on the map. So far I have not had a problem.

Later, however, I want to load another geojson file on top of another (replacing the drawn "functions" on the map). When you simply upload another file on top of another, it simply redraws it on top of the other. How do you clear the map.data layer of all functions before loading them into a new geojson form file?

I tried using map.data.remove(feature) with an outline, but I cannot get all the functions from the map.data layer.

+6
source share
2 answers

This will repeat all functions and remove them from map.data strong>.

 map.data.forEach(function(feature) { // If you want, check here for some constraints. map.data.remove(feature); }); 

Edit 1: Explanation Map data for each function uses callbacks, so you must give the callback function as a parameter:

 var callback = function(){ alert("Hi, I am a callback"); }; map.data.forEach(callback); 

Now for each item in the data a warning will appear. It is also possible to give callbacks with a parameter, as in the code above.

  var callback = function(feature) { // If you want, check here for some constraints. map.data.remove(feature); }; map.data.forEach(callback); 

Additional explanations and examples: http://recurial.com/programming/understanding-callback-functions-in-javascript/

+16
source

map.data seems to be a collection of feature classes.

This way you can use map.data to iterate and delete each function in the collection

+1
source

Source: https://habr.com/ru/post/971340/


All Articles