1

i want to use FSInteractiveMap in swift but documentations are only is Objective-C and i can't Translate click Handler in swift.

 NSDictionary* data = @{    @"asia" : @12,
                    @"australia" : @2,
                    @"north_america" : @5,
                    @"south_america" : @14,
                    @"africa" : @5,
                    @"europe" : @20
                  };

FSInteractiveMapView* map = [[FSInteractiveMapView alloc] initWithFrame:self.view.frame];

[map loadMap:@"world-continents-low" withData:data colorAxis:@[[UIColor lightGrayColor], [UIColor darkGrayColor]]];

in swift working with:

    let map: FSInteractiveMapView = FSInteractiveMapView()
    map.frame = self.view.frame
    var mapData = [String: Int]()
    mapData["IR-01"] = 0
    mapData["IR-02"] = 10
    var mapColors = [UIColor]()
    mapColors.append(UIColor(red:0.26, green:0.112, blue:0.0, alpha:1.0))
    mapColors.append(UIColor(red:0.45, green:0.132, blue:0.0, alpha:1.0))


    map.loadMap("iranHigh", withData:mapData, colorAxis:mapColors)
    view.addSubview(map)
    view.setNeedsDisplay()

its working correctly but i cant add click Handler

here is documentation in Objective-C:

FSInteractiveMapView* map = [[FSInteractiveMapView alloc] initWithFrame:CGRectMake(16, 96, self.view.frame.size.width - 32, 500)];
[map loadMap:@"usa-low" withColors:nil];

[map setClickHandler:^(NSString* identifier, CAShapeLayer* layer) {
    if(_oldClickedLayer) {
        _oldClickedLayer.zPosition = 0;
        _oldClickedLayer.shadowOpacity = 0;
    }

_oldClickedLayer = layer;

// We set a simple effect on the layer clicked to highlight it
layer.zPosition = 10;
layer.shadowOpacity = 0.5;
layer.shadowColor = [UIColor blackColor].CGColor;
layer.shadowRadius = 5;
layer.shadowOffset = CGSizeMake(0, 0);
}];

OR:

[map setClickHandler:^(NSString* identifier, CAShapeLayer* layer) {
self.detailDescriptionLabel.text = [NSString stringWithFormat:@"Continent clicked: %@", identifier];}];


how can i do it in Swift?

1 Answer 1

1

add some code, looks like that

    override func viewDidLoad() {
        super.viewDidLoad()

        let map: FSInteractiveMapView = FSInteractiveMapView()
        weak var oldClickedLayer = CAShapeLayer()

        var mapData              = [String: Int]()
        mapData["asia"]          = 12
        mapData["australia"]     = 2
        mapData["north_america"] = 5
        mapData["south_america"] = 14
        mapData["africa"]        = 5
        mapData["europe"]        = 20

        var mapColors = [UIColor]()
        mapColors.append(UIColor.lightGray)
        mapColors.append(UIColor.darkGray)
        map.frame = self.view.frame
        map.clickHandler = {(identifier: String? , _ layer: CAShapeLayer?) -> Void in
            if (oldClickedLayer != nil) {
                oldClickedLayer?.zPosition = 0
                oldClickedLayer?.shadowOpacity = 0
            }
            oldClickedLayer = layer
            // We set a simple effect on the layer clicked to highlight it
            layer?.zPosition = 10
            layer?.shadowOpacity = 0.5
            layer?.shadowColor = UIColor.black.cgColor
            layer?.shadowRadius = 5
            layer?.shadowOffset = CGSize(width: 0, height: 0)

            print("clicked")
        }
        let mapName: String! = String("world-continents-low")
        map.loadMap(mapName, withData:mapData, colorAxis:mapColors)

        view.addSubview(map)
        view.setNeedsDisplay()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

that worked for me. Don't forget to import library

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.