2

I am trying to make iOS subway map application.

I have a subway map imageview looking something like this:

enter image description here

When the user touches somewhere close (so that the user doesn't neccessarily have to touch on the exact location) to the station, I want to highlight the station with a different color.

Considering that this subway map is one large image, what would be the most efficient way to determine which CGPoint of the station is what the user intended to touch on?

For example,

What I want to do is: if one station is located at (10.0, 10.0) on the map -> have this station selected even if the user touches somewhere NEAR that point(ex: (12.3, 10.3), (10.5, 11.0), (13.0, 12.3) etc)

I am thinking of two methods to do this:

  1. have a set of array(or dictionary) which contains all x and y position of the station on image -> when the user touches on the image -> calculate the distance between the station the UITouch CGPoint using pythagoras theorem.(might be able to faster this by using appropriate data structure?)

  2. Layout transparent UIButtons for every stations on the subway map. Let the user touch on the UIButton.(I also question if this would make the app slower)

Can anyone give me some idea on how to detect the closest station point from the user's UITouch CGPoint?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Joon. P
  • 2,238
  • 7
  • 26
  • 53

1 Answers1

1

There are various solutions to your problem. What you are looking for is a Nearest Neighbor Search Algorithm.

Your method 1 looks fine if you can optimize it. Since you already know the map, storing all the station points in a sorted array would help.

ntsh
  • 759
  • 5
  • 19