GMSMapView Draw Custom Polyline in Swift
Asked Answered
S

2

7

I made a map view with GMSMapView and I wanted to draw a polyline according to the touch of a finger. but this feature makes me stuck because I have not been able to find the appropriate reference.

in my case example like the result domain app that I want.

enter image description here

Thank you for your help.

Satinet answered 6/6, 2018 at 6:36 Comment(2)
use the mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) delegate function to determine where the user tapped and then work out if the tap location is on/near to one of the route pathsPamella
Hi @ketakiDamale, thank you for your response. but if only using the delegate it can not reach when the finger movesSatinet
P
5

This is a pretty big, complicated question, so I will outline steps for you to follow to implement this. If you need help with a specific step, I suggest you post a separate, specific question.

Here we go:

  1. When the "draw mode" button is tapped, add a view above the map

    • Make sure this view takes all touch events so that the map does not scroll when the user draws
    • Subclass this view and override touchesBegan and touchesMoved to actually do the drawing. You can follow the tutorial here for that.

    • Keep an array of all vertices of the polygon in the view's coordinate system.

  2. On touchesEnded (when the user stops drawing), convert the points from above to map coordinates.
  3. Draw a polygon from the location coordinates.
  4. Remove that view you added in step 1 and turn off drawing mode.

If you need clarification about this process don't hesitate to ask.

Panama answered 16/6, 2018 at 17:3 Comment(1)
Hi @Dopapp, thank you for your response. You are a genius, after I tried step by step I managed to apply it. it's great to get out of it. once again I am very grateful for your helpSatinet
G
2

You should draw the lines using Quartz2D since this question is kind of large i'll just point you to a good reference.

https://www.raywenderlich.com/87899/make-simple-drawing-app-uikit-swift

here you should be able to figure out how to draw to the screen.

Glasses answered 14/6, 2018 at 20:5 Comment(2)
Hi @cheesey, thank you for your response. using Quartz2D is not possible to apply to GMSMapView because all I need is polyline in the map. because of the polyline form I have to determine which marker is in it or not.Satinet
@Satinet I see I've never worked with that controller before, so I wasn't exactly sure what the problem was, But I'm glad you found a solution.Glasses

© 2022 - 2024 — McMap. All rights reserved.