ArthurGuibert / FSInteractiveMap

A charting library to visualize and interact with a vector map on iOS. It's like Geochart but for iOS!
Apache License 2.0
550 stars 80 forks source link

Swift documentation #13

Open mahdigh99 opened 6 years ago

mahdigh99 commented 6 years ago

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

how can i add click handler in Swift? @ArthurGuibert @birslip

mahdigh99 commented 6 years ago

Its working for me:


 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()

    }
rakeshdev6 commented 6 years ago

Hello mahdigh99, When I add this code in the ViewDidLoad() and run the project nothing is displayed in the simulator. Do you have working example for it?

mahdigh99 commented 6 years ago

@rakeshbethu Make sure you do these things:

  1. correctly add FSInteractiveMap library in your Project ( you can easily add it by CocoaPod )
  2. make sure before deceleration of Viewcontroller you have -> import FSInteractiveMap
  3. make sure you have an SVG file named "world-continents-low" in the same directory with ViewController let mapName: String! = String("world-continents-low") if your SVG file name is different just edit this line of code.

I'm attaching my working project you can use it to understand better. (if it doesn't work in your computer just open the terminal and go to project directory and Run pod install in terminal)

Map-chart-Test.zip

rakeshdev6 commented 6 years ago

Thanks a lot mahi, This worked out really well. Very thanks for your quick turn around.

mazharhameed25 commented 5 years ago

Hello mahdigh99, i want to show state names and change color of map also want to zoom in and zoom out map. can you please help me

mahdigh99 commented 5 years ago

Hi @mazharhameed25 , I think the best way to show state names is adding state names to SVG files. you can change the color of the map by this code:

        var mapColors = [UIColor]()
            mapColors.append(UIColor(red:1.00, green:0.00, blue:0.00, alpha:1.0))
            mapColors.append(UIColor(red:1.00, green:0.20, blue:0.00, alpha:1.0))
            mapColors.append(UIColor(red:1.00, green:0.40, blue:0.00, alpha:1.0))
            mapColors.append(UIColor(red:1.00, green:0.71, blue:0.00, alpha:1.0))
            mapColors.append(UIColor(red:1.00, green:0.94, blue:0.00, alpha:1.0))
            mapColors.append(UIColor(red:0.44, green:0.71, blue:0.00, alpha:1.0))
            mapColors.append(UIColor(red:0.00, green:0.50, blue:0.00, alpha:1.0))

        mapColors.reverse()

        map.loadMap(mapName, withData:mapData, colorAxis:mapColors)

and for zoom in and & zoom out I think the best way is using scroll view,

scrollView.minimumZoomScale = 1.0; scrollView.maximumZoomScale = 3.0

but the alternative is :

var previousScale:CGFloat = 1.0

    override func viewDidLoad() {
        super.viewDidLoad()

        let gesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(sender:)))
        self.view.addGestureRecognizer(gesture)
    }

    func pinchAction(sender:UIPinchGestureRecognizer) {
        let scale:CGFloat = previousScale * sender.scale
        self.view.transform = CGAffineTransform(scaleX: scale, y: scale);

        previousScale = sender.scale
    }
mazharhameed25 commented 5 years ago

thanks alot bro

On Sat, Apr 27, 2019 at 7:33 PM mahdigh99 notifications@github.com wrote:

Hi mazharhameed, I think the best way to show state names is adding state names to SVG files. you can change the color of the map by this code:

` var mapColors = UIColor mapColors.append(UIColor(red:1.00, green:0.00, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.20, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.40, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.71, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.94, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:0.44, green:0.71, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:0.00, green:0.50, blue:0.00, alpha:1.0))

mapColors.reverse()

map.loadMap(mapName, withData:mapData, colorAxis:mapColors)`

and for zoom in and & zoom out I think the best way is using scroll view,

scrollView.minimumZoomScale = 1.0; scrollView.maximumZoomScale = 3.0

but the alternative is :

` var previousScale:CGFloat = 1.0

override func viewDidLoad() { super.viewDidLoad()

let gesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(sender:)))
self.view.addGestureRecognizer(gesture)

}

func pinchAction(sender:UIPinchGestureRecognizer) { let scale:CGFloat = previousScale * sender.scale self.view.transform = CGAffineTransform(scaleX: scale, y: scale);

previousScale = sender.scale

}

`

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ArthurGuibert/FSInteractiveMap/issues/13#issuecomment-487290911, or mute the thread https://github.com/notifications/unsubscribe-auth/AL5SV2L5LU5AZJIEZUQ7R6DPSRPZZANCNFSM4EKMK4EA .

mazharhameed25 commented 5 years ago

bro i need your help. i want to switch on new controller on map clickable area. how to get clickable state and move accordingly

On Mon, Apr 29, 2019 at 1:58 PM mazhar hameed mazharhameed25@gmail.com wrote:

thanks alot bro

On Sat, Apr 27, 2019 at 7:33 PM mahdigh99 notifications@github.com wrote:

Hi mazharhameed, I think the best way to show state names is adding state names to SVG files. you can change the color of the map by this code:

` var mapColors = UIColor mapColors.append(UIColor(red:1.00, green:0.00, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.20, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.40, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.71, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.94, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:0.44, green:0.71, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:0.00, green:0.50, blue:0.00, alpha:1.0))

mapColors.reverse()

map.loadMap(mapName, withData:mapData, colorAxis:mapColors)`

and for zoom in and & zoom out I think the best way is using scroll view,

scrollView.minimumZoomScale = 1.0; scrollView.maximumZoomScale = 3.0

but the alternative is :

` var previousScale:CGFloat = 1.0

override func viewDidLoad() { super.viewDidLoad()

let gesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(sender:)))
self.view.addGestureRecognizer(gesture)

}

func pinchAction(sender:UIPinchGestureRecognizer) { let scale:CGFloat = previousScale * sender.scale self.view.transform = CGAffineTransform(scaleX: scale, y: scale);

previousScale = sender.scale

}

`

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ArthurGuibert/FSInteractiveMap/issues/13#issuecomment-487290911, or mute the thread https://github.com/notifications/unsubscribe-auth/AL5SV2L5LU5AZJIEZUQ7R6DPSRPZZANCNFSM4EKMK4EA .

mazharhameed25 commented 5 years ago

my map is not showing when i add state names on svg file

On Mon, Apr 29, 2019 at 3:39 PM mazhar hameed mazharhameed25@gmail.com wrote:

bro i need your help. i want to switch on new controller on map clickable area. how to get clickable state and move accordingly

On Mon, Apr 29, 2019 at 1:58 PM mazhar hameed mazharhameed25@gmail.com wrote:

thanks alot bro

On Sat, Apr 27, 2019 at 7:33 PM mahdigh99 notifications@github.com wrote:

Hi mazharhameed, I think the best way to show state names is adding state names to SVG files. you can change the color of the map by this code:

` var mapColors = UIColor mapColors.append(UIColor(red:1.00, green:0.00, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.20, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.40, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.71, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:1.00, green:0.94, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:0.44, green:0.71, blue:0.00, alpha:1.0)) mapColors.append(UIColor(red:0.00, green:0.50, blue:0.00, alpha:1.0))

mapColors.reverse()

map.loadMap(mapName, withData:mapData, colorAxis:mapColors)`

and for zoom in and & zoom out I think the best way is using scroll view,

scrollView.minimumZoomScale = 1.0; scrollView.maximumZoomScale = 3.0

but the alternative is :

` var previousScale:CGFloat = 1.0

override func viewDidLoad() { super.viewDidLoad()

let gesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(sender:)))
self.view.addGestureRecognizer(gesture)

}

func pinchAction(sender:UIPinchGestureRecognizer) { let scale:CGFloat = previousScale * sender.scale self.view.transform = CGAffineTransform(scaleX: scale, y: scale);

previousScale = sender.scale

}

`

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ArthurGuibert/FSInteractiveMap/issues/13#issuecomment-487290911, or mute the thread https://github.com/notifications/unsubscribe-auth/AL5SV2L5LU5AZJIEZUQ7R6DPSRPZZANCNFSM4EKMK4EA .

mahdigh99 commented 5 years ago

bro i need your help. i want to switch on new controller on map clickable area. how to get clickable state and move accordingly On Mon, Apr 29, 2019 at 1:58 PM mazhar hameed

you can add this code to click event of each state to switch view controllers: performSegueWithIdentifier("nextViewController", sender: self)


my map is not showing when i add state names on svg file On Mon, Apr 29, 2019 at 3:39 PM mazhar

send me your SVG file to check it.

mazharhameed25 commented 5 years ago

bro. my map is not clickable when i zoom in

On Wed, May 1, 2019 at 3:09 PM mahdigh99 notifications@github.com wrote:

area. how to get clickable state and move accordingly

you can add this code to click event of each state to switch view controllers: performSegueWithIdentifier("nextViewController", sender: self)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ArthurGuibert/FSInteractiveMap/issues/13#issuecomment-488249393, or mute the thread https://github.com/notifications/unsubscribe-auth/AL5SV2ICAIWRCHVJZSYHIDLPTFT5VANCNFSM4EKMK4EA .

BrettKessler commented 2 years ago

I'm confused by the UIViewController, is there a way to add this to a view so I can get it to render a preview?