none
Bing Maps API - Get LocationRect bounds from iPrimitive RRS feed

  • Question

  • Hi,

    Is it possible to get the LocationRect bounds from an iPrimitive object?

    The primitive class seems to have a bounds property - but I just get undefined when I try and access it on an iPrimitive. I guess they are different beasts.

    Trying to simplify my code and reduce the need to calculate the bounds in PostGIS and send them through to my addWkt function.

    Code:

            var wkt = Microsoft.Maps.WellKnownText.read(data.wkt_4326, style);
            layer.add(wkt);
    
            var LocationRect = wkt.bounds;
            map.setView({bounds:LocationRect});

    Thanks,

    Steve


    Steve Goddard - Biodiversity Information Service


    Wednesday, February 1, 2017 5:32 PM

Answers

  • The bounds property you are referencing is not a public property and is not documented, it shouldn't be used. Also, if the WellKnownText you are parsing is a Multipoint, MultiLinestring, MultiPolygon or GeometryCollection, the read function will return an array of shapes, not an individual shape. There are few options to get the bounding box.

    The first is to use the SpatialMath module and the geometry getBounds function. https://msdn.microsoft.com/en-US/library/mt762861.aspx Here is a code sample:

    Microsoft.Maps.loadModule('Microsoft.Maps.SpatialMath', function () {
        var bounds = Microsoft.Maps.SpatialMath.Geometry.bounds(shape);
    });
    

    The second option is to use the following code to extract all the Locations from the shapes and then use that to create a LocationRect:

    function getBounds(shape){
    	if (shape instanceof Microsoft.Maps.Pushpin) {
    		return new Microsoft.Maps.LocationRect(shape.getLocation(), 0.0001, 0.0001);
    	} else if (shape instanceof Microsoft.Maps.Polyline || shape instanceof Microsoft.Maps.Polygon) {
    		//Assumes first ring in polygon is the exterior ring.
    		var locs = shape.getLocations();		
    		return Microsoft.Maps.LocationRect.fromLocations(locs);
    	} else if (shapes instanceof Array) {
    		var locs = [];
    		var shapeLocs;		
    		
    		for(var i=0;i<shapes.length;i++){
    			shapeLocs = getBounds(shape[i]);
    			
    			if(shapeLocs){
    				locs = locs.concat(shapeLocs);
    			}
    		}
    		
    		return Microsoft.Maps.LocationRect.fromLocations(locs);
    	}
    	
    	return null;
    }

    The third is to wait a few months and there will be a new feature added to the LocationRect class that will make this a one line task and will be much more efficient at merging bounding boxes of shapes in an array. This is a feature requested that our dev team is planning to add in one of the next few releases.


    [Blog] [twitter] [LinkedIn]

    • Proposed as answer by Ricky_Brundritt Wednesday, February 1, 2017 10:09 PM
    • Marked as answer by SteveGoddard14 Thursday, February 2, 2017 11:19 AM
    Wednesday, February 1, 2017 10:09 PM

All replies

  • The bounds property you are referencing is not a public property and is not documented, it shouldn't be used. Also, if the WellKnownText you are parsing is a Multipoint, MultiLinestring, MultiPolygon or GeometryCollection, the read function will return an array of shapes, not an individual shape. There are few options to get the bounding box.

    The first is to use the SpatialMath module and the geometry getBounds function. https://msdn.microsoft.com/en-US/library/mt762861.aspx Here is a code sample:

    Microsoft.Maps.loadModule('Microsoft.Maps.SpatialMath', function () {
        var bounds = Microsoft.Maps.SpatialMath.Geometry.bounds(shape);
    });
    

    The second option is to use the following code to extract all the Locations from the shapes and then use that to create a LocationRect:

    function getBounds(shape){
    	if (shape instanceof Microsoft.Maps.Pushpin) {
    		return new Microsoft.Maps.LocationRect(shape.getLocation(), 0.0001, 0.0001);
    	} else if (shape instanceof Microsoft.Maps.Polyline || shape instanceof Microsoft.Maps.Polygon) {
    		//Assumes first ring in polygon is the exterior ring.
    		var locs = shape.getLocations();		
    		return Microsoft.Maps.LocationRect.fromLocations(locs);
    	} else if (shapes instanceof Array) {
    		var locs = [];
    		var shapeLocs;		
    		
    		for(var i=0;i<shapes.length;i++){
    			shapeLocs = getBounds(shape[i]);
    			
    			if(shapeLocs){
    				locs = locs.concat(shapeLocs);
    			}
    		}
    		
    		return Microsoft.Maps.LocationRect.fromLocations(locs);
    	}
    	
    	return null;
    }

    The third is to wait a few months and there will be a new feature added to the LocationRect class that will make this a one line task and will be much more efficient at merging bounding boxes of shapes in an array. This is a feature requested that our dev team is planning to add in one of the next few releases.


    [Blog] [twitter] [LinkedIn]

    • Proposed as answer by Ricky_Brundritt Wednesday, February 1, 2017 10:09 PM
    • Marked as answer by SteveGoddard14 Thursday, February 2, 2017 11:19 AM
    Wednesday, February 1, 2017 10:09 PM
  • Thanks Ricky, I'll have a play with these - but I suspect I can wait for the new feature.

    Thanks again,

    Steve


    Steve Goddard - Biodiversity Information Service

    Thursday, February 2, 2017 11:20 AM