none
Polygon questions RRS feed

  • Question

  • Does V8 return the center of a Polygon yet and how can I "name" a polygon so I can act on it when clicked? My app: http://www.bmap8.com/

    ...with a firm reliance on the protection of Divine Providence.

    Wednesday, May 11, 2016 11:37 AM

Answers

  • You can add custom information to any shape in Bing Maps. Pushpins, Polylines, and Polygons all derive from a common interface called IPrimitive. You can add custom data to any IPrimitive classes using the metadata property. For example:

    var polygon = new Microsoft.Maps.Polygon([new Microsoft.Maps.Location(center.latitude - 0.05, center.longitude - 0.15),
        new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude - 0.15),
        new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude - 0.05)], null);
    
    polygon.metadata = {
     customProperty: "custom value"
    };
    
    map.entities.push(polygon);


    [Blog] [twitter] [LinkedIn]

    Thursday, May 19, 2016 4:14 PM

All replies

  • The geometry functions for the spatial math library which will include this capability is not yet released and likely won't be until sometime this summer.

    Note that there are a lot of different ways to calculate a center for a polygon and get different results. For example:

    • Calculate the bounding box of polygon and then take the center of the bounding box. Pretty easy to do in Bing Maps, simply grab the array of locations for a polygon, pass it into the LocationRect.fromLocations function and then use the center property of the generated bounding box. This would take about 2-3 lines of code.
    • Calculate the mean average of the lat/lon values.
    • Calculate the median lat/lon values.
    • Calculate the center of gravity of the polygon.

    One issue with all of these is that there is no guarantee that the center point will actually intersect the polygon itself. You could go a step further and take one of the values calculated above and then snap it to the closest point on the polygon.


    [Blog] [twitter] [LinkedIn]

    Wednesday, May 11, 2016 5:11 PM
  • Fortunately, Summer's right around the corner :-) I'm displaying U.S. County polygons on your Map and I'd like to center the "county name" on the polygon and then display information about that county when I click the polygon. Centering and adding a text layer on top of a polygon is one issue, but as a separate issue, I can't seem to pass any sort of identifying information to the polygon when I create it, so that I know what to do with it when clicked (like the county name...)

    Currently, I'm creating an invisible marker and assigning a "title" to the marker and then reacting to the title click. Pretty funky :-) Anyway, just passing along my experience with your API.

    Bmap8


    ...with a firm reliance on the protection of Divine Providence.

    Thursday, May 12, 2016 12:33 AM
  • I've included county centroids into my database and everything is working great. I've also got a nice geoJson overlay of the U.S. working. Click on the map and then click "Counties" to explore... Bmap8 

    ...with a firm reliance on the protection of Divine Providence.

    Sunday, May 15, 2016 12:21 AM
  • The second part of that question was: is there a way to name individual polygons so you can act on them when clicked. Apparently not, but I devised a technique where I set one of the fill colors values as an increment, then get the fill color when a polygon is clicked, and use that number as an index into my array of names. Setting the opacity to zero of course...  Fortunately, Texas only has 254 counties :-) Bmap8

    ...with a firm reliance on the protection of Divine Providence.



    Sunday, May 15, 2016 6:56 PM
  • You can add custom information to any shape in Bing Maps. Pushpins, Polylines, and Polygons all derive from a common interface called IPrimitive. You can add custom data to any IPrimitive classes using the metadata property. For example:

    var polygon = new Microsoft.Maps.Polygon([new Microsoft.Maps.Location(center.latitude - 0.05, center.longitude - 0.15),
        new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude - 0.15),
        new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude - 0.05)], null);
    
    polygon.metadata = {
     customProperty: "custom value"
    };
    
    map.entities.push(polygon);


    [Blog] [twitter] [LinkedIn]

    Thursday, May 19, 2016 4:14 PM
  • ... and retrieve it with 'e.target.metadata.[property name] on click. That's what I was looking for, thanks Ricky!

    I guess I should have dug deeper into the API other than V8 docs, or I just missed it...


    ...with a firm reliance on the protection of Divine Providence.

    Thursday, May 19, 2016 6:45 PM