Moving an MKPointAnnotation in Swift

If you’ve been working with MapKit, you may have had to go about moving annotations from one point to another.
It’s easy to simply remove an annotation and add a new annotation in the second coordinate, but this way will involve you having to set the title and subtitle again. Visually this won’t look as appealing.

What you’ll want to do first, is to create a new project in xCode and head straight into your ViewController.

Start by importing MapKit.

import MapKit

We’re now going to add a variable to store our annotation.

private let myAnnotation = MKPointAnnotation()

In our viewDidLoad() function will exist the rest of the code.

We’ll create two variables to hold our first and second coordinate. Set the coordinates to whatever, I’ll be using my home of Dublin as my start coordinate.

let startPosition = CLLocationCoordinate2D(latitude: 53.3498, longitude: -6.2603)
let destinationPosition = CLLocationCoordinate2D(latitude: 52.3369, longitude: -6.4633)

When we open the app for the first time, let’s have the map center be our start coordinate so we don’t have to go looking for our two points.

let region = MKCoordinateRegionMakeWithDistance(startPosition, 100000, 100000)
mapView.setRegion(region, animated: true)

We’ll add our annotation directly below this snippet.

myAnnotation.coordinate = startPosition
mapView.addAnnotation(myAnnotation)

Now that the code for the map is done. Let’s add the MKMapView to your storyboard and set the constraints to our ViewController. I’ve set my constraints to zero so the map will take up the full width and height of the ViewController. Be sure to also link your MKMapView to the mapView outlet created earlier and also to link your delegate to your ViewController.

 

Now to create the function that will move our annotation. For the purpose of the demo, we’ll add a timer to run this function after 5 seconds from when the user launches the app. Head back to the ViewController and add the following code.

        weak var timer: Timer?
        
        func movePosition() {
            // Set timer to run after 5 seconds.
            timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: false) { [weak self] _ in
                // Set animation to last 4 seconds.
                UIView.animate(withDuration: 4, animations: {
                    // Update annotation coordinate to be the destination coordinate
                    self?.myAnnotation.coordinate = destinationPosition
                }, completion: nil)
            }
        }

When you build and run the app, you should now be able to see your MKPointAnnotation move from one coordinate to another like in the video below.

I’ve made the source of this app available on my GitHub if you prefer to clone the project with an MIT license.

Connecting to a Cisco RV345 PPTP VPN from MacOS

Apples MacOS no longer supports a PPTP VPN client directly from the System Preferences app. All that can be found online are premium apps and freeware that is very limited in configuration options.

Pair this with the Cisco RV345 which supports a PPTP VPN minus all the configurable PPTP options, you really have to tailor your client to be able to connect to this server.

Fortunately, if you don’t want to shell out the money for a PPTP client, deep in the darwin internals there is PPTP functionality.

If you’re cool with copy & pasting into the terminal, this will be an easy one to set up.

First up is the RV345. This router doesn’t support any CLI configuration which is part of the problem with how not so configurable the PPTP VPN is.

If you’re setting up your router for the first time, you can connect to the web interface using the address 192.168.1.1

The default login is:

username: cisco

password: cisco

Under the VPN settings we can configure our IPs. Here’s how I have configured mine. Note that MPPE encryption is disabled. You’ll find that it is also disabled in my VPN profile below.

PPTP Configuration RV345

Now for setting up our mac.

First create a directory with the mkdir command where we are going to put our PPTP connection profile.

sudo mkdir /etc/ppp/peers

Then we, want to create a file that will hold our profile configuration to be compatible with the RV345 PPTP VPN.

touch /etc/ppp/peers/myvpn

Using the configuration below and your favorite text editor, you can connect to the PPTP VPN. Just be sure to change the remoteaddress, user and password lines to for your own server.

/etc/ppp/peers/myvpn
plugin PPTP.ppp
noauth
# logfile /tmp/ppp.log
remoteaddress "IP Address/hostname"
user "VPN username"
password "VPN user password"
redialcount 1
redialtimer 5
idle 1800
# mru 1368
# mtu 1368
receive-all
novj 0:0
ipcp-accept-local
ipcp-accept-remote
# noauth
refuse-eap
refuse-chap-md5
refuse-mschap
refuse-mschap-v2
refuse-chap
hide-password
mppe-stateless
#mppe-128
# require-mppe-128
looplocal
nodetach
ms-dns 8.8.8.8
ms-dns 192.168.10.27
usepeerdns
# ipparam gwvpn
defaultroute
debug

 

Now it’s time to connect.

Pasting the following command into the terminal will establish the connection between your mac and the VPN.

sudo pppd call myvpn

After running this command, you’ll receive an output similar to what is below and you’ll be connected to the PPTP VPN.