0

my requirement is similar as you can see in below link as references (How make polygon without intersection in Swift)

But this solution is in google map sdk, i want same solution in bing map. Please help anyone. i am using below code....

`   
 func addPin(atLocation location: MSGeopoint, withTitle title: String) {
        let pushpin = MSMapIcon()
        pushpin.location = location
        pushpin.title = title
        if self.pinImage != nil {
        pushpin.image = self.pinImage
        pushpin.normalizedAnchorPoint = CGPoint(x: 0.5, y: 1.0)
        }
    
        //******************** my code to draw line between pin
    
        let position = MSGeoposition(latitude: location.position.latitude, longitude:       location.position.longitude)
        positions.append(position)
        highlightArea(position: positions)
    
        //****************************
    
        pushpin.accessibilityLabel = "Pushpin"
        if title.isEmpty {
        untitledPushpinCount += 1;
        pushpin.accessibilityValue = String(format: "Untitled %d", untitledPushpinCount);
        } else {
        pushpin.accessibilityValue = title;
        }
        self.pinLayer.elements.add(pushpin)
    }

    //***********************  my code to draw line between pin
    func highlightArea(position: [MSGeoposition]) {
    
    let geopath = MSGeopath(positions:position)
    
    //for polygon draw
    let mapPolygon = MSMapPolygon()
    mapPolygon.paths = [geopath]
    mapPolygon.zIndex = 1
    mapPolygon.fillColor = .clear//.red.withAlphaComponent(0.1)
    mapPolygon.strokeColor = .blue
    mapPolygon.strokeWidth = 1
    mapPolygon.strokeDashed = true
    
    let highlightsLayer = MSMapElementLayer()
    highlightsLayer.zIndex = 1
    highlightsLayer.elements.add(mapPolygon)
    mapView.layers.add(highlightsLayer)
    
    
    //for polyline draw
    /*   let mapPolyline = MSMapPolyline()
     mapPolyline.path = geopath
     mapPolyline.strokeColor = UIColor.black
     mapPolyline.strokeWidth = 1
     mapPolyline.strokeDashed = false
     
     // Add Polyline to a layer on the map control.
     let linesLayer = MSMapElementLayer()
     linesLayer.zIndex = 1
     linesLayer.elements.add(mapPolyline)
     mapView.layers.add(linesLayer);
     */
    
    }
`
2
  • Can you add some more details. Are you looking at just creating rectangles like the other post you linked to, or do you want to support any polygon? If it is the later, are you looking to be able to add a bunch of pins all over the map and then create a polygon around it? If so do you want a convex hull (like putting an elastic around your pins, or a concave hull? Commented Apr 28, 2023 at 16:17
  • I want to add a bunch of pins any where over the map and then create a polygon around it as a concave hull. Commented Apr 29, 2023 at 17:41

1 Answer 1

0

Calculating a concave hull is fairly complex as it requires a few hundred lines of code. You will most likely want to use an existing library for this, such as https://github.com/SanyM/ConcaveHull

Note that there is no single concave hull that represents a set of points. When creating concave hulls you specify a level on "concavity" that basically indicates the maximum distance between point allowed before the edge moves inwards.

If you use the library I linked to, you could take your MSGeoposition(s), convert them into an array of CLLocationCoordinate2D as required by that library, calculate the hull, then convert the resulting array of CLLocationCoordinate2D into a MSGeopath that can be used to create a polygon.

Here is a function for creating a MSPolygon concave hull from an array of MSGeoposition(s) (I'm not an iOS dev, so this is approximate):

func getConcaveHull(_ positions: ArrayList<MSGeoposition>) -> MSMapPolygon {
    //Convert the array of MSGeoposition into an array of CLLocationCoordinate2D.       
    let coords = positions.map { CLLocationCoordinate2D(latitude: $0.getLatitude(), longitude: $0.getLongitude()) }
    
    var hull = Hull()
    
    //Adjust the concavity as you see fit.
    hull.concavity = 0.2
    
    //Calculate the coordinates of the concave hull.
    let hulledCoords = hull.hull(coordinates: coords)
    
    //Create an array of MSGeoposition from the hulled coordinates.
    let hullPositions = hulledCoords.map { MSGeoposition(latitude: $0.latitude, longitude: $0.longitude) }
    
    //Create a geopath.
    let geopath = MSGeopath(positions: hullPositions)
    
    //Create a polygon.
    let mapPolygon = MSMapPolygon() 
    mapPolygon.paths = [geopath]

    //Add your polygon style options
    mapPolygon.zIndex = 1
    mapPolygon.fillColor = .clear//.red.withAlphaComponent(0.1)
    mapPolygon.strokeColor = .blue
    mapPolygon.strokeWidth = 1
    mapPolygon.strokeDashed = true

    //Return the polygon.
    return mapPolygon;
}

Since you are using a Microsoft map control, it is a little easier to do this with the Azure Maps iOS SDK since it uses the native CLLocationCoordinate2D class rather than creating its own position/coordinate class. Here is a modified version of the above function for use with Azure Maps.

func getConcaveHull(_ coords: ArrayList<CLLocationCoordinate2D>) -> Polygon {
    var hull = Hull()
    
    //Adjust the concavity as you see fit.
    hull.concavity = 0.2
    
    //Calculate the coordinates of the concave hull.
    let hulledCoords = hull.hull(coordinates: coords)
    
    return Polygon(hulledCoords)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.