none
Find out the all locations within a Rectangle RRS feed

  • Question

  • Hi,

    I need to find out all locations within a Rectangle.

    I have 14K Locations (Lat Long), which I am drawing on Canvas using Canvas Layer.

    I need to add a feature to user such that user can create a rectangle using Custom Toolbar option  and I need to find out points lying in this rectangle.

    I have Microsoft.Maps.Location array for all the points rendered on Canvas Layer.

    I need to get array of Locations which lie within the rectangle created. 

    The method Microsoft.Maps.SpatialMath.Geometry.intersection, takes IPrimitive [] array, instead of Locations.

    I have not been able to make IPrimitive array from Location array.

    -Amit


    • Edited by Amit Dube Friday, November 10, 2017 12:13 PM
    Friday, November 10, 2017 12:12 PM

Answers

All replies

  • The Bing Maps drawing tools does not support rectangles currently, but you can extend it to do so. I've put together a code sample that lets you search for pushpins on the map by drawing a rectangle here: http://bingmapsv8samples.azurewebsites.net/#Select%20Data%20in%20Drawn%20Rectangle

    I have a similar example for polygon selection areas which is a much more popular scenario: http://bingmapsv8samples.azurewebsites.net/#Select%20Data%20in%20Drawn%20Polygon%20Area


    [Blog] [twitter] [LinkedIn]

    Saturday, November 11, 2017 1:26 AM
  • Thanks Ricky,

    I have gone through the link to draw Rectangle using Bing Maps Drawing Tools. 

    I am facing challenge to select the points within the Rectangle because I am drawing points on the Canvas Layer instead of Pushpin.

    Do you know a way to convert array of Microsoft.Maps.Location objects to array of IPrimitive.

    Monday, November 13, 2017 5:58 AM
  • An IPrimitive is an interface in which all shapes inherit from (pushpin, polyline, polygon). In your case you will want to create pushpins out of your data an use it in that calculation. Alternatively, since you are using a rectangle you could also do a min/max test of latitude/longitude values as a quick and dirty lookup. This wouldn't be spatially accurate, but if your search area is the size of a city or smaller, it will be accurate enough.

    [Blog] [twitter] [LinkedIn]

    Monday, November 13, 2017 9:10 PM
  • Try this:

            function rectangleSearch(start_corner, final_corner, latlong) {
                // 2 bounding box coordinates, type Maps.Location and titto for latlong
                // Function return true if latlong is included in rectangle
                //check to see if bounding box crosses 180 degrees
                if (start_corner.longitude > final_corner.longitude)
                { if (start_corner.longitude >= latlong.longitude && final_corner.longitude <= latlong.longitude && start_corner.latitude >= latlong.latitude && final_corner.latitude <= latlong.latitude) return true;
                    else
                    if (start_corner.longitude >= latlong.longitude && final_corner.longitude <= latlong.longitude && start_corner.latitude <= latlong.latitude && final_corner.latitude >= latlong.latitude) return true;
      else return false;
                }
                else
                { if (start_corner.longitude <= latlong.longitude && final_corner.longitude >= latlong.longitude && start_corner.latitude >= latlong.latitude && final_corner.latitude <= latlong.latitude) return true;
                    else
                    if (start_corner.longitude <= latlong.longitude  && final_corner.longitude >= latlong.longitude && start_corner.latitude <= latlong.latitude && final_corner.latitude >= latlong.latitude) return true;
       else return false;
                }
          }

    Ronald

    Saturday, December 2, 2017 6:41 PM
  • If you simple want to see if a location is in a rectangle you can use the LocationRect.contains function which is a single line of code.

    [Blog] [twitter] [LinkedIn]

    Monday, December 4, 2017 4:38 PM