3

Basically i have a list of POI's (name,lat,long) and i want to draw them on the UIView, relative to my current lat/long. I'm looking for some best practice for mapping these POI (lat/long) to a UIView.

I don't want to use MKMapView (no need for displaying map-data).

I was reading:

http://developer.apple.com/library/ios/#documentation/general/conceptual/Devpedia-CocoaApp/CoordinateSystem.html

But I'm clueless how i get from a CLLocation to a (x,y) on my UIView. I only want to draw those POI's around my current location. So, for example if my screen would represent a 20 by 30 KM region, how do i map my POI's to their corresponding (x,y) coordinates?

Thanks.

Daniyar
  • 2,975
  • 2
  • 26
  • 39
Roger
  • 7,535
  • 5
  • 41
  • 63

4 Answers4

2

What you're doing is a little strange, but you can convert latitude/longitude to a CGPoint-like struct called an MKMapPoint. An MKMapPoint has an x and y value which correspond to points on a map. Imagine if you laid out a flat map of the world, and 0,0 was the top left. MKMapPoint is a point on that map using that coordinate system.

Use the function MKMapPointForCoordinate() to convert a CLLocationCoordinate2D to an MKMapPoint

MKMapPoint myMapPoint = MKMapPointForCoordinate(myLocationCoordinate);

When you get the list of points, you'll have to do something like finding the max and min x and y values, then fitting all the points into your view using those values, otherwise you'll end up with a load of very close points in one place in your view.

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • But **does** this means i need to user MKMap? I go play with it. I don't understand whats *strange*?, i basically want MKMapView without the satellite imagery. – Roger Dec 17 '12 at 12:15
  • You can use `MKMapPointForCoordinate()` without an `MKMapView`. You'll still need to import the MapKit framework to be able to use the function though. Also, you can have a map view without satellite imagery. Just set `MKMapType` to `MKMapTypeStandard`. – nevan king Dec 17 '12 at 12:51
0

My guess is that, for a 20KM by 30KM region, you can consider the earth to be flat and there fore linearly extrapolate the coordinates. I am sure you can google and find out as to how much distance is a difference in 0.00001 in latitude and longitude.

So if you have 20Km to be represented on X axis, and your current location is 30.1234567 in latitude, and 0.0000001 is 1 km then you can put your coordinate in the center of the screen and 30.1234557 as the left most X coordinate and so on.

I am not trying to provide an answer here, but just trying to think out loud, because I wanted to do some thing similar as well and did it as an Internet based app (without display though), where given two coordinates, I had to find the distance between them.

Srikanth
  • 1,725
  • 2
  • 10
  • 11
  • Don't try this. At the sizes you are considering (20km) the transform is definitely not linear, nor is it consistent. In my day-job, we discovered that you can get transform distortion over a distance of *metres* – Jeff Laing Dec 16 '12 at 01:18
  • Well 20*30KM was just a example. I would like to pinch and zoom to a smaller (or larger) area. – Roger Dec 17 '12 at 12:13
0

There are many (many) different approaches to modelling the planet and translating 3D coordinates onto a 2D surface, and the errors introduced by the various methods vary depending on what part of the globe you are. This question seems to cover most of what you are after though:

Converting Longitude & Latitude to X Y on a map with Calibration points

Community
  • 1
  • 1
Tark
  • 5,153
  • 3
  • 24
  • 23
0

I think its best way (correctly work for Mercator projection map):

extension UIView
{
    func addLocation(coordinate: CLLocationCoordinate2D)
    {
        // max MKMapPoint values
        let maxY = Double(267995781)
        let maxX = Double(268435456)

        let mapPoint = MKMapPointForCoordinate(coordinate)

        let normalizatePointX = CGFloat(mapPoint.x / maxX)
        let normalizatePointY = CGFloat(mapPoint.y / maxY)

        let pointView = UIView(frame: CGRectMake(0, 0, 5, 5))
        pointView.center = CGPointMake(normalizatePointX * frame.width, normalizatePointY * frame.height)

        pointView.backgroundColor = UIColor.blueColor()

        addSubview(pointView)
    }
}

My simple project for adding coordinate on UIView: https://github.com/Glechik/MapCoordinateDrawer