none
Setting Zoom Range for GeoDataAPI RRS feed

  • Question

  • It'd be much appreciated if someone could assist with modifying the code below to set a zoom range for the GeoData. 

    Link to Zoom Range code:

    https://github.com/Microsoft/BingMapsV8CodeSamples/blob/master/Samples/Layers/Limit%20Layer%20to%20Zoom%20Range.html


      var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
       center: new Microsoft.Maps.Location(4.318729, 148.452588),
       zoom: 2
      });
    
    
      //Load the Spatial Data Services module.
      Microsoft.Maps.loadModule('Microsoft.Maps.SpatialDataService', function() {
       //Create an array of state boundaries to retrieve.
    
       var countries1 = ['Russia', 'France', 'Italy'];
       var geoDataRequestOptions = {
        entityType: 'CountryRegion',
        getAllPolygons: false,
       };
       //Use the GeoData API manager to get the State
       Microsoft.Maps.SpatialDataService.GeoDataAPIManager.getBoundary(countries1, geoDataRequestOptions, map, function(data) {
        if (data.results && data.results.length > 0) {
         map.entities.push(data.results[0].Polygons);
        }
       });
      });

    Link to GeoDataAPI:
    https://www.bing.com/api/maps/sdk/mapcontrol/isdk/sdsloadsingleboundary#JS


    Tuesday, February 26, 2019 7:54 AM

Answers

  • Hi John, 

    Much more simpler than that... 

    In any case, I've figured it out:  

      var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
       center: new Microsoft.Maps.Location(4.318729, 148.452588),
       zoom: 2
      });
    
            /////////////////////////////////////////////////////////
            ////////Countries 
            var layer1 = new Microsoft.Maps.Layer();
            //Add zoom information as metadata of the layer. 
            layer1.metadata = {
              zoomRange: {
                min: 1,
                max: 3
              }
            };
            //Load the Spatial Data Services module.
            Microsoft.Maps.loadModule('Microsoft.Maps.SpatialDataService', function() {
              //Create an array of state boundaries to retrieve.
              var countries1 = ['Russia', 'France', 'Italy'];
              var geoDataRequestOptions = {
                entityType: 'CountryRegion',
                getAllPolygons: false,
              };
              //Use the GeoData API manager to get the State
              Microsoft.Maps.SpatialDataService.GeoDataAPIManager.getBoundary(countries1, geoDataRequestOptions, map, function(data) {
                if (data.results && data.results.length > 0) {
                  layer1.add((data.results[0].Polygons));
                }
              });
           
            map.layers.insert(layer1);
    		})
    		
    		
    		//Add a viewchangeend event to the map so that it updates the visibility of the layers. 
            Microsoft.Maps.Events.addHandler(map, 'viewchangeend', updateLayerVisibility);
            //Do an initial update of the visibility. 
            updateLayerVisibility();
            function updateLayerVisibility() {
              //Get the current zoom level of the map. 
              var zoom = map.getZoom();
              var layer;
              //Loop through the layers in the map and check to see if it has zoomRange metadata. 
              for (var i = 0; i < map.layers.length; i++) {
                layer = map.layers[i];
                if (layer.metadata && layer.metadata.zoomRange) {
                  if (zoom >= layer.metadata.zoomRange.min && zoom <= layer.metadata.zoomRange.max) {
                    layer.setVisible(true);
                  } else {
                    layer.setVisible(false);
                  }
                }
    		  }
    		}
    	

    • Marked as answer by nick-shannon Thursday, March 7, 2019 3:52 AM
    Thursday, March 7, 2019 3:52 AM

All replies

  • Hello Nick,

    To clarify, you are trying to implement a default zoom level depending on the boundary returned from the GeoData API? (Dynamic so the zoom level changes dependent on the actual size of the boundary?)


    Tuesday, March 5, 2019 5:35 PM
    Moderator
  • Hi John, 

    Much more simpler than that... 

    In any case, I've figured it out:  

      var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
       center: new Microsoft.Maps.Location(4.318729, 148.452588),
       zoom: 2
      });
    
            /////////////////////////////////////////////////////////
            ////////Countries 
            var layer1 = new Microsoft.Maps.Layer();
            //Add zoom information as metadata of the layer. 
            layer1.metadata = {
              zoomRange: {
                min: 1,
                max: 3
              }
            };
            //Load the Spatial Data Services module.
            Microsoft.Maps.loadModule('Microsoft.Maps.SpatialDataService', function() {
              //Create an array of state boundaries to retrieve.
              var countries1 = ['Russia', 'France', 'Italy'];
              var geoDataRequestOptions = {
                entityType: 'CountryRegion',
                getAllPolygons: false,
              };
              //Use the GeoData API manager to get the State
              Microsoft.Maps.SpatialDataService.GeoDataAPIManager.getBoundary(countries1, geoDataRequestOptions, map, function(data) {
                if (data.results && data.results.length > 0) {
                  layer1.add((data.results[0].Polygons));
                }
              });
           
            map.layers.insert(layer1);
    		})
    		
    		
    		//Add a viewchangeend event to the map so that it updates the visibility of the layers. 
            Microsoft.Maps.Events.addHandler(map, 'viewchangeend', updateLayerVisibility);
            //Do an initial update of the visibility. 
            updateLayerVisibility();
            function updateLayerVisibility() {
              //Get the current zoom level of the map. 
              var zoom = map.getZoom();
              var layer;
              //Loop through the layers in the map and check to see if it has zoomRange metadata. 
              for (var i = 0; i < map.layers.length; i++) {
                layer = map.layers[i];
                if (layer.metadata && layer.metadata.zoomRange) {
                  if (zoom >= layer.metadata.zoomRange.min && zoom <= layer.metadata.zoomRange.max) {
                    layer.setVisible(true);
                  } else {
                    layer.setVisible(false);
                  }
                }
    		  }
    		}
    	

    • Marked as answer by nick-shannon Thursday, March 7, 2019 3:52 AM
    Thursday, March 7, 2019 3:52 AM