<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by William Liu on Medium]]></title>
        <description><![CDATA[Stories by William Liu on Medium]]></description>
        <link>https://medium.com/@williamliu_19785?source=rss-2d37b5a9d154------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*U4c7Qi3VUqy78RFM.</url>
            <title>Stories by William Liu on Medium</title>
            <link>https://medium.com/@williamliu_19785?source=rss-2d37b5a9d154------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 13:16:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@williamliu_19785/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Drawing on MKMapView]]></title>
            <link>https://medium.com/@williamliu_19785/drawing-on-mkmapview-daceab966177?source=rss-2d37b5a9d154------2</link>
            <guid isPermaLink="false">https://medium.com/p/daceab966177</guid>
            <category><![CDATA[cocoa]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[mkmapview]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[William Liu]]></dc:creator>
            <pubDate>Mon, 13 Mar 2017 08:59:17 GMT</pubDate>
            <atom:updated>2017-03-15T07:40:21.495Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*uRpQAu4NlFh9yP3d1QHHCQ.gif" /></figure><h4>My note of drawing polyline and polygon on MKMapView</h4><p>Firstly, drag Map View to View Controller and add constraints. Drag this Map View to your View Controller to create a @IBOutlet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*4rCiVl0J3yzO_aWIXUfKNA.jpeg" /></figure><p>Remember to make View Controller conform to MKMapViewDelegate and add the delegate function to View Controller as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2574fe12037fa483873995a5b47882d6/href">https://medium.com/media/2574fe12037fa483873995a5b47882d6/href</a></iframe><p>You can set colors to whatever you prefer.</p><p>Now let’s draw. Add a property which holds coordinates in the View Controller</p><pre>var points = [CLLocationCoordinate2D]()</pre><p>For this tutorial, set <strong><em>isUserInteractionEnabled</em></strong> to false so that the map won’t be dragged.</p><pre>@IBOutlet weak var mapView: MKMapView!<br>override func viewDidLoad() {</pre><pre>   super.viewDidLoad()</pre><pre>   mapView.isUserInteractionEnabled = false</pre><pre>}</pre><p>Now we need to override <strong>touchesBegan </strong>function:</p><pre>override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {</pre><pre>   mapView.removeOverlays(mapView.overlays)</pre><pre>   if let touch = touches.first {</pre><pre>      let coordinate = mapView.convert(touch.location(in: mapView),      toCoordinateFrom: mapView)</pre><pre>      points.append(coordinate)</pre><pre>   }</pre><pre>}</pre><p>Above code detect the touch location on the Map View and covert it to coordinate. Then, add that coordinate to the points array.</p><p>Override function <strong>touchesMoved</strong> to get every point on the path:</p><pre>override func touchesMoved(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {</pre><pre>   if let touch = touches.first {</pre><pre>      let coordinate = mapView.convert(touch.location(in: mapView),       toCoordinateFrom: mapView)</pre><pre>       points.append(coordinate)</pre><pre>       let polyline = MKPolyline(coordinates: points, count: points.count)</pre><pre>       mapView.add(polyline)</pre><pre>}</pre><p>Lastly, finish drawing with MKPolygon to highlight selection area.</p><pre>override func touchesEnded(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {</pre><pre>   let polygon = MKPolygon(coordinates: &amp;points, count: points.count)</pre><pre>   mapView.add(polygon)</pre><pre>   points = [] // Reset points</pre><pre>}</pre><p>The Complete Code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0a6cd41b1ac265bac1f2e272850da4f/href">https://medium.com/media/a0a6cd41b1ac265bac1f2e272850da4f/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=daceab966177" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>